Skip to content

Commit

Permalink
don't set the zig.path config option to an invalid value
Browse files Browse the repository at this point in the history
The "Manually Specify Path" option could previously be used to set `zig.path` to an invalid value. This commit change is so that only an error message is displayed while leaving `zig.path` unchanged.
  • Loading branch information
Techatrix committed Dec 9, 2024
1 parent 611c802 commit dd70be0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
23 changes: 19 additions & 4 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,15 +38,31 @@ 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");
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].fsPath, true);
await zigProvider.setAndSave(uris[0].fsPath);
break;
default:
const version = new semver.SemVer(selection.detail ?? selection.label);
Expand Down

0 comments on commit dd70be0

Please sign in to comment.