Skip to content

Commit 85ff49c

Browse files
authored
Respect conditionals when finding/building executables used by plugins (#5813)
So far, we were ignoring conditionals here which could lead to looking for an executable that wasn't produced or trying to build one that was excluded.
1 parent d770b90 commit 85ff49c

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Sources/Commands/SwiftPackageTool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ extension SwiftPackageTool {
10721072

10731073
// Build or bring up-to-date any executable host-side tools on which this plugin depends. Add them and any binary dependencies to the tool-names-to-path map.
10741074
var toolNamesToPaths: [String: AbsolutePath] = [:]
1075-
for dep in plugin.dependencies {
1075+
for dep in plugin.dependencies(satisfying: try swiftTool.buildParameters().buildEnvironment) {
10761076
let buildOperation = try swiftTool.createBuildOperation(cacheBuildManifest: false)
10771077
switch dep {
10781078
case .product(let productRef, _):

Sources/SPMBuildCore/PluginInvocation.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ extension PackageGraph {
356356
for pluginTarget in pluginTargets {
357357
// Determine the tools to which this plugin has access, and create a name-to-path mapping from tool
358358
// names to the corresponding paths. Built tools are assumed to be in the build tools directory.
359-
let accessibleTools = pluginTarget.accessibleTools(fileSystem: fileSystem, for: pluginScriptRunner.hostTriple)
359+
let accessibleTools = pluginTarget.accessibleTools(fileSystem: fileSystem, environment: buildEnvironment, for: pluginScriptRunner.hostTriple)
360360
let toolNamesToPaths = accessibleTools.reduce(into: [String: AbsolutePath](), { dict, tool in
361361
switch tool {
362362
case .builtTool(let name, let path):
@@ -491,9 +491,13 @@ public enum PluginAccessibleTool: Hashable {
491491

492492
public extension PluginTarget {
493493

494+
func dependencies(satisfying environment: BuildEnvironment) -> [Dependency] {
495+
return self.dependencies.filter { $0.satisfies(environment) }
496+
}
497+
494498
/// The set of tools that are accessible to this plugin.
495-
func accessibleTools(fileSystem: FileSystem, for hostTriple: Triple) -> Set<PluginAccessibleTool> {
496-
return Set(self.dependencies.flatMap { dependency -> [PluginAccessibleTool] in
499+
func accessibleTools(fileSystem: FileSystem, environment: BuildEnvironment, for hostTriple: Triple) -> Set<PluginAccessibleTool> {
500+
return Set(self.dependencies(satisfying: environment).flatMap { dependency -> [PluginAccessibleTool] in
497501
switch dependency {
498502
case .target(let target, _):
499503
// For a binary target we create a `vendedTool`.
@@ -520,6 +524,19 @@ public extension PluginTarget {
520524
}
521525
}
522526

527+
fileprivate extension Target.Dependency {
528+
var conditions: [PackageConditionProtocol] {
529+
switch self {
530+
case .target(_, let conditions): return conditions
531+
case .product(_, let conditions): return conditions
532+
}
533+
}
534+
535+
func satisfies(_ environment: BuildEnvironment) -> Bool {
536+
conditions.allSatisfy { $0.satisfies(environment) }
537+
}
538+
}
539+
523540

524541
/// Represents the result of invoking a build tool plugin for a particular target. The result includes generated build commands and prebuild commands as well as any diagnostics and stdout/stderr output emitted by the plugin.
525542
public struct BuildToolPluginInvocationResult {

0 commit comments

Comments
 (0)