Skip to content

Fix findManifestTargetCall to only search top-level targets array#3374

Open
krishnapermi wants to merge 2 commits into
swiftlang:mainfrom
krishnapermi:fix-manifest-target-dependency-search
Open

Fix findManifestTargetCall to only search top-level targets array#3374
krishnapermi wants to merge 2 commits into
swiftlang:mainfrom
krishnapermi:fix-manifest-target-dependency-search

Conversation

@krishnapermi

Copy link
Copy Markdown

When swift package add-target-dependency is used on a target whose name also appears as a dependency of another target, the refactoring incorrectly modifies the dependency reference instead of the actual target definition — producing a malformed Package.swift.

Root cause: findManifestTargetCall used FirstNodeFinder which walks the entire syntax tree recursively. Because a target's name string (e.g. .target(name: "TargetOne")) can appear both as a top-level target definition and nested inside another target's dependencies: array, the recursive search finds the nested occurrence first and returns the wrong node.

Fix: Replace the recursive findFirst search with a linear scan over the top-level elements of the targets: array, so only target definitions are considered — not dependency references nested inside them.

A regression test is added that covers the exact scenario from the linked bug report.

Fixes swiftlang/swift-package-manager#10122

Refactor target call search to only check top-level elements in the targets array, preventing incorrect matches from nested dependencies.
Added a regression test to ensure that modifying a target's name in dependencies updates the actual target definition correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

swift package add-target-dependency adds incorrectly and create malformed Package.swift

1 participant