Skip to content

Commit ee37cd3

Browse files
committed
Handle macOS CLT --show-sdk-platform-path issues
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 63e1787 commit ee37cd3

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

Sources/Commands/SwiftTestTool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ 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+
if try (toolchain.targetTriple.isDarwin() && toolchain.xctestPath == nil) || !swiftTool.getHostToolchain().swiftSDK.supportsTesting {
199199
throw TestError.xctestNotAvailable
200200
}
201201
} catch {

Sources/CoreCommands/SwiftTool.swift

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

Sources/PackageModel/SwiftSDK.swift

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ 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+
public let supportsTesting: Bool
142+
141143
/// Root directory path of the SDK used to compile for the target triple.
142144
@available(*, deprecated, message: "use `pathsConfiguration.sdkRootPath` instead")
143145
public var sdk: AbsolutePath? {
@@ -411,12 +413,14 @@ public struct SwiftSDK: Equatable {
411413
hostTriple: Triple? = nil,
412414
targetTriple: Triple? = nil,
413415
toolset: Toolset,
414-
pathsConfiguration: PathsConfiguration
416+
pathsConfiguration: PathsConfiguration,
417+
supportsTesting: Bool = true
415418
) {
416419
self.hostTriple = hostTriple
417420
self.targetTriple = targetTriple
418421
self.toolset = toolset
419422
self.pathsConfiguration = pathsConfiguration
423+
self.supportsTesting = supportsTesting
420424
}
421425

422426
/// Returns the bin directory for the host.
@@ -447,7 +451,8 @@ public struct SwiftSDK: Equatable {
447451
public static func hostSwiftSDK(
448452
_ binDir: AbsolutePath? = nil,
449453
originalWorkingDirectory: AbsolutePath? = nil,
450-
environment: [String: String] = ProcessEnv.vars
454+
environment: [String: String] = ProcessEnv.vars,
455+
observabilityScope: ObservabilityScope? = nil
451456
) throws -> SwiftSDK {
452457
let originalWorkingDirectory = originalWorkingDirectory ?? localFileSystem.currentWorkingDirectory
453458
// Select the correct binDir.
@@ -482,14 +487,23 @@ public struct SwiftSDK: Equatable {
482487
#endif
483488

484489
// Compute common arguments for clang and swift.
490+
let supportsTesting: Bool
485491
var extraCCFlags: [String] = []
486492
var extraSwiftCFlags: [String] = []
487493
#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]
494+
do {
495+
let sdkPaths = try SwiftSDK.sdkPlatformFrameworkPaths(environment: environment)
496+
extraCCFlags += ["-F", sdkPaths.fwk.pathString]
497+
extraSwiftCFlags += ["-F", sdkPaths.fwk.pathString]
498+
extraSwiftCFlags += ["-I", sdkPaths.lib.pathString]
499+
extraSwiftCFlags += ["-L", sdkPaths.lib.pathString]
500+
supportsTesting = true
501+
} catch {
502+
supportsTesting = false
503+
observabilityScope?.emit(warning: "could not determine XCTest paths: \(error)")
504+
}
505+
#else
506+
supportsTesting = true
493507
#endif
494508

495509
#if !os(Windows)
@@ -504,7 +518,8 @@ public struct SwiftSDK: Equatable {
504518
],
505519
rootPaths: [binDir]
506520
),
507-
pathsConfiguration: .init(sdkRootPath: sdkPath)
521+
pathsConfiguration: .init(sdkRootPath: sdkPath),
522+
supportsTesting: supportsTesting
508523
)
509524
}
510525

0 commit comments

Comments
 (0)