Skip to content

Commit 732d540

Browse files
authored
[5.9] Handle macOS CLT --show-sdk-platform-path issues (#6991)
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 (cherry picked from commit 3ab568e)
1 parent 9c432d2 commit 732d540

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

Sources/Commands/SwiftTestTool.swift

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

167167
// validate XCTest available on darwin based systems
168168
let toolchain = try swiftTool.getDestinationToolchain()
169-
if toolchain.triple.isDarwin() && toolchain.xctestPath == nil {
169+
let isHostTestingAvailable = try swiftTool.getHostToolchain().destination.supportsTesting
170+
if toolchain.triple.isDarwin() && toolchain.xctestPath == nil || !isHostTestingAvailable {
170171
throw TestError.xctestNotAvailable
171172
}
172173
} catch {

Sources/CoreCommands/SwiftTool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ public final class SwiftTool {
792792
private lazy var _hostToolchain: Result<UserToolchain, Swift.Error> = {
793793
return Result(catching: {
794794
try UserToolchain(destination: Destination.hostDestination(
795-
originalWorkingDirectory: self.originalWorkingDirectory))
795+
originalWorkingDirectory: self.originalWorkingDirectory, observabilityScope: self.observabilityScope))
796796
})
797797
}()
798798

Sources/PackageModel/Destination.swift

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

135+
/// Whether or not the receiver supports testing.
136+
public let supportsTesting: Bool
137+
135138
/// Root directory path of the SDK used to compile for the destination.
136139
@available(*, deprecated, message: "use `sdkRootDir` instead")
137140
public var sdk: AbsolutePath? {
@@ -402,12 +405,14 @@ public struct Destination: Equatable {
402405
hostTriple: Triple? = nil,
403406
targetTriple: Triple? = nil,
404407
toolset: Toolset,
405-
pathsConfiguration: PathsConfiguration
408+
pathsConfiguration: PathsConfiguration,
409+
supportsTesting: Bool = true
406410
) {
407411
self.hostTriple = hostTriple
408412
self.targetTriple = targetTriple
409413
self.toolset = toolset
410414
self.pathsConfiguration = pathsConfiguration
415+
self.supportsTesting = supportsTesting
411416
}
412417

413418
/// Returns the bin directory for the host.
@@ -428,7 +433,8 @@ public struct Destination: Equatable {
428433
public static func hostDestination(
429434
_ binDir: AbsolutePath? = nil,
430435
originalWorkingDirectory: AbsolutePath? = nil,
431-
environment: [String: String] = ProcessEnv.vars
436+
environment: [String: String] = ProcessEnv.vars,
437+
observabilityScope: ObservabilityScope? = nil
432438
) throws -> Destination {
433439
let originalWorkingDirectory = originalWorkingDirectory ?? localFileSystem.currentWorkingDirectory
434440
// Select the correct binDir.
@@ -463,14 +469,23 @@ public struct Destination: Equatable {
463469
#endif
464470

465471
// Compute common arguments for clang and swift.
472+
let supportsTesting: Bool
466473
var extraCCFlags: [String] = []
467474
var extraSwiftCFlags: [String] = []
468475
#if os(macOS)
469-
let sdkPaths = try Destination.sdkPlatformFrameworkPaths(environment: environment)
470-
extraCCFlags += ["-F", sdkPaths.fwk.pathString]
471-
extraSwiftCFlags += ["-F", sdkPaths.fwk.pathString]
472-
extraSwiftCFlags += ["-I", sdkPaths.lib.pathString]
473-
extraSwiftCFlags += ["-L", sdkPaths.lib.pathString]
476+
do {
477+
let sdkPaths = try Destination.sdkPlatformFrameworkPaths(environment: environment)
478+
extraCCFlags += ["-F", sdkPaths.fwk.pathString]
479+
extraSwiftCFlags += ["-F", sdkPaths.fwk.pathString]
480+
extraSwiftCFlags += ["-I", sdkPaths.lib.pathString]
481+
extraSwiftCFlags += ["-L", sdkPaths.lib.pathString]
482+
supportsTesting = true
483+
} catch {
484+
supportsTesting = false
485+
observabilityScope?.emit(warning: "could not determine XCTest paths: \(error)")
486+
}
487+
#else
488+
supportsTesting = true
474489
#endif
475490

476491
#if !os(Windows)
@@ -485,7 +500,8 @@ public struct Destination: Equatable {
485500
],
486501
rootPaths: [binDir]
487502
),
488-
pathsConfiguration: .init(sdkRootPath: sdkPath)
503+
pathsConfiguration: .init(sdkRootPath: sdkPath),
504+
supportsTesting: supportsTesting
489505
)
490506
}
491507

0 commit comments

Comments
 (0)