Skip to content

Commit 48df054

Browse files
committed
Support running tests that require building with a Swift 5.10 toolchain
When we detect that we’re running using a 5.10 toolchain, adjust the compiler arguments that we received from SwiftPM to drop any `/Modules` suffixes in the build directory, to account for the fact that 5.10 stores the modules one directory higher up (swiftlang/swift-package-manager#7103).
1 parent 129d3a4 commit 48df054

11 files changed

+25
-34
lines changed

Sources/SKSwiftPMWorkspace/SwiftPMBuildSystem.swift

+22-3
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,30 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
357357

358358
public var indexPrefixMappings: [PathPrefixMapping] { return [] }
359359

360+
/// Return the compiler arguments for the given source file within a target, making any necessary adjustments to
361+
/// account for differences in the SwiftPM versions being linked into SwiftPM and being installed in the toolchain.
362+
private func compilerArguments(for file: URL, in buildTarget: any SwiftBuildTarget) async throws -> [String] {
363+
let compileArguments = try buildTarget.compileArguments(for: file)
364+
365+
// Fix up compiler arguments that point to a `/Modules` subdirectory if the Swift version in the toolchain is less
366+
// than 6.0 because it places the modules one level higher up.
367+
let toolchainVersion = await orLog("Getting Swift version") { try await toolchainRegistry.default?.swiftVersion }
368+
guard let toolchainVersion, toolchainVersion < SwiftVersion(6, 0) else {
369+
return compileArguments
370+
}
371+
return compileArguments.map { argument in
372+
if argument.hasSuffix("/Modules"), argument.contains(self.workspace.location.scratchDirectory.pathString) {
373+
return String(argument.dropLast(8))
374+
}
375+
return argument
376+
}
377+
}
378+
360379
public func buildSettings(
361380
for uri: DocumentURI,
362381
in configuredTarget: ConfiguredTarget,
363382
language: Language
364-
) throws -> FileBuildSettings? {
383+
) async throws -> FileBuildSettings? {
365384
guard let url = uri.fileURL, let path = try? AbsolutePath(validating: url.path) else {
366385
// We can't determine build settings for non-file URIs.
367386
return nil
@@ -387,13 +406,13 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
387406
// getting its compiler arguments and then patching up the compiler arguments by replacing the substitute file
388407
// with the `.cpp` file.
389408
return FileBuildSettings(
390-
compilerArguments: try buildTarget.compileArguments(for: substituteFile),
409+
compilerArguments: try await compilerArguments(for: substituteFile, in: buildTarget),
391410
workingDirectory: workspacePath.pathString
392411
).patching(newFile: try resolveSymlinks(path).pathString, originalFile: substituteFile.absoluteString)
393412
}
394413

395414
return FileBuildSettings(
396-
compilerArguments: try buildTarget.compileArguments(for: url),
415+
compilerArguments: try await compilerArguments(for: url, in: buildTarget),
397416
workingDirectory: workspacePath.pathString
398417
)
399418
}

Sources/SKTestSupport/SkipUnless.swift

-4
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,6 @@ public actor SkipUnless {
170170

171171
/// SwiftPM moved the location where it stores Swift modules to a subdirectory in
172172
/// https://github.com/apple/swift-package-manager/pull/7103.
173-
///
174-
/// sourcekit-lsp uses the built-in SwiftPM to synthesize compiler arguments and cross-module tests fail if the host
175-
/// toolchain’s SwiftPM stores the Swift modules on the top level but we synthesize compiler arguments expecting the
176-
/// modules to be in a `Modules` subdirectory.
177173
public static func swiftpmStoresModulesInSubdirectory(
178174
file: StaticString = #filePath,
179175
line: UInt = #line

Tests/SKSwiftPMWorkspaceTests/SwiftPMBuildSystemTests.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import struct PackageModel.BuildFlags
2828
#endif
2929

3030
fileprivate extension SwiftPMBuildSystem {
31-
func buildSettings(for uri: DocumentURI, language: Language) throws -> FileBuildSettings? {
31+
func buildSettings(for uri: DocumentURI, language: Language) async throws -> FileBuildSettings? {
3232
guard let target = self.configuredTargets(for: uri).only else {
3333
return nil
3434
}
35-
return try buildSettings(for: uri, in: target, language: language)
35+
return try await buildSettings(for: uri, in: target, language: language)
3636
}
3737
}
3838

@@ -116,6 +116,7 @@ final class SwiftPMBuildSystemTests: XCTestCase {
116116
}
117117

118118
func testBasicSwiftArgs() async throws {
119+
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
119120
let fs = localFileSystem
120121
try await withTestScratchDir { tempDir in
121122
try fs.createFiles(

Tests/SourceKitLSPTests/BackgroundIndexingTests.swift

-6
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ final class BackgroundIndexingTests: XCTestCase {
104104
}
105105

106106
func testBackgroundIndexingOfMultiModuleProject() async throws {
107-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
108107
let project = try await SwiftPMTestProject(
109108
files: [
110109
"LibA/MyFile.swift": """
@@ -212,7 +211,6 @@ final class BackgroundIndexingTests: XCTestCase {
212211
}
213212

214213
func testBackgroundIndexingOfPackageDependency() async throws {
215-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
216214
let dependencyContents = """
217215
public func 1️⃣doSomething() {}
218216
"""
@@ -541,7 +539,6 @@ final class BackgroundIndexingTests: XCTestCase {
541539
}
542540

543541
func testPrepareTargetAfterEditToDependency() async throws {
544-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
545542
var serverOptions = SourceKitLSPServer.Options.testDefault
546543
let expectedPreparationTracker = ExpectedIndexTaskTracker(expectedPreparations: [
547544
[
@@ -651,7 +648,6 @@ final class BackgroundIndexingTests: XCTestCase {
651648
let libBStartedPreparation = self.expectation(description: "LibB started preparing")
652649
let libDPreparedForEditing = self.expectation(description: "LibD prepared for editing")
653650

654-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
655651
var serverOptions = SourceKitLSPServer.Options.testDefault
656652
let expectedPreparationTracker = ExpectedIndexTaskTracker(expectedPreparations: [
657653
// Preparation of targets during the initial of the target
@@ -749,8 +745,6 @@ final class BackgroundIndexingTests: XCTestCase {
749745
}
750746

751747
func testIndexingHappensInParallel() async throws {
752-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
753-
754748
let fileAIndexingStarted = self.expectation(description: "FileA indexing started")
755749
let fileBIndexingStarted = self.expectation(description: "FileB indexing started")
756750

Tests/SourceKitLSPTests/DefinitionTests.swift

-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,6 @@ class DefinitionTests: XCTestCase {
440440

441441
func testDependentModuleGotBuilt() async throws {
442442
try SkipUnless.longTestsEnabled()
443-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
444443
let project = try await SwiftPMTestProject(
445444
files: [
446445
"LibA/LibA.swift": """

Tests/SourceKitLSPTests/DependencyTrackingTests.swift

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import XCTest
1616

1717
final class DependencyTrackingTests: XCTestCase {
1818
func testDependenciesUpdatedSwift() async throws {
19-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
2019
let project = try await SwiftPMTestProject(
2120
files: [
2221
"LibA/LibA.swift": """

Tests/SourceKitLSPTests/IndexTests.swift

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import XCTest
1616

1717
final class IndexTests: XCTestCase {
1818
func testIndexSwiftModules() async throws {
19-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
2019
let project = try await SwiftPMTestProject(
2120
files: [
2221
"LibA/LibA.swift": """

Tests/SourceKitLSPTests/PublishDiagnosticsTests.swift

-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ final class PublishDiagnosticsTests: XCTestCase {
154154

155155
func testDiagnosticUpdatedAfterDependentModuleIsBuilt() async throws {
156156
try SkipUnless.longTestsEnabled()
157-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
158157

159158
let project = try await SwiftPMTestProject(
160159
files: [

Tests/SourceKitLSPTests/PullDiagnosticsTests.swift

-3
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ final class PullDiagnosticsTests: XCTestCase {
185185

186186
func testDiagnosticUpdatedAfterDependentModuleIsBuilt() async throws {
187187
try SkipUnless.longTestsEnabled()
188-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
189188

190189
let project = try await SwiftPMTestProject(
191190
files: [
@@ -251,8 +250,6 @@ final class PullDiagnosticsTests: XCTestCase {
251250
}
252251

253252
func testDiagnosticsWaitForDocumentToBePrepared() async throws {
254-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
255-
256253
nonisolated(unsafe) var diagnosticRequestSent = AtomicBool(initialValue: false)
257254
var serverOptions = SourceKitLSPServer.Options.testDefault
258255
serverOptions.indexTestHooks.preparationTaskDidStart = { @Sendable taskDescription in

Tests/SourceKitLSPTests/SwiftInterfaceTests.swift

-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ final class SwiftInterfaceTests: XCTestCase {
5555
}
5656

5757
func testOpenInterface() async throws {
58-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
5958
let project = try await SwiftPMTestProject(
6059
files: [
6160
"MyLibrary/MyLibrary.swift": """
@@ -152,7 +151,6 @@ final class SwiftInterfaceTests: XCTestCase {
152151
}
153152

154153
func testSwiftInterfaceAcrossModules() async throws {
155-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
156154
let project = try await SwiftPMTestProject(
157155
files: [
158156
"MyLibrary/MyLibrary.swift": """

Tests/SourceKitLSPTests/WorkspaceTests.swift

-10
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import XCTest
2323
final class WorkspaceTests: XCTestCase {
2424

2525
func testMultipleSwiftPMWorkspaces() async throws {
26-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
27-
2826
// The package manifest is the same for both packages we open.
2927
let packageManifest = """
3028
// swift-tools-version: 5.7
@@ -195,8 +193,6 @@ final class WorkspaceTests: XCTestCase {
195193
}
196194

197195
func testSwiftPMPackageInSubfolder() async throws {
198-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
199-
200196
let packageManifest = """
201197
// swift-tools-version: 5.7
202198
@@ -274,8 +270,6 @@ final class WorkspaceTests: XCTestCase {
274270
}
275271

276272
func testNestedSwiftPMWorkspacesWithoutDedicatedWorkspaceFolder() async throws {
277-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
278-
279273
// The package manifest is the same for both packages we open.
280274
let packageManifest = """
281275
// swift-tools-version: 5.7
@@ -600,8 +594,6 @@ final class WorkspaceTests: XCTestCase {
600594
}
601595

602596
func testChangeWorkspaceFolders() async throws {
603-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
604-
605597
let project = try await MultiFileTestProject(
606598
files: [
607599
"subdir/Sources/otherPackage/otherPackage.swift": """
@@ -775,8 +767,6 @@ final class WorkspaceTests: XCTestCase {
775767
func testIntegrationTest() async throws {
776768
// This test is doing the same as `test-sourcekit-lsp` in the `swift-integration-tests` repo.
777769

778-
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
779-
780770
let project = try await SwiftPMTestProject(
781771
files: [
782772
"Sources/clib/include/clib.h": """

0 commit comments

Comments
 (0)