From c4afea81593507a446a8e50c3e3b26962a8e75e6 Mon Sep 17 00:00:00 2001 From: Paul LeMarquand Date: Thu, 10 Jul 2025 10:51:40 -0400 Subject: [PATCH] Improve swift not found error logging If the swift binary can't be found log better messages to the Swift output channel for dianosing. --- src/extension.ts | 2 +- src/toolchain/toolchain.ts | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 4ab80c10a..33ef8a0b3 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -236,7 +236,7 @@ async function createActiveToolchain( outputChannel: SwiftOutputChannel ): Promise { try { - const toolchain = await SwiftToolchain.create(); + const toolchain = await SwiftToolchain.create(undefined, outputChannel); toolchain.logDiagnostics(outputChannel); contextKeys.updateKeysBasedOnActiveVersion(toolchain.swiftVersion); return toolchain; diff --git a/src/toolchain/toolchain.ts b/src/toolchain/toolchain.ts index b903ddef3..d3a1370ab 100644 --- a/src/toolchain/toolchain.ts +++ b/src/toolchain/toolchain.ts @@ -118,8 +118,11 @@ export class SwiftToolchain { this.swiftVersionString = targetInfo.compilerVersion; } - static async create(folder?: vscode.Uri): Promise { - const swiftFolderPath = await this.getSwiftFolderPath(folder); + static async create( + folder?: vscode.Uri, + outputChannel?: vscode.OutputChannel + ): Promise { + const swiftFolderPath = await this.getSwiftFolderPath(folder, outputChannel); const toolchainPath = await this.getToolchainPath(swiftFolderPath, folder); const targetInfo = await this.getSwiftTargetInfo( this._getToolchainExecutable(toolchainPath, "swift") @@ -561,7 +564,10 @@ export class SwiftToolchain { channel.logDiagnostic(this.diagnostics); } - private static async getSwiftFolderPath(cwd?: vscode.Uri): Promise { + private static async getSwiftFolderPath( + cwd?: vscode.Uri, + outputChannel?: vscode.OutputChannel + ): Promise { try { let swift: string; if (configuration.path !== "") { @@ -589,7 +595,7 @@ export class SwiftToolchain { // use `type swift` to find `swift`. Run inside /bin/sh to ensure // we get consistent output as different shells output a different // format. Tried running with `-p` but that is not available in /bin/sh - const { stdout } = await execFile("/bin/sh", [ + const { stdout, stderr } = await execFile("/bin/sh", [ "-c", "LC_MESSAGES=C type swift", ]); @@ -597,7 +603,9 @@ export class SwiftToolchain { if (swiftMatch) { swift = swiftMatch[1]; } else { - throw Error("Failed to find swift executable"); + throw Error( + `/bin/sh -c LC_MESSAGES=C type swift: stdout: ${stdout}, stderr: ${stderr}` + ); } break; } @@ -617,7 +625,8 @@ export class SwiftToolchain { } const swiftPath = expandFilePathTilde(path.dirname(realSwift)); return await this.getSwiftEnvPath(swiftPath); - } catch { + } catch (error) { + outputChannel?.appendLine(`Failed to find swift executable: ${error}`); throw Error("Failed to find swift executable"); } }