Skip to content

Commit

Permalink
do not use empty string to indicate lookup in PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Apr 5, 2024
1 parent dfbeb49 commit 9d3d86b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
"null"
],
"default": null,
"description": "Set a custom path to the Zig binary. Empty string will lookup zig in PATH."
"description": "Set a custom path to the Zig binary. The string \"zig\" means lookup zig in PATH."
},
"zig.checkForUpdate": {
"scope": "resource",
Expand Down Expand Up @@ -173,7 +173,7 @@
"null"
],
"default": null,
"description": "Path to `zls` executable. Example: `C:/zls/zig-cache/bin/zls.exe`. Empty string will lookup ZLS in PATH.",
"description": "Path to `zls` executable. Example: `C:/zls/zig-cache/bin/zls.exe`. The string \"zls\" means lookup ZLS in PATH.",
"format": "path"
},
"zig.zls.enableSnippets": {
Expand Down
32 changes: 22 additions & 10 deletions src/zigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,8 @@ async function checkUpdate(context: vscode.ExtensionContext) {
async function getUpdatedVersion(context: vscode.ExtensionContext): Promise<ZigVersion | null> {
const configuration = vscode.workspace.getConfiguration("zig");
const zigPath = configuration.get<string | null>("path", null);
if (zigPath) {
const zigBinPath = vscode.Uri.joinPath(context.globalStorageUri, "zig_install", "zig").fsPath;
if (!zigPath.startsWith(zigBinPath)) return null;
} else {
return null;
}
const zigBinPath = vscode.Uri.joinPath(context.globalStorageUri, "zig_install", "zig").fsPath;
if (!zigPath?.startsWith(zigBinPath)) return null;

const curVersion = getVersion(zigPath, "version");
if (!curVersion) return null;
Expand Down Expand Up @@ -222,13 +218,29 @@ export async function setupZig(context: vscode.ExtensionContext) {
await checkUpdate(context);
});

{
// convert an empty string for `zig.path` and `zig.zls.path` to `zig` and `zls` respectively.
// This check can be removed once enough time has passed so that most users switched to the new value

const zigConfig = vscode.workspace.getConfiguration("zig");
const initialSetupDone = zigConfig.get<boolean>("initialSetupDone", false);
const zigPath = zigConfig.get<string | null>("path");
if (zigPath === "" || (initialSetupDone && zigPath === null)) {
await zigConfig.update("path", "zig", true);
}

const zlsConfig = vscode.workspace.getConfiguration("zig.zls");
const zlsPath = zlsConfig.get<string | null>("path");
if (zlsPath === "" || (initialSetupDone && zlsPath === null)) {
await zlsConfig.update("path", "zls", true);
}
}

const configuration = vscode.workspace.getConfiguration("zig");
if (!configuration.get<boolean>("initialSetupDone")) {
await configuration.update("initialSetupDone", await initialSetup(context), true);
}

// The `path` value may have changed after the initial setup is done but it will not need updating
if (!configuration.get<string | null>("path", null)) return; // also returns on ""
if (!configuration.get<boolean>("checkForUpdate")) return;
if (!(await shouldCheckUpdate(context, "zigUpdate"))) return;
await checkUpdate(context);
Expand Down Expand Up @@ -269,7 +281,7 @@ async function initialSetup(context: vscode.ExtensionContext): Promise<boolean>
await zigConfig.update("path", uris[0].path, true);
break;
case "Use Zig in PATH":
await zigConfig.update("path", "", true);
await zigConfig.update("path", "zig", true);
break;
case undefined:
await zigConfig.update("path", undefined, true);
Expand Down Expand Up @@ -303,7 +315,7 @@ async function initialSetup(context: vscode.ExtensionContext): Promise<boolean>

await zlsConfig.update("path", uris[0].path, true);
case "Use ZLS in PATH":
await zlsConfig.update("path", "", true);
await zlsConfig.update("path", "zls", true);
break;
case undefined:
// explicitly set `zig.zls.path` to null so it is visible in the `settings.json`
Expand Down
7 changes: 4 additions & 3 deletions src/zigUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function getExePath(exePath: string | null, exeName: string, optionName:
}
}

if (!exePath) {
if (exePath === null) {
exePath = which.sync(exeName, { nothrow: true });
} else if (exePath.startsWith("~")) {
exePath = path.join(os.homedir(), exePath.substring(1));
Expand All @@ -30,7 +30,7 @@ export function getExePath(exePath: string | null, exeName: string, optionName:
}

let message;
if (!exePath) {
if (exePath === null) {
message = `Could not find ${exeName} in PATH`;
} else if (!fs.existsSync(exePath)) {
message = `\`${optionName}\` ${exePath} does not exist`;
Expand All @@ -49,7 +49,8 @@ export function getExePath(exePath: string | null, exeName: string, optionName:
export function getZigPath(): string {
const configuration = vscode.workspace.getConfiguration("zig");
const zigPath = configuration.get<string | null>("path", null);
return getExePath(zigPath, "zig", "zig.path");
const exePath = zigPath !== "zig" ? zigPath : null; // the string "zig" means lookup in PATH
return getExePath(exePath, "zig", "zig.path");
}

// Check timestamp `key` to avoid automatically checking for updates
Expand Down
3 changes: 2 additions & 1 deletion src/zls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ export async function stopClient() {
export function getZLSPath(): string {
const configuration = vscode.workspace.getConfiguration("zig.zls");
const zlsPath = configuration.get<string | null>("path", null);
return getExePath(zlsPath, "zls", "zig.zls.path");
const exePath = zlsPath !== "zls" ? zlsPath : null; // the string "zls" means lookup in PATH
return getExePath(exePath, "zls", "zig.zls.path");
}

const downloadsRoot = "https://zigtools-releases.nyc3.digitaloceanspaces.com/zls";
Expand Down

0 comments on commit 9d3d86b

Please sign in to comment.