AddToTagVisitor: skip insert when a semantically-equal child already exists#7567
Merged
steve-aom-elliott merged 1 commit intomainfrom May 4, 2026
Merged
Conversation
8727772 to
52a80c5
Compare
52a80c5 to
40c8f2c
Compare
…exists Make `AddToTagVisitor` idempotent by default: if `scope` already contains a child where `SemanticallyEqual.areEqual(existingChild, tagToAdd)` holds, return without mutating the tree. `SemanticallyEqual` ignores comments and attribute order, so this catches semantic duplicates that only differ in whitespace, attribute ordering, or interleaved comments. Preserves reference identity on no-op runs so that downstream "did this recipe change anything" checks (dirty-marker tracking, conditional `doAfterVisit` chains, recipe-cycle convergence) remain accurate. An `allowDuplicates` opt-out is provided for the rare caller that wants the prior unconditional-append behavior. Two new constructor overloads accept the flag without forcing callers to pass `null` for `tagComparator`: - `(Xml.Tag, Xml.Tag, boolean)` - `(Xml.Tag, Xml.Tag, @nullable Comparator<Content>, boolean)` An audit of every in-tree caller (~22 sites in `rewrite-maven`, `rewrite-csharp`, and `rewrite-xml`) confirmed none want duplicate sibling tags — Maven POM, csproj, and `AddOrUpdateChild` (which already gates externally) all rely on the schemas forbidding duplicates or on caller-side checks that have been redundant until now.
40c8f2c to
7a9d305
Compare
Jenson3210
approved these changes
May 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Make
AddToTagVisitoridempotent by default: if the targetscopealready contains a child for whichSemanticallyEqual.areEqual(existingChild, tagToAdd)holds, return without mutating the tree.SemanticallyEqualignores comments and attribute order, so this catches semantic duplicates that only differ in whitespace, attribute ordering, or interleaved comments.An
allowDuplicatesopt-out flag is provided for the rare caller that wants the prior unconditional-append behavior. Two new constructor overloads accept the flag without forcing callers to passnullfortagComparatorjust to opt out:(Xml.Tag, Xml.Tag, boolean)(Xml.Tag, Xml.Tag, @Nullable Comparator<Content>, boolean)Why default-on
I audited every in-tree call site of
AddToTagVisitor(~22, acrossrewrite-maven,rewrite-csharp, and the staticaddToTag(...)helpers used byrewrite-xml'sAddOrUpdateChildandrewrite-maven'sAddPluginDependency). None want duplicate sibling tags:<dependency>,<plugin>,<property>, etc.AddNuGetPackageReferenceVisitor): csproj schema forbids duplicate<PackageReference>.AddOrUpdateChild: already gates externally by checking for an existing child before delegating toaddToTag, so the new check is redundant rather than behavior-changing for it.Centralizing the check here removes existing per-site duplicate prevention and prevents the semantically-equal-but-different-identity output that has tripped up downstream tree-identity checks.
Why default-on with an opt-out
Raw XML technically allows duplicate sibling tags. Although no in-tree caller wants them, an unknown third-party consumer of
rewrite-xmlcould in principle.allowDuplicates=truekeeps that path available without forcing every in-tree consumer to flip a flag.Behavior change
Default-off-then-on. Anyone passing a
tagToAddthat isSemanticallyEqualto a child already present inscopewill now see a no-op instead of a duplicate insert. A literal "I want a duplicate" use case isn't expected for in-tree callers (XML duplicates of<dependency>/<plugin>/<property>etc. are usually bugs); flagging here so reviewers can call out anything that needs adjustment.Test plan
doesNotAddSemanticallyEqualDuplicatetest exercises the basic skip pathdoesNotAddDuplicateIgnoringAttributeOrdertest exercises attribute-order toleranceaddsWhenChildrenShareNameButDifferentAttributestest confirms additions still happen when children only share a tag nameAddToTagTestcases still passAddToTagVisitor(Add{Dependency,Property,Repository,ManagedDependency,Profile,Plugin}, ExcludeDependency) all pass