Skip to content

Commit bdcf9a1

Browse files
authored
[6.0] plugins: Pass correct dependency origin information to plugins (#7511)
Cherry pick of #7506 **Explanation**: SwiftPM sends information about the origin of each package dependency to the plugin process, but the `PluginContext` passed to the plugin function incorrectly shows all origins as `root`. **Scope**: Isolated to `PackagePlugin/PluginContextDeserializer.swift` **Risk**: Low, isolated to plugins. **Testing**: Existing end-to-end automated test was modified to cover the fix. **Issue**: rdar://127104161 **Reviewer**: @MaxDesiatov ### Modifications: The necessary information is included correctly in the serialized structure sent to the plugin by SwiftPM. The startup wrapper code in the plugin process correctly deserializes it but does not copy it into the final `PluginContext` struct which is passed to the plugin function; instead, `origin` is hard-coded to `.root`. This change copies the dependency information into the final context struct. ### Result: Plugins receive correct information about package dependencies - `local`, with a local path, or `repository` with a URL, version number and revision hash.
1 parent 81d9d6f commit bdcf9a1

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Sources/PackagePlugin/PluginContextDeserializer.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,22 @@ internal struct PluginContextDeserializer {
251251
}
252252
let products = try wirePackage.productIds.map { try self.product(for: $0) }
253253
let targets = try wirePackage.targetIds.map { try self.target(for: $0) }
254+
let origin : PackageOrigin = switch wirePackage.origin {
255+
case .root:
256+
.root
257+
case .local(let pathId):
258+
try .local(path: url(for: pathId).path)
259+
case .repository(let url, let displayVersion, let scmRevision):
260+
.repository(url: url, displayVersion: displayVersion, scmRevision: scmRevision)
261+
case .registry(let identity, let displayVersion):
262+
.registry(identity: identity, displayVersion: displayVersion)
263+
}
254264
let package = Package(
255265
id: wirePackage.identity,
256266
displayName: wirePackage.displayName,
257267
directory: Path(url: directory),
258268
directoryURL: directory,
259-
origin: .root,
269+
origin: origin,
260270
toolsVersion: toolsVersion,
261271
dependencies: dependencies,
262272
products: products,

Tests/CommandsTests/PackageCommandTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,6 +1879,11 @@ final class PackageCommandTests: CommandsTestCase {
18791879
print(" \\(file.path): \\(file.type)")
18801880
}
18811881
}
1882+
1883+
// Print out the dependencies so that we can check them.
1884+
for dependency in context.package.dependencies {
1885+
print(" dependency \\(dependency.package.displayName): \\(dependency.package.origin)")
1886+
}
18821887
}
18831888
}
18841889
"""
@@ -1958,6 +1963,12 @@ final class PackageCommandTests: CommandsTestCase {
19581963
let (stdout, _) = try SwiftPM.Package.execute(["mycmd"], packagePath: packageDir)
19591964
XCTAssertMatch(stdout, .contains("Initial working directory: \(workingDirectory)"))
19601965
}
1966+
1967+
// Check that information about the dependencies was properly sent to the plugin.
1968+
do {
1969+
let (stdout, _) = try SwiftPM.Package.execute(["mycmd", "--target", "MyLibrary"], packagePath: packageDir)
1970+
XCTAssertMatch(stdout, .contains("dependency HelperPackage: local"))
1971+
}
19611972
}
19621973
}
19631974

0 commit comments

Comments
 (0)