Skip to content

Commit 46bf794

Browse files
committed
Improve Destination.sdkPlatformFrameworkPaths()
We should not ignore errors or an empty SDK platform path since that means XCTest imports and test execution might silently fail on macOS with no indication to what the problem is.
1 parent 5700473 commit 46bf794

File tree

4 files changed

+28
-38
lines changed

4 files changed

+28
-38
lines changed

Sources/Commands/Utilities/TestingSupport.swift

+7-7
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ enum TestingSupport {
8282
),
8383
sanitizers: sanitizers
8484
)
85-
// Add the sdk platform path if we have it. If this is not present, we
86-
// might always end up failing.
87-
if let sdkPlatformFrameworksPath = try Destination.sdkPlatformFrameworkPaths() {
88-
// appending since we prefer the user setting (if set) to the one we inject
89-
env.appendPath("DYLD_FRAMEWORK_PATH", value: sdkPlatformFrameworksPath.fwk.pathString)
90-
env.appendPath("DYLD_LIBRARY_PATH", value: sdkPlatformFrameworksPath.lib.pathString)
91-
}
85+
86+
// Add the sdk platform path if we have it. If this is not present, we might always end up failing.
87+
let sdkPlatformFrameworksPath = try Destination.sdkPlatformFrameworkPaths()
88+
// appending since we prefer the user setting (if set) to the one we inject
89+
env.appendPath("DYLD_FRAMEWORK_PATH", value: sdkPlatformFrameworksPath.fwk.pathString)
90+
env.appendPath("DYLD_LIBRARY_PATH", value: sdkPlatformFrameworksPath.lib.pathString)
91+
9292
try TSCBasic.Process.checkNonZeroExit(arguments: args, environment: env)
9393
// Read the temporary file's content.
9494
return try swiftTool.fileSystem.readFileContents(tempFile.path)

Sources/PackageModel/Destination.swift

+20-18
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,11 @@ public struct Destination: Encodable, Equatable {
195195
var extraCCFlags: [String] = []
196196
var extraSwiftCFlags: [String] = []
197197
#if os(macOS)
198-
if let sdkPaths = try Destination.sdkPlatformFrameworkPaths(environment: environment) {
199-
extraCCFlags += ["-F", sdkPaths.fwk.pathString]
200-
extraSwiftCFlags += ["-F", sdkPaths.fwk.pathString]
201-
extraSwiftCFlags += ["-I", sdkPaths.lib.pathString]
202-
extraSwiftCFlags += ["-L", sdkPaths.lib.pathString]
203-
}
198+
let sdkPaths = try Destination.sdkPlatformFrameworkPaths(environment: environment)
199+
extraCCFlags += ["-F", sdkPaths.fwk.pathString]
200+
extraSwiftCFlags += ["-F", sdkPaths.fwk.pathString]
201+
extraSwiftCFlags += ["-I", sdkPaths.lib.pathString]
202+
extraSwiftCFlags += ["-L", sdkPaths.lib.pathString]
204203
#endif
205204

206205
#if !os(Windows)
@@ -217,26 +216,29 @@ public struct Destination: Encodable, Equatable {
217216
/// Returns macosx sdk platform framework path.
218217
public static func sdkPlatformFrameworkPaths(
219218
environment: EnvironmentVariables = .process()
220-
) throws -> (fwk: AbsolutePath, lib: AbsolutePath)? {
219+
) throws -> (fwk: AbsolutePath, lib: AbsolutePath) {
221220
if let path = _sdkPlatformFrameworkPath {
222221
return path
223222
}
224-
let platformPath = try? TSCBasic.Process.checkNonZeroExit(
223+
let platformPath = try TSCBasic.Process.checkNonZeroExit(
225224
arguments: ["/usr/bin/xcrun", "--sdk", "macosx", "--show-sdk-platform-path"],
226225
environment: environment).spm_chomp()
227226

228-
if let platformPath = platformPath, !platformPath.isEmpty {
229-
// For XCTest framework.
230-
let fwk = try AbsolutePath(validating: platformPath).appending(
231-
components: "Developer", "Library", "Frameworks")
227+
guard !platformPath.isEmpty else {
228+
throw StringError("could not determine SDK platform path")
229+
}
230+
231+
// For XCTest framework.
232+
let fwk = try AbsolutePath(validating: platformPath).appending(
233+
components: "Developer", "Library", "Frameworks")
232234

233-
// For XCTest Swift library.
234-
let lib = try AbsolutePath(validating: platformPath).appending(
235-
components: "Developer", "usr", "lib")
235+
// For XCTest Swift library.
236+
let lib = try AbsolutePath(validating: platformPath).appending(
237+
components: "Developer", "usr", "lib")
236238

237-
_sdkPlatformFrameworkPath = (fwk, lib)
238-
}
239-
return _sdkPlatformFrameworkPath
239+
let sdkPlatformFrameworkPath = (fwk, lib)
240+
_sdkPlatformFrameworkPath = sdkPlatformFrameworkPath
241+
return sdkPlatformFrameworkPath
240242
}
241243

242244
/// Cache storage for sdk platform path.

Sources/SPMTestSupport/Toolchain.swift

-12
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,6 @@ private func resolveBinDir() throws -> AbsolutePath {
3333
#endif
3434
}
3535

36-
extension UserToolchain {
37-
38-
#if os(macOS)
39-
public var sdkPlatformFrameworksPath: AbsolutePath {
40-
get throws {
41-
return try Destination.sdkPlatformFrameworkPaths()!.fwk
42-
}
43-
}
44-
#endif
45-
46-
}
47-
4836
extension Destination {
4937
public static var `default`: Self {
5038
get throws {

Tests/FunctionalTests/SwiftPMXCTestHelperTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class SwiftPMXCTestHelperTests: XCTestCase {
4949

5050
func XCTAssertXCTestHelper(_ bundlePath: AbsolutePath, testCases: NSDictionary) throws {
5151
#if os(macOS)
52-
let env = ["DYLD_FRAMEWORK_PATH": try UserToolchain.default.sdkPlatformFrameworksPath.pathString]
52+
let env = ["DYLD_FRAMEWORK_PATH": try Destination.sdkPlatformFrameworkPaths().fwk.pathString]
5353
let outputFile = bundlePath.parentDirectory.appending(component: "tests.txt")
5454
let _ = try SwiftPMProduct.XCTestHelper.execute([bundlePath.pathString, outputFile.pathString], env: env)
5555
guard let data = NSData(contentsOfFile: outputFile.pathString) else {

0 commit comments

Comments
 (0)