Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.
Closed
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
10 changes: 2 additions & 8 deletions src/zigProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class ZigProvider {
const zigConfig = vscode.workspace.getConfiguration("zig");
if (!zigPath) {
await workspaceConfigUpdateNoThrow(zigConfig, "path", undefined, true);
this.set(null);
return;
}
const newValue = this.resolveZigPathConfigOption(zigPath);
Expand All @@ -59,16 +60,9 @@ export class ZigProvider {
const result = resolveExePathAndVersion(zigPath, "version");
if ("message" in result) {
vscode.window
.showErrorMessage(`Unexpected 'zig.path': ${result.message}`, "install Zig", "open settings")
.showErrorMessage(`Unexpected 'zig.path': ${result.message}`, "open settings")
.then(async (response) => {
switch (response) {
case "install Zig":
await workspaceConfigUpdateNoThrow(
vscode.workspace.getConfiguration("zig"),
"path",
undefined,
);
break;
case "open settings":
await vscode.commands.executeCommand("workbench.action.openSettings", "zig.path");
break;
Expand Down
37 changes: 28 additions & 9 deletions src/zigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ let languageStatusItem: vscode.LanguageStatusItem;
let versionManagerConfig: versionManager.Config;
export let zigProvider: ZigProvider;

/** Removes the `zig.path` config option. */
/** Installs Zig, respecting existing `zig.path` config option. */
async function installZig(context: vscode.ExtensionContext, temporaryVersion?: semver.SemVer) {
const existingPath = vscode.workspace.getConfiguration("zig").get<string>("path");
if (existingPath) {
const result = resolveExePathAndVersion(existingPath, "version");
if ("exe" in result) {
zigProvider.set(result);
return;
}
}

let version = temporaryVersion;

if (!version) {
Expand All @@ -43,7 +52,6 @@ async function installZig(context: vscode.ExtensionContext, temporaryVersion?: s
// Lookup zig in $PATH
const result = resolveExePathAndVersion("zig", "version");
if ("exe" in result) {
await vscode.workspace.getConfiguration("zig").update("path", undefined, true);
zigProvider.set(result);
return;
}
Expand All @@ -61,8 +69,6 @@ async function installZig(context: vscode.ExtensionContext, temporaryVersion?: s

try {
const exePath = await versionManager.install(versionManagerConfig, version);
const zigConfig = vscode.workspace.getConfiguration("zig");
await workspaceConfigUpdateNoThrow(zigConfig, "path", undefined, true);
zigProvider.set({ exe: exePath, version: version });
} catch (err) {
zigProvider.set(null);
Expand Down Expand Up @@ -690,15 +696,26 @@ export async function setupZig(context: vscode.ExtensionContext) {
const watcher2 = vscode.workspace.createFileSystemWatcher("**/build.zig.zon");

const refreshZigInstallation = asyncDebounce(async () => {
if (!vscode.workspace.getConfiguration("zig").get<string>("path")) {
const zigPath = vscode.workspace.getConfiguration("zig").get<string>("path");
if (!zigPath) {
await installZig(context);
} else {
await updateStatus(context);
const result = zigProvider.resolveZigPathConfigOption(zigPath);
if (result) {
zigProvider.set(result);
}
}
await updateStatus(context);
}, 200);

if (!vscode.workspace.getConfiguration("zig").get<string>("path")) {
const zigPath = vscode.workspace.getConfiguration("zig").get<string>("path");
if (!zigPath) {
await installZig(context);
} else {
const result = zigProvider.resolveZigPathConfigOption(zigPath);
if (result) {
zigProvider.set(result);
}
}
await updateStatus(context);

Expand All @@ -724,10 +741,12 @@ export async function setupZig(context: vscode.ExtensionContext) {
if (change.affectsConfiguration("zig.path")) {
const result = zigProvider.resolveZigPathConfigOption();
if (result === undefined) return; // error message already reported
if (result !== null) {
if (result === null) {
void refreshZigInstallation();
} else {
zigProvider.set(result);
void updateStatus(context);
}
void refreshZigInstallation();
}
}),
vscode.window.onDidChangeActiveTextEditor(onDidChangeActiveTextEditor),
Expand Down
16 changes: 8 additions & 8 deletions src/zigUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,9 @@ export function resolveExePathAndVersion(
};
}

const exePath = which.sync(cmd, { nothrow: true });
if (!exePath) {
if (!isAbsolute) {
return { message: `Could not find '${cmd}' in PATH.` };
}
let exePath: string | null = null;

if (isAbsolute) {
const stats = fs.statSync(cmd, { throwIfNoEntry: false });
if (!stats) {
return {
Expand All @@ -103,9 +100,12 @@ export function resolveExePathAndVersion(
};
}

return {
message: `'${cmd}' is not an executable.`,
};
exePath = cmd;
} else {
exePath = which.sync(cmd, { nothrow: true });
if (!exePath) {
return { message: `Could not find '${cmd}' in PATH.` };
}
}

const version = getVersion(exePath, versionArg);
Expand Down
7 changes: 1 addition & 6 deletions src/zls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,9 @@ async function getZLSPath(context: vscode.ExtensionContext): Promise<{ exe: stri
const result = resolveExePathAndVersion(zlsExePath, "--version");
if ("message" in result) {
vscode.window
.showErrorMessage(`Unexpected 'zig.zls.path': ${result.message}`, "install ZLS", "open settings")
.showErrorMessage(`Unexpected 'zig.zls.path': ${result.message}`, "open settings")
.then(async (response) => {
switch (response) {
case "install ZLS":
const zlsConfig = vscode.workspace.getConfiguration("zig.zls");
await workspaceConfigUpdateNoThrow(zlsConfig, "enabled", "on", true);
await workspaceConfigUpdateNoThrow(zlsConfig, "path", undefined);
break;
case "open settings":
await vscode.commands.executeCommand("workbench.action.openSettings", "zig.zls.path");
break;
Expand Down