Skip to content

Enable contrived test plugin test case #8756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 4, 2025
Merged
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
8 changes: 6 additions & 2 deletions Sources/SwiftBuildSupport/PIFBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public final class PIFBuilder {
var packagesAndProjects: [(ResolvedPackage, ProjectModel.Project)] = []

for package in sortedPackages {
var buildToolPluginResultsByTargetName: [String: PackagePIFBuilder.BuildToolPluginInvocationResult] = [:]
var buildToolPluginResultsByTargetName: [String: [PackagePIFBuilder.BuildToolPluginInvocationResult]] = [:]

for module in package.modules {
// Apply each build tool plugin used by the target in order,
Expand Down Expand Up @@ -388,7 +388,11 @@ public final class PIFBuilder {

// Add a BuildToolPluginInvocationResult to the mapping.
buildToolPluginResults.append(result2)
buildToolPluginResultsByTargetName[module.name] = result2
if var existingResults = buildToolPluginResultsByTargetName[module.name] {
existingResults.append(result2)
} else {
buildToolPluginResultsByTargetName[module.name] = [result2]
}
}
}

Expand Down
26 changes: 24 additions & 2 deletions Sources/SwiftBuildSupport/PackagePIFBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public final class PackagePIFBuilder {
}

/// Records the results of applying build tool plugins to modules in the package.
let buildToolPluginResultsByTargetName: [String: PackagePIFBuilder.BuildToolPluginInvocationResult]
let buildToolPluginResultsByTargetName: [String: [PackagePIFBuilder.BuildToolPluginInvocationResult]]

/// Whether to create dynamic libraries for dynamic products.
///
Expand Down Expand Up @@ -192,7 +192,7 @@ public final class PackagePIFBuilder {
resolvedPackage: ResolvedPackage,
packageManifest: PackageModel.Manifest,
delegate: PackagePIFBuilder.BuildDelegate,
buildToolPluginResultsByTargetName: [String: BuildToolPluginInvocationResult],
buildToolPluginResultsByTargetName: [String: [BuildToolPluginInvocationResult]],
createDylibForDynamicProducts: Bool = false,
packageDisplayVersion: String?,
fileSystem: FileSystem,
Expand All @@ -209,6 +209,28 @@ public final class PackagePIFBuilder {
self.observabilityScope = observabilityScope
}

public init(
modulesGraph: ModulesGraph,
resolvedPackage: ResolvedPackage,
packageManifest: PackageModel.Manifest,
delegate: PackagePIFBuilder.BuildDelegate,
buildToolPluginResultsByTargetName: [String: BuildToolPluginInvocationResult],
createDylibForDynamicProducts: Bool = false,
packageDisplayVersion: String?,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope
) {
self.package = resolvedPackage
self.packageManifest = packageManifest
self.modulesGraph = modulesGraph
self.delegate = delegate
self.buildToolPluginResultsByTargetName = buildToolPluginResultsByTargetName.mapValues { [$0] }
self.createDylibForDynamicProducts = createDylibForDynamicProducts
self.packageDisplayVersion = packageDisplayVersion
self.fileSystem = fileSystem
self.observabilityScope = observabilityScope
}

/// Build an empty PIF project.
public func buildEmptyPIF() {
self._pifProject = PackagePIFBuilder.buildEmptyPIF(package: self.package.underlying)
Expand Down
54 changes: 32 additions & 22 deletions Sources/SwiftBuildSupport/PackagePIFProjectBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -380,28 +380,36 @@ struct PackagePIFProjectBuilder {
targetKeyPath: WritableKeyPath<ProjectModel.Project, ProjectModel.Target>,
addBuildToolPluginCommands: Bool
) -> (sourceFilePaths: [AbsolutePath], resourceFilePaths: [String]) {
guard let pluginResult = pifBuilder.buildToolPluginResultsByTargetName[module.name] else {
guard let pluginResults = pifBuilder.buildToolPluginResultsByTargetName[module.name] else {
// We found no results for the target.
return (sourceFilePaths: [], resourceFilePaths: [])
}

// Process the results of applying any build tool plugins on the target.
// If we've been asked to add build tool commands for the result, we do so now.
if addBuildToolPluginCommands {
for command in pluginResult.buildCommands {
self.addBuildToolCommand(command, to: targetKeyPath)
var sourceFilePaths: [AbsolutePath] = []
var resourceFilePaths: [AbsolutePath] = []

for pluginResult in pluginResults {
// Process the results of applying any build tool plugins on the target.
// If we've been asked to add build tool commands for the result, we do so now.
if addBuildToolPluginCommands {
for command in pluginResult.buildCommands {
self.addBuildToolCommand(command, to: targetKeyPath)
}
}
}

// Process all the paths of derived output paths using the same rules as for source.
let result = self.process(
pluginGeneratedFilePaths: pluginResult.allDerivedOutputPaths,
forModule: module,
toolsVersion: self.package.manifest.toolsVersion
)
// Process all the paths of derived output paths using the same rules as for source.
let result = self.process(
pluginGeneratedFilePaths: pluginResult.allDerivedOutputPaths,
forModule: module,
toolsVersion: self.package.manifest.toolsVersion
)

sourceFilePaths.append(contentsOf: result.sourceFilePaths)
resourceFilePaths.append(contentsOf: result.resourceFilePaths.map(\.path))
}
return (
sourceFilePaths: result.sourceFilePaths,
resourceFilePaths: result.resourceFilePaths.map(\.path.pathString)
sourceFilePaths: sourceFilePaths,
resourceFilePaths: resourceFilePaths.map(\.pathString)
)
}

Expand All @@ -414,17 +422,19 @@ struct PackagePIFProjectBuilder {
sourceFilePaths: [AbsolutePath],
resourceFilePaths: [String]
) {
guard let pluginResult = pifBuilder.buildToolPluginResultsByTargetName[module.name] else {
guard let pluginResults = pifBuilder.buildToolPluginResultsByTargetName[module.name] else {
return
}

for command in pluginResult.buildCommands {
let producesResources = Set(command.outputPaths).intersection(resourceFilePaths).hasContent
for pluginResult in pluginResults {
for command in pluginResult.buildCommands {
let producesResources = Set(command.outputPaths).intersection(resourceFilePaths).hasContent

if producesResources {
self.addBuildToolCommand(command, to: resourceBundleTargetKeyPath)
} else {
self.addBuildToolCommand(command, to: sourceModuleTargetKeyPath)
if producesResources {
self.addBuildToolCommand(command, to: resourceBundleTargetKeyPath)
} else {
self.addBuildToolCommand(command, to: sourceModuleTargetKeyPath)
}
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions Tests/FunctionalTests/PluginTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,12 @@ final class PluginTests: XCTestCase {
XCTAssert(stdout.contains("Build of product 'MyLocalTool' complete!"), "stdout:\n\(stdout)")
}

/*
FIXME: Determine the cause of the compile error.
#if os(macOS) // See https://github.com/swiftlang/swift-package-manager/issues/8416 for errors running build tools on Linux
try await fixture(name: "Miscellaneous/Plugins") { fixturePath in
let (stdout, _) = try await executeSwiftBuild(fixturePath.appending("ContrivedTestPlugin"), configuration: .Debug, extraArgs: ["--build-system", "swiftbuild", "--product", "MyLocalTool", "--disable-sandbox"])
XCTAssert(stdout.contains("Build complete!"), "stdout:\n\(stdout)")
}
#endif
*/
}

func testPluginScriptSandbox() async throws {
Expand Down Expand Up @@ -1335,5 +1332,12 @@ FIXME: Determine the cause of the compile error.
let (stdout, _) = try await executeSwiftBuild(fixturePath)
XCTAssert(stdout.contains("Build complete!"), "stdout:\n\(stdout)")
}

#if os(macOS) // See https://github.com/swiftlang/swift-package-manager/issues/8416 for errors running build tools on Linux
try await fixture(name: "Miscellaneous/Plugins/DependentPlugins") { fixturePath in
let (stdout, _) = try await executeSwiftBuild(fixturePath, extraArgs: ["--build-system", "swiftbuild"])
XCTAssert(stdout.contains("Build complete!"), "stdout:\n\(stdout)")
}
#endif
}
}