Skip to content

Commit

Permalink
allow setting zig.path and zig.zls.path to null
Browse files Browse the repository at this point in the history
`zig.path` and `zig.zls.path` default to an empty string which makes it
impossible to differentiate between an unset value and lookup in PATH.

The following issues will also be resolved:
- initial setup asks about Zig and ZLS even when using lookup in PATH
- initial setup returns early after installing Zig
- `checkInstalled` would error when ZLS is used from PATH
  • Loading branch information
Techatrix committed Apr 5, 2024
1 parent eb2453f commit dfbeb49
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 17 deletions.
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@
},
"zig.path": {
"scope": "machine-overridable",
"type": "string",
"default": "",
"type": [
"string",
"null"
],
"default": null,
"description": "Set a custom path to the Zig binary. Empty string will lookup zig in PATH."
},
"zig.checkForUpdate": {
Expand Down Expand Up @@ -165,8 +168,11 @@
},
"zig.zls.path": {
"scope": "machine-overridable",
"type": "string",
"default": "",
"type": [
"string",
"null"
],
"default": null,
"description": "Path to `zls` executable. Example: `C:/zls/zig-cache/bin/zls.exe`. Empty string will lookup ZLS in PATH.",
"format": "path"
},
Expand Down
14 changes: 9 additions & 5 deletions src/zigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ 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>("path");
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;
Expand Down Expand Up @@ -227,7 +227,8 @@ export async function setupZig(context: vscode.ExtensionContext) {
await configuration.update("initialSetupDone", await initialSetup(context), true);
}

if (!configuration.get<string>("path")) return;
// 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 All @@ -236,7 +237,7 @@ export async function setupZig(context: vscode.ExtensionContext) {
async function initialSetup(context: vscode.ExtensionContext): Promise<boolean> {
const zigConfig = vscode.workspace.getConfiguration("zig");

if (zigConfig.get<string>("path") === "") {
if (zigConfig.get<string | null>("path", null) === null) {
const zigResponse = await vscode.window.showInformationMessage(
"Zig path hasn't been set, do you want to specify the path or install Zig?",
{ modal: true },
Expand All @@ -247,7 +248,7 @@ async function initialSetup(context: vscode.ExtensionContext): Promise<boolean>
switch (zigResponse) {
case "Install":
await selectVersionAndInstall(context);
const path = zigConfig.get<string>("path");
const path = vscode.workspace.getConfiguration("zig").get<string | null>("path", null);
if (!path) return false;
void vscode.window.showInformationMessage(
`Zig was installed at '${path}', add it to PATH to use it from the terminal`,
Expand All @@ -271,13 +272,14 @@ async function initialSetup(context: vscode.ExtensionContext): Promise<boolean>
await zigConfig.update("path", "", true);
break;
case undefined:
await zigConfig.update("path", undefined, true);
return false;
}
}

const zlsConfig = vscode.workspace.getConfiguration("zig.zls");

if (zlsConfig.get<string>("path") === "") {
if (zlsConfig.get<string | null>("path", null) === null) {
const zlsResponse = await vscode.window.showInformationMessage(
"We recommend enabling ZLS (the Zig Language Server) for a better editing experience. Would you like to install it?",
{ modal: true },
Expand All @@ -304,6 +306,8 @@ async function initialSetup(context: vscode.ExtensionContext): Promise<boolean>
await zlsConfig.update("path", "", true);
break;
case undefined:
// explicitly set `zig.zls.path` to null so it is visible in the `settings.json`
await zlsConfig.update("path", null, true);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/zigUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function getExePath(exePath: string | null, exeName: string, optionName:

export function getZigPath(): string {
const configuration = vscode.workspace.getConfiguration("zig");
const zigPath = configuration.get<string>("path") ?? null;
const zigPath = configuration.get<string | null>("path", null);
return getExePath(zigPath, "zig", "zig.path");
}

Expand Down
15 changes: 8 additions & 7 deletions src/zls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function stopClient() {
// returns the file system path to the zls executable
export function getZLSPath(): string {
const configuration = vscode.workspace.getConfiguration("zig.zls");
const zlsPath = configuration.get<string>("path") ?? null;
const zlsPath = configuration.get<string | null>("path", null);
return getExePath(zlsPath, "zls", "zig.zls.path");
}

Expand Down Expand Up @@ -127,7 +127,7 @@ async function getVersionIndex(): Promise<VersionIndex> {
// checks whether there is newer version on master
async function checkUpdate(context: vscode.ExtensionContext) {
const configuration = vscode.workspace.getConfiguration("zig.zls");
const zlsPath = configuration.get<string>("path");
const zlsPath = configuration.get<string | null>("path", null);
const zlsBinPath = vscode.Uri.joinPath(context.globalStorageUri, "zls_install", "zls").fsPath;
if (!zlsPath) return;
if (!zlsPath.startsWith(zlsBinPath)) return;
Expand Down Expand Up @@ -270,13 +270,14 @@ async function installVersion(context: vscode.ExtensionContext, version: semver.
}

function checkInstalled(): boolean {
const zlsPath = vscode.workspace.getConfiguration("zig.zls").get<string>("path");
if (!zlsPath) {
const zlsPath = vscode.workspace.getConfiguration("zig.zls").get<string | null>("path", null);
if (zlsPath === null) {
void vscode.window.showErrorMessage("This command cannot be run without setting 'zig.zls.path'.", {
modal: true,
});
return false;
}
return !!zlsPath;
return true;
}

export async function activate(context: vscode.ExtensionContext) {
Expand Down Expand Up @@ -323,14 +324,14 @@ export async function activate(context: vscode.ExtensionContext) {
) {
await stopClient();
const zlsConfig = vscode.workspace.getConfiguration("zig.zls");
if (!!zlsConfig.get<string>("path")) {
if (zlsConfig.get<string | null>("path", null) !== null) {
await startClient();
}
}
}, context.subscriptions);

const zlsConfig = vscode.workspace.getConfiguration("zig.zls");
if (zlsConfig.get<string>("path") === undefined) return;
if (zlsConfig.get<string | null>("path", null) === null) return;
if (zlsConfig.get<boolean>("checkForUpdate") && (await shouldCheckUpdate(context, "zlsUpdate"))) {
await checkUpdate(context);
}
Expand Down

0 comments on commit dfbeb49

Please sign in to comment.