Skip to content

Commit 3ab568e

Browse files
authored
Handle macOS CLT --show-sdk-platform-path issues (#6977)
We have been seeing this issue for a while when using SwiftPM from the CommandLineTools package: ``` ❯ /usr/bin/xcrun --sdk macosx --show-sdk-platform-path xcrun: error: unable to lookup item 'PlatformPath' from command line tools installation xcrun: error: unable to lookup item 'PlatformPath' in SDK '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk' ``` This changes SwiftPM to handle this gracefully during builds (a warning gets emitted) and to fail properly when running `swift test`. rdar://107479428
1 parent 3251a55 commit 3ab568e

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

Sources/Commands/SwiftTestTool.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ public struct SwiftTestTool: SwiftCommand {
195195

196196
// validate XCTest available on darwin based systems
197197
let toolchain = try swiftTool.getTargetToolchain()
198-
if toolchain.targetTriple.isDarwin() && toolchain.xctestPath == nil {
198+
let isHostTestingAvailable = try swiftTool.getHostToolchain().swiftSDK.supportsTesting
199+
if (toolchain.targetTriple.isDarwin() && toolchain.xctestPath == nil) || !isHostTestingAvailable {
199200
throw TestError.xctestNotAvailable
200201
}
201202
} catch {

Sources/CoreCommands/SwiftTool.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,9 @@ public final class SwiftTool {
797797
private lazy var _hostToolchain: Result<UserToolchain, Swift.Error> = {
798798
return Result(catching: {
799799
try UserToolchain(swiftSDK: SwiftSDK.hostSwiftSDK(
800-
originalWorkingDirectory: self.originalWorkingDirectory))
800+
originalWorkingDirectory: self.originalWorkingDirectory,
801+
observabilityScope: self.observabilityScope
802+
))
801803
})
802804
}()
803805

Sources/PackageModel/SwiftSDK.swift

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ public struct SwiftSDK: Equatable {
138138
/// The architectures to build for. We build for host architecture if this is empty.
139139
public var architectures: [String]? = nil
140140

141+
/// Whether or not the receiver supports testing.
142+
public let supportsTesting: Bool
143+
141144
/// Root directory path of the SDK used to compile for the target triple.
142145
@available(*, deprecated, message: "use `pathsConfiguration.sdkRootPath` instead")
143146
public var sdk: AbsolutePath? {
@@ -411,12 +414,14 @@ public struct SwiftSDK: Equatable {
411414
hostTriple: Triple? = nil,
412415
targetTriple: Triple? = nil,
413416
toolset: Toolset,
414-
pathsConfiguration: PathsConfiguration
417+
pathsConfiguration: PathsConfiguration,
418+
supportsTesting: Bool = true
415419
) {
416420
self.hostTriple = hostTriple
417421
self.targetTriple = targetTriple
418422
self.toolset = toolset
419423
self.pathsConfiguration = pathsConfiguration
424+
self.supportsTesting = supportsTesting
420425
}
421426

422427
/// Returns the bin directory for the host.
@@ -447,7 +452,8 @@ public struct SwiftSDK: Equatable {
447452
public static func hostSwiftSDK(
448453
_ binDir: AbsolutePath? = nil,
449454
originalWorkingDirectory: AbsolutePath? = nil,
450-
environment: [String: String] = ProcessEnv.vars
455+
environment: [String: String] = ProcessEnv.vars,
456+
observabilityScope: ObservabilityScope? = nil
451457
) throws -> SwiftSDK {
452458
let originalWorkingDirectory = originalWorkingDirectory ?? localFileSystem.currentWorkingDirectory
453459
// Select the correct binDir.
@@ -482,14 +488,23 @@ public struct SwiftSDK: Equatable {
482488
#endif
483489

484490
// Compute common arguments for clang and swift.
491+
let supportsTesting: Bool
485492
var extraCCFlags: [String] = []
486493
var extraSwiftCFlags: [String] = []
487494
#if os(macOS)
488-
let sdkPaths = try SwiftSDK.sdkPlatformFrameworkPaths(environment: environment)
489-
extraCCFlags += ["-F", sdkPaths.fwk.pathString]
490-
extraSwiftCFlags += ["-F", sdkPaths.fwk.pathString]
491-
extraSwiftCFlags += ["-I", sdkPaths.lib.pathString]
492-
extraSwiftCFlags += ["-L", sdkPaths.lib.pathString]
495+
do {
496+
let sdkPaths = try SwiftSDK.sdkPlatformFrameworkPaths(environment: environment)
497+
extraCCFlags += ["-F", sdkPaths.fwk.pathString]
498+
extraSwiftCFlags += ["-F", sdkPaths.fwk.pathString]
499+
extraSwiftCFlags += ["-I", sdkPaths.lib.pathString]
500+
extraSwiftCFlags += ["-L", sdkPaths.lib.pathString]
501+
supportsTesting = true
502+
} catch {
503+
supportsTesting = false
504+
observabilityScope?.emit(warning: "could not determine XCTest paths: \(error)")
505+
}
506+
#else
507+
supportsTesting = true
493508
#endif
494509

495510
#if !os(Windows)
@@ -504,7 +519,8 @@ public struct SwiftSDK: Equatable {
504519
],
505520
rootPaths: [binDir]
506521
),
507-
pathsConfiguration: .init(sdkRootPath: sdkPath)
522+
pathsConfiguration: .init(sdkRootPath: sdkPath),
523+
supportsTesting: supportsTesting
508524
)
509525
}
510526

0 commit comments

Comments
 (0)