Skip to content

[6.2] Include search path for Swift Testing's macro plugin from toolchain if present #8706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 41 additions & 10 deletions Sources/PackageModel/UserToolchain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -636,26 +636,28 @@ public final class UserToolchain: Toolchain {
)

if triple.isMacOSX, let swiftTestingPath {
// swift-testing in CommandLineTools, needs extra frameworks search path
// Swift Testing is a framework (e.g. from CommandLineTools) so use -F.
if swiftTestingPath.extension == "framework" {
swiftCompilerFlags += ["-F", swiftTestingPath.pathString]
}

// Otherwise we must have a custom toolchain, add overrides to find its swift-testing ahead of any in the
// SDK. We expect the library to be in `lib/swift/macosx/testing` and the plugin in
// `lib/swift/host/plugins/testing`
if let pluginsPath = try? AbsolutePath(
validating: "../../host/plugins/testing",
relativeTo: swiftTestingPath
) {
// Otherwise Swift Testing is assumed to be a swiftmodule + library, so use -I and -L.
} else {
swiftCompilerFlags += [
"-I", swiftTestingPath.pathString,
"-L", swiftTestingPath.pathString,
"-plugin-path", pluginsPath.pathString,
]
}
}

// Specify the plugin path for Swift Testing's macro plugin if such a
// path exists in this toolchain.
if let swiftTestingPluginPath = Self.deriveSwiftTestingPluginPath(
derivedSwiftCompiler: swiftCompilers.compile,
fileSystem: fileSystem
) {
swiftCompilerFlags += ["-plugin-path", swiftTestingPluginPath.pathString]
}

swiftCompilerFlags += try Self.deriveSwiftCFlags(
triple: triple,
swiftSDK: swiftSDK,
Expand Down Expand Up @@ -1037,6 +1039,35 @@ public final class UserToolchain: Toolchain {
return nil
}

/// Derive the plugin path needed to locate the Swift Testing macro plugin,
/// if such a path exists in the toolchain of the specified compiler.
///
/// - Parameters:
/// - derivedSwiftCompiler: The derived path of the Swift compiler to use
/// when deriving the Swift Testing plugin path.
/// - fileSystem: The file system instance to use when validating the path
/// to return.
///
/// - Returns: A path to the directory containing Swift Testing's macro
/// plugin, or `nil` if the path does not exist or cannot be determined.
///
/// The path returned is a directory containing a library, suitable for
/// passing to a client compiler via the `-plugin-path` flag.
private static func deriveSwiftTestingPluginPath(
derivedSwiftCompiler: Basics.AbsolutePath,
fileSystem: any FileSystem
) -> AbsolutePath? {
guard let toolchainLibDir = try? toolchainLibDir(swiftCompilerPath: derivedSwiftCompiler) else {
return nil
}

if let pluginsPath = try? AbsolutePath(validating: "swift/host/plugins/testing", relativeTo: toolchainLibDir), fileSystem.exists(pluginsPath) {
return pluginsPath
}

return nil
}

public var sdkRootPath: AbsolutePath? {
configuration.sdkRootPath
}
Expand Down
118 changes: 117 additions & 1 deletion Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4997,6 +4997,122 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
])
}

