[SwiftRefactor] Fix add-target-dependency matching nested target references - #3386
[SwiftRefactor] Fix add-target-dependency matching nested target references#3386mit112 wants to merge 1 commit into
Conversation
…rences `findManifestTargetCall` searched the `targets` array with a recursive `findFirst`, so it could match a `.target(name:)` reference nested inside another target's `dependencies` array before reaching the actual target definition. Adding a dependency then mutated the reference and produced a manifest that no longer compiles. Match only the top-level elements of the `targets` array so the target definition is always the one that gets modified. Fixes swiftlang/swift-package-manager#10122
|
Gentle ping on this one — it's been open about three weeks with no review and no CI run yet. For context, this fixes swiftlang/swift-package-manager#10122: @ahoppen @bnbarham — you've both reviewed recent |
|
Ben and I are no longer code owners of swift-syntax. @rintaro @hamishknight would be the points of contact to review this PR. |
|
Thanks @ahoppen, appreciate the redirect. @rintaro @hamishknight — would one of you be able to take a look at this, or kick off CI? Short version: One thing worth surfacing: #3374 by @krishnapermi predates this and fixes the same root cause in the same two files — I only spotted it after opening this. No preference from me on which one you take; happy to close this in favour of theirs if that's simpler. Just flagging it so you don't review the same bug twice. |
Summary
swift package add-target-dependencycan produce aPackage.swiftthat no longer compiles when the target being modified is also referenced as a dependency of another target. The root cause is inSwiftRefactor, so fixing it here.Reported as swiftlang/swift-package-manager#10122.
Details
findManifestTargetCall(targetName:)locates the target to modify with a recursiveFunctionCallExprSyntax.findFirst(in: targetArray, ...). Because that walks the whole subtree, given a manifest like:it matches the nested
.target(name: "TargetOne")reference before the actual top-level definition, then appends adependencies:argument to that reference, yielding malformed output such as:which fails to compile (
extra argument 'dependencies' in call).Fix
Match only the top-level elements of the
targetsarray rather than recursively descending into nested calls, so the target definition is always the node that gets modified.Test
Added
testAddTargetDependencyToTargetReferencedByAnotherTargettoManifestEditTests, which reproduces the case above. It fails before the change and passes after; the fullManifestEditTestssuite (19 tests) passes.