@@ -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 }
0 commit comments