Skip to content

Commit 7a5dfd2

Browse files
authored
Validate SDK before overwriting (#192)
Running an invalid `sdk` subcommand like `xtool sdk list` (there's no `list` subcommand) is interpreted as "install the sdk at path `./list`". We previously unconditionally removed the existing SDK during installation. By switching the order so that we validate the path first, we avoid performing a potentially destructive action unless we have clear intent. See #186. Before: ```bash $ xtool sdk list Removing existing SDK... # destructive! Error: Could not read file or directory at path 'list'. ``` After: ```bash $ xtool sdk list Error: Could not read file or directory at path 'list'. See 'xtool help sdk' for usage. ```
1 parent 22655f3 commit 7a5dfd2

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

Sources/XToolSupport/SDKBuilder.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ struct SDKBuilder {
1818
init(path: String) throws {
1919
var isDir: ObjCBool = false
2020
guard FileManager.default.fileExists(atPath: path, isDirectory: &isDir) else {
21-
throw Console.Error("Could not read file or directory at path '\(path)'")
21+
throw Console.Error("""
22+
Could not read file or directory at path '\(path)'.
23+
See 'xtool help sdk' for usage.
24+
""")
2225
}
2326

2427
let url = URL(fileURLWithPath: path)

Sources/XToolSupport/SDKCommand.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,15 @@ struct InstallSDKOperation {
243243
#if os(macOS)
244244
print("Skipping SDK install; the iOS SDK ships with Xcode on macOS")
245245
#else
246+
// validate input before removing existing SDK
247+
let input = try SDKBuilder.Input(path: path)
248+
let arch = try ArchSelection.auto.sdkBuilderArch
249+
246250
if let sdk = try await DarwinSDK.current() {
247251
print("Removing existing SDK...")
248252
try sdk.remove()
249253
}
250254

251-
let input = try SDKBuilder.Input(path: path)
252-
let arch = try ArchSelection.auto.sdkBuilderArch
253-
254255
let tempDir = try TemporaryDirectory(name: "DarwinSDKBuild")
255256
let builder = SDKBuilder(input: input, outputPath: tempDir.url.path, arch: arch)
256257
let sdkPath = try await builder.buildSDK()

0 commit comments

Comments
 (0)