Skip to content

Commit 843ba4a

Browse files
committed
Warn when SWIFT_EXEC or SWIFT_EXEC_MANIFEST point to invalid paths.
Previously when SWIFT_EXEC or SWIFT_EXEC_MANIFEST were set to non-existent or non-executable paths, swiftpm would silently ignore them and fall back to the default Swift compiler. This caused confusion during development when typos or incorrect paths were used.
1 parent b520573 commit 843ba4a

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

Sources/CoreCommands/SwiftCommandState.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,7 @@ public final class SwiftCommandState {
10411041
swiftSDK: swiftSDK,
10421042
environment: self.environment,
10431043
customTargetInfo: targetInfo,
1044+
observabilityScope: self.observabilityScope,
10441045
fileSystem: self.fileSystem)
10451046
})
10461047
}()
@@ -1057,6 +1058,7 @@ public final class SwiftCommandState {
10571058
swiftSDK: hostSwiftSDK,
10581059
environment: self.environment,
10591060
customTargetInfo: targetInfo,
1061+
observabilityScope: self.observabilityScope,
10601062
fileSystem: self.fileSystem
10611063
)
10621064
})

Sources/PackageModel/UserToolchain.swift

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ public final class UserToolchain: Toolchain {
339339
useXcrun: Bool,
340340
environment: Environment,
341341
searchPaths: [AbsolutePath],
342-
fileSystem: any FileSystem
342+
fileSystem: any FileSystem,
343+
observabilityScope: ObservabilityScope? = nil
343344
) throws -> SwiftCompilers {
344345
func validateCompiler(at path: AbsolutePath?) throws {
345346
guard let path else { return }
@@ -351,10 +352,37 @@ public final class UserToolchain: Toolchain {
351352
}
352353

353354
let lookup = { UserToolchain.lookup(variable: $0, searchPaths: searchPaths, environment: environment) }
355+
356+
// Warn if SWIFT_EXEC or SWIFT_EXEC_MANIFEST is set but points to a non-existent or non-executable path
357+
func warnIfInvalid(envVar: String, value: String, resolved: AbsolutePath?) {
358+
guard resolved == nil else { return }
359+
360+
let message: String
361+
if let absolutePath = try? AbsolutePath(validating: value) {
362+
if fileSystem.exists(absolutePath) {
363+
message = "\(envVar) is set to '\(value)' which exists but is not executable; ignoring"
364+
} else {
365+
message = "\(envVar) is set to '\(value)' but the file does not exist; ignoring"
366+
}
367+
} else {
368+
message = "\(envVar) is set to '\(value)' but no executable was found in search paths; ignoring"
369+
}
370+
371+
observabilityScope?.emit(warning: message)
372+
}
373+
354374
// Get overrides.
355375
let SWIFT_EXEC_MANIFEST = lookup("SWIFT_EXEC_MANIFEST")
356376
let SWIFT_EXEC = lookup("SWIFT_EXEC")
357377

378+
// Emit warnings if environment variables are set but lookup failed
379+
if let swiftExecValue = environment["SWIFT_EXEC"], !swiftExecValue.isEmpty {
380+
warnIfInvalid(envVar: "SWIFT_EXEC", value: swiftExecValue, resolved: SWIFT_EXEC)
381+
}
382+
if let swiftExecManifestValue = environment["SWIFT_EXEC_MANIFEST"], !swiftExecManifestValue.isEmpty {
383+
warnIfInvalid(envVar: "SWIFT_EXEC_MANIFEST", value: swiftExecManifestValue, resolved: SWIFT_EXEC_MANIFEST)
384+
}
385+
358386
// Validate the overrides.
359387
try validateCompiler(at: SWIFT_EXEC)
360388
try validateCompiler(at: SWIFT_EXEC_MANIFEST)
@@ -693,6 +721,7 @@ public final class UserToolchain: Toolchain {
693721
customTargetInfo: JSON? = nil,
694722
customLibrariesLocation: ToolchainConfiguration.SwiftPMLibrariesLocation? = nil,
695723
customInstalledSwiftPMConfiguration: InstalledSwiftPMConfiguration? = nil,
724+
observabilityScope: ObservabilityScope? = nil,
696725
fileSystem: any FileSystem = localFileSystem
697726
) throws {
698727
self.swiftSDK = swiftSDK
@@ -716,7 +745,8 @@ public final class UserToolchain: Toolchain {
716745
useXcrun: self.useXcrun,
717746
environment: environment,
718747
searchPaths: self.envSearchPaths,
719-
fileSystem: fileSystem
748+
fileSystem: fileSystem,
749+
observabilityScope: observabilityScope
720750
)
721751
self.swiftCompilerPath = swiftCompilers.compile
722752
self.architectures = swiftSDK.architectures

0 commit comments

Comments
 (0)