Skip to content

Commit 6a77e3b

Browse files
committed
Don’t store the default toolchain in the ToolchainRegistry
The computation of the default toolchain was a weird mixture of manually setting it and lazy resolving that made it hard to understand where the default toolchain was coming from. To simplify the logic, always compute the default toolchain from the raw data that we have.
1 parent 5e536a7 commit 6a77e3b

File tree

2 files changed

+13
-57
lines changed

2 files changed

+13
-57
lines changed

Sources/SKCore/ToolchainRegistry.swift

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ public final actor ToolchainRegistry {
4141
/// Note: Not all toolchains have a path.
4242
private var toolchainsByPath: [AbsolutePath: Toolchain] = [:]
4343

44-
/// The default toolchain.
45-
private var _default: Toolchain? = nil
46-
4744
/// The currently selected toolchain identifier on Darwin.
4845
public lazy var darwinToolchainOverride: String? = {
4946
if let id = ProcessEnv.vars["TOOLCHAINS"], !id.isEmpty, id != "default" {
@@ -90,9 +87,6 @@ public final actor ToolchainRegistry {
9087
) async {
9188
scanForToolchains(installPath: installPath, fileSystem)
9289
}
93-
}
94-
95-
extension ToolchainRegistry {
9690

9791
/// The default toolchain.
9892
///
@@ -103,26 +97,12 @@ extension ToolchainRegistry {
10397
/// The default toolchain must be only of the registered toolchains.
10498
public var `default`: Toolchain? {
10599
get {
106-
if _default == nil {
107-
if let tc = toolchainsByIdentifier[darwinToolchainIdentifier]?.first {
108-
_default = tc
109-
} else {
110-
_default = toolchains.first
111-
}
112-
}
113-
return _default
114-
}
115-
116-
set {
117-
guard let toolchain = newValue else {
118-
_default = nil
119-
return
100+
if let tc = toolchainsByIdentifier[darwinToolchainIdentifier]?.first {
101+
return tc
102+
} else {
103+
return toolchains.first
120104
}
121-
precondition(
122-
toolchains.contains { $0 === toolchain },
123-
"default toolchain must be registered first"
124-
)
125-
_default = toolchain
105+
return nil
126106
}
127107
}
128108

@@ -206,12 +186,11 @@ extension ToolchainRegistry {
206186
public func registerToolchain(
207187
_ path: AbsolutePath,
208188
_ fileSystem: FileSystem = localFileSystem
209-
) throws -> Toolchain {
189+
) throws {
210190
guard let toolchain = Toolchain(path, fileSystem) else {
211191
throw Error.invalidToolchain
212192
}
213193
try registerToolchain(toolchain)
214-
return toolchain
215194
}
216195
}
217196

@@ -240,12 +219,9 @@ extension ToolchainRegistry {
240219
AbsolutePath(validating: "/Library/Developer/Toolchains"),
241220
]
242221

243-
scanForToolchains(environmentVariables: environmentVariables, setDefault: true, fileSystem)
244-
if let installPath = installPath,
245-
let toolchain = try? registerToolchain(installPath, fileSystem),
246-
_default == nil
247-
{
248-
_default = toolchain
222+
scanForToolchains(environmentVariables: environmentVariables, fileSystem)
223+
if let installPath = installPath {
224+
try? registerToolchain(installPath, fileSystem)
249225
}
250226
for xcode in xcodes {
251227
scanForToolchains(xcode: xcode, fileSystem)
@@ -261,22 +237,16 @@ extension ToolchainRegistry {
261237
///
262238
/// - parameters:
263239
/// - environmentVariables: A list of environment variable names to search for toolchain paths.
264-
/// - setDefault: If true, the first toolchain found will be set as the default.
265240
@_spi(Testing)
266241
public func scanForToolchains(
267242
environmentVariables: [String],
268-
setDefault: Bool,
269243
_ fileSystem: FileSystem = localFileSystem
270244
) {
271-
var shouldSetDefault = setDefault
272245
for envVar in environmentVariables {
273246
if let pathStr = ProcessEnv.vars[envVar],
274-
let path = try? AbsolutePath(validating: pathStr),
275-
let toolchain = try? registerToolchain(path, fileSystem),
276-
shouldSetDefault
247+
let path = try? AbsolutePath(validating: pathStr)
277248
{
278-
shouldSetDefault = false
279-
_default = toolchain
249+
try? registerToolchain(path, fileSystem)
280250
}
281251
}
282252
}
@@ -285,7 +255,6 @@ extension ToolchainRegistry {
285255
///
286256
/// - parameters:
287257
/// - pathVariables: A list of PATH-like environment variable names to search.
288-
/// - setDefault: If true, the first toolchain found will be set as the default.
289258
@_spi(Testing)
290259
public func scanForToolchains(pathVariables: [String], _ fileSystem: FileSystem = localFileSystem) {
291260
pathVariables.lazy.flatMap { envVar in

Tests/SKCoreTests/ToolchainRegistryTests.swift

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ import XCTest
2020
import enum PackageLoading.Platform
2121

2222
extension ToolchainRegistry {
23-
func setDefault(_ newDefault: Toolchain?) {
24-
self.default = newDefault
25-
}
26-
2723
func setDarwinToolchainOverride(_ newValue: String?) {
2824
self.darwinToolchainOverride = newValue
2925
}
@@ -38,10 +34,6 @@ final class ToolchainRegistryTests: XCTestCase {
3834
let b = Toolchain(identifier: "b", displayName: "b", path: nil)
3935
try await tr.registerToolchain(b)
4036
await assertEqual(tr.default?.identifier, "a")
41-
await tr.setDefault(b)
42-
await assertEqual(tr.default?.identifier, "b")
43-
await tr.setDefault(nil)
44-
await assertEqual(tr.default?.identifier, "a")
4537
await assertTrue(tr.default === tr.toolchain(identifier: "a"))
4638
}
4739

@@ -59,10 +51,6 @@ final class ToolchainRegistryTests: XCTestCase {
5951
Toolchain(identifier: ToolchainRegistry.darwinDefaultToolchainIdentifier, displayName: "a", path: nil)
6052
)
6153
await assertEqual(tr.default?.identifier, ToolchainRegistry.darwinDefaultToolchainIdentifier)
62-
await tr.setDefault(a)
63-
await assertEqual(tr.default?.identifier, "a")
64-
await tr.setDefault(nil)
65-
await assertEqual(tr.default?.identifier, ToolchainRegistry.darwinDefaultToolchainIdentifier)
6654
}
6755

6856
func testUnknownPlatform() throws {
@@ -322,7 +310,6 @@ final class ToolchainRegistryTests: XCTestCase {
322310

323311
await tr.scanForToolchains(
324312
environmentVariables: ["TEST_ENV_SOURCEKIT_TOOLCHAIN_PATH_2"],
325-
setDefault: false,
326313
fs
327314
)
328315

@@ -388,8 +375,8 @@ final class ToolchainRegistryTests: XCTestCase {
388375
XCTAssertNotNil(t2.swiftc)
389376

390377
let tr = ToolchainRegistry.empty
391-
let t3 = try await tr.registerToolchain(path.parentDirectory, fs)
392-
XCTAssertEqual(t3.identifier, t2.identifier)
378+
try await tr.registerToolchain(path.parentDirectory, fs)
379+
let t3 = try await unwrap(tr.toolchain(identifier: t2.identifier))
393380
XCTAssertEqual(t3.sourcekitd, t2.sourcekitd)
394381
XCTAssertEqual(t3.clang, t2.clang)
395382
XCTAssertEqual(t3.clangd, t2.clangd)

0 commit comments

Comments
 (0)