Skip to content

Commit c14c506

Browse files
ensure that the .swift-version file exists before calling swiftly use (#1875)
1 parent 7829093 commit c14c506

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

src/toolchain/swiftly.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { installSwiftlyToolchainWithProgress } from "../commands/installSwiftlyT
2525
import { ContextKeys } from "../contextKeys";
2626
import { SwiftLogger } from "../logging/SwiftLogger";
2727
import { showMissingToolchainDialog } from "../ui/ToolchainSelection";
28+
import { touch } from "../utilities/filesystem";
2829
import { findBinaryPath } from "../utilities/shell";
2930
import { ExecFileError, execFile, execFileStreamOutput } from "../utilities/utilities";
3031
import { Version } from "../utilities/version";
@@ -339,6 +340,7 @@ export class Swiftly {
339340
const options: ExecFileOptions = {};
340341
if (cwd) {
341342
options.cwd = cwd;
343+
await touch(path.join(cwd, ".swift-version"));
342344
} else {
343345
useArgs.push("--global-default");
344346
}

src/utilities/filesystem.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ export async function fileExists(...pathComponents: string[]): Promise<boolean>
4848
}
4949
}
5050

51+
/**
52+
* Checks if a file exists on disk and, if it doesn't, creates it. If the file does exist
53+
* then this function does nothing.
54+
* @param path The path to the file.
55+
*/
56+
export async function touch(path: string): Promise<void> {
57+
if (!(await fileExists(path))) {
58+
const handle = await fs.open(path, "a");
59+
await handle.close();
60+
}
61+
}
62+
5163
/**
5264
* Return whether a file/folder is inside a folder.
5365
* @param subpath child file/folder

test/unit-tests/toolchain/swiftly.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ suite("Swiftly Unit Tests", () => {
8585
});
8686

8787
test("sets the toolchain in cwd if it is provided", async () => {
88+
// CWD exists
89+
mockFS({ "/home/user/project": mockFS.directory() });
8890
// Mock version check to return 1.0.1
8991
mockUtilities.execFile.withArgs("swiftly", ["--version"]).resolves({
9092
stdout: "1.1.0\n",
@@ -100,6 +102,8 @@ suite("Swiftly Unit Tests", () => {
100102
["use", "-y", "6.1.0"],
101103
match.has("cwd", "/home/user/project")
102104
);
105+
const stats = await fs.stat("/home/user/project/.swift-version");
106+
expect(stats.isFile(), "Expected .swift-version file to be created").to.be.true;
103107
});
104108
});
105109

0 commit comments

Comments
 (0)