Skip to content

Commit 0d175a1

Browse files
authored
do not use fatalError in Triple.getHostTriple (#6278)
motivation: avoid crashes, use structured error handling changes: * make Triple.getHostTriple throwable and remove use of fatalError * update call sites rdar://90317112
1 parent 81f972e commit 0d175a1

File tree

7 files changed

+15
-15
lines changed

7 files changed

+15
-15
lines changed

Sources/Basics/Triple.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public struct Triple: Encodable, Equatable, Sendable {
194194
public static let macOS = try! Triple("x86_64-apple-macosx")
195195

196196
/// Determine the versioned host triple using the Swift compiler.
197-
public static func getHostTriple(usingSwiftCompiler swiftCompiler: AbsolutePath) -> Triple {
197+
public static func getHostTriple(usingSwiftCompiler swiftCompiler: AbsolutePath) throws -> Triple {
198198
// Call the compiler to get the target info JSON.
199199
let compilerOutput: String
200200
do {
@@ -206,28 +206,28 @@ public struct Triple: Encodable, Equatable, Sendable {
206206
#if os(macOS)
207207
return .macOS
208208
#else
209-
fatalError("Failed to get target info (\(error))")
209+
throw InternalError("Failed to get target info (\(error))")
210210
#endif
211211
}
212212
// Parse the compiler's JSON output.
213213
let parsedTargetInfo: JSON
214214
do {
215215
parsedTargetInfo = try JSON(string: compilerOutput)
216216
} catch {
217-
fatalError("Failed to parse target info (\(error)).\nRaw compiler output: \(compilerOutput)")
217+
throw InternalError("Failed to parse target info (\(error)).\nRaw compiler output: \(compilerOutput)")
218218
}
219219
// Get the triple string from the parsed JSON.
220220
let tripleString: String
221221
do {
222222
tripleString = try parsedTargetInfo.get("target").get("triple")
223223
} catch {
224-
fatalError("Target info does not contain a triple string (\(error)).\nTarget info: \(parsedTargetInfo)")
224+
throw InternalError("Target info does not contain a triple string (\(error)).\nTarget info: \(parsedTargetInfo)")
225225
}
226226
// Parse the triple string.
227227
do {
228228
return try Triple(tripleString)
229229
} catch {
230-
fatalError("Failed to parse triple string (\(error)).\nTriple string: \(tripleString)")
230+
throw InternalError("Failed to parse triple string (\(error)).\nTriple string: \(tripleString)")
231231
}
232232
}
233233

Sources/CoreCommands/SwiftTool.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ public final class SwiftTool {
720720
let dataPath = self.scratchDirectory.appending(
721721
component: destinationTriple.platformBuildPathComponent(buildSystem: options.build.buildSystem)
722722
)
723-
return BuildParameters(
723+
return try BuildParameters(
724724
dataPath: dataPath,
725725
configuration: options.build.configuration,
726726
toolchain: destinationToolchain,
@@ -757,7 +757,7 @@ public final class SwiftTool {
757757
do {
758758
let hostToolchain = try _hostToolchain.get()
759759
hostDestination = hostToolchain.destination
760-
let hostTriple = Triple.getHostTriple(usingSwiftCompiler: hostToolchain.swiftCompilerPath)
760+
let hostTriple = try Triple.getHostTriple(usingSwiftCompiler: hostToolchain.swiftCompilerPath)
761761

762762
// Create custom toolchain if present.
763763
if let customDestination = options.locations.customCompileDestination {

Sources/CrossCompilationDestinationsTool/DestinationCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ extension DestinationCommand {
6969
let destinationsDirectory = try self.getOrCreateDestinationsDirectory()
7070

7171
let hostToolchain = try UserToolchain(destination: Destination.hostDestination())
72-
let triple = Triple.getHostTriple(usingSwiftCompiler: hostToolchain.swiftCompilerPath)
72+
let triple = try Triple.getHostTriple(usingSwiftCompiler: hostToolchain.swiftCompilerPath)
7373

7474
var commandError: Error? = nil
7575
do {

Sources/PackageModel/UserToolchain.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ public final class UserToolchain: Toolchain {
453453
self.architectures = destination.architectures
454454

455455
// Use the triple from destination or compute the host triple using swiftc.
456-
var triple = destination.targetTriple ?? Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)
456+
var triple = try destination.targetTriple ?? Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)
457457

458458
// Change the triple to the specified arch if there's exactly one of them.
459459
// The Triple property is only looked at by the native build system currently.

Sources/SPMBuildCore/BuildParameters.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,13 @@ public struct BuildParameters: Encodable {
251251
linkerDeadStrip: Bool = true,
252252
colorizedOutput: Bool = false,
253253
verboseOutput: Bool = false
254-
) {
255-
let triple = destinationTriple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompilerPath)
254+
) throws {
255+
let triple = try destinationTriple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompilerPath)
256256

257257
self.dataPath = dataPath
258258
self.configuration = configuration
259259
self._toolchain = _Toolchain(toolchain: toolchain)
260-
self.hostTriple = hostTriple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompilerPath)
260+
self.hostTriple = try hostTriple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompilerPath)
261261
self.triple = triple
262262
self.flags = flags
263263
self.pkgConfigDirectories = pkgConfigDirectories
@@ -305,7 +305,7 @@ public struct BuildParameters: Encodable {
305305
testEntryPointPath = nil
306306
}
307307

308-
return .init(
308+
return try .init(
309309
dataPath: self.dataPath.parentDirectory.appending(components: ["plugins", "tools"]),
310310
configuration: self.configuration,
311311
toolchain: try UserToolchain(destination: Destination.hostDestination()),

Sources/swift-bootstrap/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ struct SwiftBootstrapBuildTool: ParsableCommand {
250250
component: self.destinationToolchain.triple.platformBuildPathComponent(buildSystem: buildSystem)
251251
)
252252

253-
let buildParameters = BuildParameters(
253+
let buildParameters = try BuildParameters(
254254
dataPath: dataPath,
255255
configuration: configuration,
256256
toolchain: self.destinationToolchain,

Tests/BuildTests/MockBuildTestHelper.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func mockBuildParameters(
7676
useExplicitModuleBuild: Bool = false,
7777
linkerDeadStrip: Bool = true
7878
) -> BuildParameters {
79-
return BuildParameters(
79+
return try! BuildParameters(
8080
dataPath: buildPath,
8181
configuration: config,
8282
toolchain: toolchain,

0 commit comments

Comments
 (0)