Skip to content

Commit 54097aa

Browse files
committed
Add an opt-out from explicit target dependency import checking.
`--disable-explicit-target-dependency-import-checking` prevents the verification code from being run as a just-in-case option to opt-out from this warning
1 parent 3728b41 commit 54097aa

File tree

6 files changed

+34
-2
lines changed

6 files changed

+34
-2
lines changed

Sources/Build/BuildOperation.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
123123
// Emit a warning if a target imports another target in this build
124124
// without specifying it as a dependency in the manifest
125125
private func verifyTargetImports(in description: BuildDescription) throws {
126+
guard !description.disableExplicitTargetDependencyImportChecking else {
127+
return
128+
}
126129
// Ensure the compiler supports the import-scan operation
127130
guard SwiftTargetBuildDescription.checkSupportedFrontendFlags(flags: ["import-prescan"], fs: localFileSystem) else {
128131
return

Sources/Build/BuildOperationBuildSystemDelegateHandler.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ public struct BuildDescription: Codable {
205205
/// The map of copy commands.
206206
let copyCommands: [BuildManifest.CmdName: LLBuildManifest.CopyTool]
207207

208+
/// A flag that inidcates this build should skip checking whether targets only import
209+
/// their explicitly-declared dependencies
210+
let disableExplicitTargetDependencyImportChecking: Bool
211+
208212
/// Every target's set of dependencies.
209213
let targetDependencyMap: [TargetName: [TargetName]]
210214

@@ -228,6 +232,7 @@ public struct BuildDescription: Codable {
228232
self.swiftFrontendCommands = swiftFrontendCommands
229233
self.testDiscoveryCommands = testDiscoveryCommands
230234
self.copyCommands = copyCommands
235+
self.disableExplicitTargetDependencyImportChecking = plan.buildParameters.disableExplicitTargetDependencyImportChecking
231236
self.targetDependencyMap = try plan.targets.reduce(into: [TargetName: [TargetName]]()) {
232237
let deps = try $1.target.recursiveTargetDependencies().map { $0.c99name }
233238
$0[$1.target.c99name] = deps

Sources/Commands/Options.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,11 @@ public struct SwiftToolOptions: ParsableArguments {
299299
@Flag()
300300
var useIntegratedSwiftDriver: Bool = false
301301

302+
/// A flag that inidcates this build should skip checking whether targets only import
303+
/// their explicitly-declared dependencies
304+
@Flag()
305+
var disableExplicitTargetDependencyImportChecking: Bool = false
306+
302307
/// Whether to use the explicit module build flow (with the integrated driver)
303308
@Flag(name: .customLong("experimental-explicit-module-build"))
304309
var useExplicitModuleBuild: Bool = false

Sources/Commands/SwiftTool.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,8 @@ public class SwiftTool {
789789
useExplicitModuleBuild: options.useExplicitModuleBuild,
790790
isXcodeBuildSystemEnabled: options.buildSystem == .xcode,
791791
printManifestGraphviz: options.printManifestGraphviz,
792-
forceTestDiscovery: options.enableTestDiscovery // backwards compatibility, remove with --enable-test-discovery
792+
forceTestDiscovery: options.enableTestDiscovery, // backwards compatibility, remove with --enable-test-discovery
793+
disableExplicitTargetDependencyImportChecking: options.disableExplicitTargetDependencyImportChecking
793794
)
794795
})
795796
}()

Sources/SPMBuildCore/BuildParameters.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ public struct BuildParameters: Encodable {
127127
/// Whether to use the explicit module build flow (with the integrated driver)
128128
public var useExplicitModuleBuild: Bool
129129

130+
/// A flag that inidcates this build should skip checking whether targets only import
131+
/// their explicitly-declared dependencies
132+
public var disableExplicitTargetDependencyImportChecking: Bool
133+
130134
/// Whether to output a graphviz file visualization of the combined job graph for all targets
131135
public var printManifestGraphviz: Bool
132136

@@ -191,7 +195,8 @@ public struct BuildParameters: Encodable {
191195
isXcodeBuildSystemEnabled: Bool = false,
192196
printManifestGraphviz: Bool = false,
193197
enableTestability: Bool? = nil,
194-
forceTestDiscovery: Bool = false
198+
forceTestDiscovery: Bool = false,
199+
disableExplicitTargetDependencyImportChecking: Bool = false
195200
) {
196201
let triple = destinationTriple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompiler)
197202

@@ -221,6 +226,7 @@ public struct BuildParameters: Encodable {
221226
self.enableTestability = enableTestability ?? (.debug == configuration)
222227
// decide if to enable the use of test manifests based on platform. this is likely to change in the future
223228
self.testDiscoveryStrategy = triple.isDarwin() ? .objectiveC : .manifest(generate: forceTestDiscovery)
229+
self.disableExplicitTargetDependencyImportChecking = disableExplicitTargetDependencyImportChecking
224230
}
225231

226232
/// The path to the build directory (inside the data directory).

Tests/CommandsTests/BuildToolTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ final class BuildToolTests: XCTestCase {
8686
XCTAssertTrue(stderr.contains("warning: Target A imports another target (B) in the package without declaring it a dependency."))
8787
}
8888
}
89+
90+
// Verify that the disable toggle works as expected
91+
fixture(name: "Miscellaneous/ImportOfMissingDependency") { path in
92+
let fullPath = resolveSymlinks(path)
93+
XCTAssertThrowsError(try build(["--disable-explicit-target-dependency-import-checking"], packagePath: fullPath)) { error in
94+
guard case SwiftPMProductError.executionFailure(_, _, let stderr) = error else {
95+
XCTFail()
96+
return
97+
}
98+
XCTAssertFalse(stderr.contains("warning: Target A imports another target (B) in the package without declaring it a dependency."))
99+
}
100+
}
89101
}
90102

91103
func testBinPathAndSymlink() throws {

0 commit comments

Comments
 (0)