func testSwiftTestingFlagsOnMacOSWithoutCustomToolchain() async throws {
#if !os(macOS)
// This is testing swift-testing in a toolchain which is macOS only feature.
try XCTSkipIf(true, "test is only supported on macOS")
#endif

let fs = InMemoryFileSystem(
emptyFiles:
"/fake/path/lib/swift/host/plugins/testing/libTestingMacros.dylib",
"/Pkg/Sources/Lib/main.swift",
"/Pkg/Tests/LibTest/test.swift"
)
try fs.createMockToolchain()

let userSwiftSDK = SwiftSDK(
hostTriple: .x86_64MacOS,
targetTriple: .x86_64MacOS,
toolset: .init(
knownTools: [
.cCompiler: .init(extraCLIOptions: []),
.swiftCompiler: .init(extraCLIOptions: []),
],
rootPaths: ["/fake/path/to"]
),
pathsConfiguration: .init(
sdkRootPath: "/fake/sdk",
swiftResourcesPath: "/fake/lib/swift",
swiftStaticResourcesPath: "/fake/lib/swift_static"
)
)

let env = Environment.mockEnvironment
let mockToolchain = try UserToolchain(
swiftSDK: userSwiftSDK,
environment: env,
searchStrategy: .custom(
searchPaths: getEnvSearchPaths(
pathString: env[.path],
currentWorkingDirectory: fs.currentWorkingDirectory
),
useXcrun: true
),
fileSystem: fs
)

XCTAssertEqual(
mockToolchain.extraFlags.swiftCompilerFlags,
[
"-plugin-path", "/fake/path/lib/swift/host/plugins/testing",
"-sdk", "/fake/sdk",
]
)
XCTAssertNoMatch(mockToolchain.extraFlags.linkerFlags, ["-rpath"])
XCTAssertNoMatch(mockToolchain.extraFlags.swiftCompilerFlags, [
"-I", "/fake/path/lib/swift/macosx/testing",
"-L", "/fake/path/lib/swift/macosx/testing",
])

let observability = ObservabilitySystem.makeForTesting()
let graph = try loadModulesGraph(
fileSystem: fs,
manifests: [
Manifest.createRootManifest(
displayName: "Pkg",
path: "/Pkg",
targets: [
TargetDescription(name: "Lib", dependencies: []),
TargetDescription(
name: "LibTest",
dependencies: ["Lib"],
type: .test
),
]
),
],
observabilityScope: observability.topScope
)
XCTAssertNoDiagnostics(observability.diagnostics)

let result = try await BuildPlanResult(plan: mockBuildPlan(
toolchain: mockToolchain,
graph: graph,
commonFlags: .init(),
fileSystem: fs,
observabilityScope: observability.topScope
))
result.checkProductsCount(2)
result.checkTargetsCount(3)

let testProductLinkArgs = try result.buildProduct(for: "Lib").linkArguments()
XCTAssertNoMatch(testProductLinkArgs, [
"-I", "/fake/path/lib/swift/macosx/testing",
"-L", "/fake/path/lib/swift/macosx/testing",
])

let libModuleArgs = try result.moduleBuildDescription(for: "Lib").swift().compileArguments()
XCTAssertMatch(libModuleArgs, [
"-plugin-path", "/fake/path/lib/swift/host/plugins/testing",
])
XCTAssertNoMatch(libModuleArgs, ["-Xlinker"])
XCTAssertNoMatch(libModuleArgs, [
"-I", "/fake/path/lib/swift/macosx/testing",
"-L", "/fake/path/lib/swift/macosx/testing",
])

let testModuleArgs = try result.moduleBuildDescription(for: "LibTest").swift().compileArguments()
XCTAssertMatch(testModuleArgs, [
"-plugin-path", "/fake/path/lib/swift/host/plugins/testing",
])
XCTAssertNoMatch(testModuleArgs, ["-Xlinker"])
XCTAssertNoMatch(testModuleArgs, [
"-I", "/fake/path/lib/swift/macosx/testing",
"-L", "/fake/path/lib/swift/macosx/testing",
])
}

func testSwiftTestingFlagsOnMacOSWithCustomToolchain() async throws {
#if !os(macOS)
// This is testing swift-testing in a toolchain which is macOS only feature.
Expand All @@ -5006,7 +5122,7 @@ class BuildPlanTestCase: BuildSystemProviderTestCase {
let fs = InMemoryFileSystem(
emptyFiles:
"/fake/path/lib/swift/macosx/testing/Testing.swiftmodule",
"/fake/path/lib/swift/host/plugins/testing/libTesting.dylib",
"/fake/path/lib/swift/host/plugins/testing/libTestingMacros.dylib",
"/Pkg/Sources/Lib/main.swift",
"/Pkg/Tests/LibTest/test.swift"
)
Expand Down