Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Sources/SwiftRefactor/PackageManifest/SyntaxEditUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,19 @@ extension FunctionCallExprSyntax {
return literalValue == targetName
}

guard let targetCall = FunctionCallExprSyntax.findFirst(in: targetArray, matching: matchesTargetCall) else {
// Only check the top-level elements of the targets array. A recursive
// search would incorrectly match a target name that appears inside
// another target's `dependencies:` array — e.g. `.target(name: "Foo")`
// listed as a dependency has the same `name:` argument as the actual
// `.target(name: "Foo")` definition at the top level.
guard let targetCall = targetArray.elements.lazy.compactMap({ element -> FunctionCallExprSyntax? in
guard let call = element.expression.as(FunctionCallExprSyntax.self),
matchesTargetCall(call: call)
else {
return nil
}
return call
}).first else {
throw ManifestEditError.cannotFindTarget(targetName: targetName)
}

Expand Down
51 changes: 51 additions & 0 deletions Tests/SwiftRefactorTest/ManifestEditTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,57 @@ final class ManifestEditTests: XCTestCase {
)
}

func testAddTargetDependencyWhenTargetIsAlsoDependency() throws {
// Regression test: when a target's name appears inside another target's
// dependencies array, the refactoring must modify the actual target
// definition, not the nested dependency reference.
// See https://github.com/swiftlang/swift-package-manager/issues/10122
try assertManifestRefactor(
"""
// swift-tools-version: 5.5
let package = Package(
name: "packages",
targets: [
.target(
name: "Library",
dependencies: [
.target(name: "Helper"),
]
),
.target(
name: "Helper"
),
]
)
""",
expectedManifest: """
// swift-tools-version: 5.5
let package = Package(
name: "packages",
targets: [
.target(
name: "Library",
dependencies: [
.target(name: "Helper"),
]
),
.target(
name: "Helper",
dependencies: [
.target(name: "Support"),
]
),
]
)
""",
provider: AddTargetDependency.self,
context: .init(
dependency: .target(name: "Support"),
targetName: "Helper"
)
)
}

func testAddJava2SwiftPlugin() throws {
try assertManifestRefactor(
"""
Expand Down