Skip to content

Commit b0b652c

Browse files
authored
plugins: Pass correct dependency origin information to plugins (#7506)
### Motivation: 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`. ### 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 ecf2e8f commit b0b652c

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
@@ -1890,6 +1890,11 @@ final class PackageCommandTests: CommandsTestCase {
18901890
print(" \\(file.path): \\(file.type)")
18911891
}
18921892
}
1893+
1894+
// Print out the dependencies so that we can check them.
1895+
for dependency in context.package.dependencies {
1896+
print(" dependency \\(dependency.package.displayName): \\(dependency.package.origin)")
1897+
}
18931898
}
18941899
}
18951900
"""
@@ -1986,6 +1991,12 @@ final class PackageCommandTests: CommandsTestCase {
19861991
let (stdout, _) = try SwiftPM.Package.execute(["mycmd"], packagePath: packageDir)
19871992
XCTAssertMatch(stdout, .contains("Initial working directory: \(workingDirectory)"))
19881993
}
1994+
1995+
// Check that information about the dependencies was properly sent to the plugin.
1996+
do {
1997+
let (stdout, _) = try SwiftPM.Package.execute(["mycmd", "--target", "MyLibrary"], packagePath: packageDir)
1998+
XCTAssertMatch(stdout, .contains("dependency HelperPackage: local"))
1999+
}
19892000
}
19902001
}
19912002

0 commit comments

Comments
 (0)