Skip to content

Improve swift not found error logging #1710

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ async function createActiveToolchain(
outputChannel: SwiftOutputChannel
): Promise<SwiftToolchain | undefined> {
try {
const toolchain = await SwiftToolchain.create();
const toolchain = await SwiftToolchain.create(undefined, outputChannel);
toolchain.logDiagnostics(outputChannel);
contextKeys.updateKeysBasedOnActiveVersion(toolchain.swiftVersion);
return toolchain;
Expand Down
21 changes: 15 additions & 6 deletions src/toolchain/toolchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,11 @@ export class SwiftToolchain {
this.swiftVersionString = targetInfo.compilerVersion;
}

static async create(folder?: vscode.Uri): Promise<SwiftToolchain> {
const swiftFolderPath = await this.getSwiftFolderPath(folder);
static async create(
folder?: vscode.Uri,
outputChannel?: vscode.OutputChannel
): Promise<SwiftToolchain> {
const swiftFolderPath = await this.getSwiftFolderPath(folder, outputChannel);
const toolchainPath = await this.getToolchainPath(swiftFolderPath, folder);
const targetInfo = await this.getSwiftTargetInfo(
this._getToolchainExecutable(toolchainPath, "swift")
Expand Down Expand Up @@ -561,7 +564,10 @@ export class SwiftToolchain {
channel.logDiagnostic(this.diagnostics);
}

private static async getSwiftFolderPath(cwd?: vscode.Uri): Promise<string> {
private static async getSwiftFolderPath(
cwd?: vscode.Uri,
outputChannel?: vscode.OutputChannel
): Promise<string> {
try {
let swift: string;
if (configuration.path !== "") {
Expand Down Expand Up @@ -589,15 +595,17 @@ 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",
]);
const swiftMatch = /^swift is (.*)$/.exec(stdout.trimEnd());
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;
}
Expand All @@ -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");
}
}
Expand Down