From 1c15fb06c62264ccf163d3a2c3c0afe0391a546b Mon Sep 17 00:00:00 2001 From: Techatrix Date: Tue, 24 Sep 2024 19:16:59 +0200 Subject: [PATCH] fix formatter initialization when Zig executable is not found --- src/zigFormat.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/zigFormat.ts b/src/zigFormat.ts index 210cf93..f954100 100644 --- a/src/zigFormat.ts +++ b/src/zigFormat.ts @@ -50,9 +50,24 @@ function preCompileZigFmt() { // This pre-compiles even if "zig.formattingProvider" is "zls". if (vscode.workspace.getConfiguration("zig").get("formattingProvider") === "off") return; - childProcess.execFile(getZigPath(), ["fmt", "--help"], { - timeout: 60000, // 60 seconds (this is a very high value because 'zig fmt' is just in time compiled) - }); + let zigPath: string; + try { + zigPath = getZigPath(); + } catch { + return; + } + + try { + childProcess.execFile(zigPath, ["fmt", "--help"], { + timeout: 60000, // 60 seconds (this is a very high value because 'zig fmt' is just in time compiled) + }); + } catch (err) { + if (err instanceof Error) { + void vscode.window.showErrorMessage(`Failed to run 'zig fmt': ${err.message}`); + } else { + throw err; + } + } } async function provideDocumentRangeFormattingEdits(