Skip to content

Commit 59cf0bd

Browse files
authored
Improve swift not found error logging (#1710)
If the swift binary can't be found log better messages to the Swift output channel for dianosing.
1 parent a512bbd commit 59cf0bd

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ async function createActiveToolchain(
236236
outputChannel: SwiftOutputChannel
237237
): Promise<SwiftToolchain | undefined> {
238238
try {
239-
const toolchain = await SwiftToolchain.create();
239+
const toolchain = await SwiftToolchain.create(undefined, outputChannel);
240240
toolchain.logDiagnostics(outputChannel);
241241
contextKeys.updateKeysBasedOnActiveVersion(toolchain.swiftVersion);
242242
return toolchain;

src/toolchain/toolchain.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,11 @@ export class SwiftToolchain {
118118
this.swiftVersionString = targetInfo.compilerVersion;
119119
}
120120

121-
static async create(folder?: vscode.Uri): Promise<SwiftToolchain> {
122-
const swiftFolderPath = await this.getSwiftFolderPath(folder);
121+
static async create(
122+
folder?: vscode.Uri,
123+
outputChannel?: vscode.OutputChannel
124+
): Promise<SwiftToolchain> {
125+
const swiftFolderPath = await this.getSwiftFolderPath(folder, outputChannel);
123126
const toolchainPath = await this.getToolchainPath(swiftFolderPath, folder);
124127
const targetInfo = await this.getSwiftTargetInfo(
125128
this._getToolchainExecutable(toolchainPath, "swift")
@@ -561,7 +564,10 @@ export class SwiftToolchain {
561564
channel.logDiagnostic(this.diagnostics);
562565
}
563566

564-
private static async getSwiftFolderPath(cwd?: vscode.Uri): Promise<string> {
567+
private static async getSwiftFolderPath(
568+
cwd?: vscode.Uri,
569+
outputChannel?: vscode.OutputChannel
570+
): Promise<string> {
565571
try {
566572
let swift: string;
567573
if (configuration.path !== "") {
@@ -589,15 +595,17 @@ export class SwiftToolchain {
589595
// use `type swift` to find `swift`. Run inside /bin/sh to ensure
590596
// we get consistent output as different shells output a different
591597
// format. Tried running with `-p` but that is not available in /bin/sh
592-
const { stdout } = await execFile("/bin/sh", [
598+
const { stdout, stderr } = await execFile("/bin/sh", [
593599
"-c",
594600
"LC_MESSAGES=C type swift",
595601
]);
596602
const swiftMatch = /^swift is (.*)$/.exec(stdout.trimEnd());
597603
if (swiftMatch) {
598604
swift = swiftMatch[1];
599605
} else {
600-
throw Error("Failed to find swift executable");
606+
throw Error(
607+
`/bin/sh -c LC_MESSAGES=C type swift: stdout: ${stdout}, stderr: ${stderr}`
608+
);
601609
}
602610
break;
603611
}
@@ -617,7 +625,8 @@ export class SwiftToolchain {
617625
}
618626
const swiftPath = expandFilePathTilde(path.dirname(realSwift));
619627
return await this.getSwiftEnvPath(swiftPath);
620-
} catch {
628+
} catch (error) {
629+
outputChannel?.appendLine(`Failed to find swift executable: ${error}`);
621630
throw Error("Failed to find swift executable");
622631
}
623632
}

0 commit comments

Comments
 (0)