Skip to content

Commit

Permalink
Merge pull request #251 from ziglang/techatrix/fix-manual-path-set
Browse files Browse the repository at this point in the history
fix some issues when trying to manually set the path to Zig
  • Loading branch information
Vexu authored Dec 9, 2024
2 parents 366c82c + adef1d3 commit 0e2dac1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
25 changes: 20 additions & 5 deletions src/zigProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class ZigProvider implements vscode.Disposable {
if (change.affectsConfiguration("zig.path")) {
const newValue = this.resolveZigPathConfigOption();
if (newValue) {
this.value = newValue;
this.set(this.value);
}
}
Expand All @@ -39,20 +38,36 @@ export class ZigProvider implements vscode.Disposable {
return this.value?.exe ?? null;
}

/** Override which zig executable should be used. The `zig.path` config option will be ignored */
/** Set the path the Zig executable. The `zig.path` config option will be ignored */
public set(value: ExeWithVersion | null) {
this.value = value;
this.onChange.fire(value);
}

/**
* Set the path the Zig executable. Will be saved in `zig.path` config option.
*
* @param zigPath The path to the zig executable. If `null`, the `zig.path` config option will be removed.
*/
public async setAndSave(zigPath: string | null) {
if (!zigPath) {
await vscode.workspace.getConfiguration("zig").update("path", undefined, true);
return;
}
const newValue = this.resolveZigPathConfigOption(zigPath);
if (!newValue) return;
await vscode.workspace.getConfiguration("zig").update("path", newValue.exe, true);
this.set(newValue);
}

/** Resolves the `zig.path` configuration option */
private resolveZigPathConfigOption(): ExeWithVersion | null {
const zigPath = vscode.workspace.getConfiguration("zig").get<string>("path", "");
private resolveZigPathConfigOption(zigPath?: string): ExeWithVersion | null {
zigPath ??= vscode.workspace.getConfiguration("zig").get<string>("path", "");
if (!zigPath) return null;
const exePath = zigPath !== "zig" ? zigPath : null; // the string "zig" means lookup in PATH
const result = resolveExePathAndVersion(exePath, "zig", "zig.path", "version");
if ("message" in result) {
void vscode.window.showErrorMessage(`'zig.path' is not valid: ${result.message}`);
void vscode.window.showErrorMessage(result.message);
return null;
}
return result;
Expand Down
5 changes: 2 additions & 3 deletions src/zigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ async function installZig(context: vscode.ExtensionContext) {
Object.values(WantedZigVersionSource) as WantedZigVersionSource[],
);
if (!wantedZig) {
await vscode.workspace.getConfiguration("zig").update("path", undefined, true);
zigProvider.set(null);
await zigProvider.setAndSave(null);
return;
}

Expand Down Expand Up @@ -249,7 +248,7 @@ async function selectVersionAndInstall(context: vscode.ExtensionContext) {
title: "Select Zig executable",
});
if (!uris) return;
await vscode.workspace.getConfiguration("zig").update("path", uris[0].path, true);
await zigProvider.setAndSave(uris[0].fsPath);
break;
default:
const version = new semver.SemVer(selection.detail ?? selection.label);
Expand Down

0 comments on commit 0e2dac1

Please sign in to comment.