Skip to content
This repository was archived by the owner on Nov 25, 2025. It is now read-only.

Commit d881ff7

Browse files
committed
more accurately resolve the workspace folder in handleConfigOption
These changes may matter when there are none or multiple workspace folders.
1 parent 1002350 commit d881ff7

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/zigDiagnosticsProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export default class ZigDiagnosticsProvider {
160160
if (!buildFilePath) break;
161161
processArg.push("--build-file");
162162
try {
163-
processArg.push(path.resolve(handleConfigOption(buildFilePath)));
163+
processArg.push(path.resolve(handleConfigOption(buildFilePath, workspaceFolder)));
164164
} catch {
165165
//
166166
}

src/zigUtil.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,20 @@ import which from "which";
1414
* Replace any references to predefined variables in config string.
1515
* https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables
1616
*/
17-
export function handleConfigOption(input: string): string {
17+
export function handleConfigOption(input: string, workspaceFolder: vscode.WorkspaceFolder | "none" | "guess"): string {
1818
if (input.includes("${userHome}")) {
1919
input = input.replaceAll("${userHome}", os.homedir());
2020
}
2121

22-
if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
23-
input = input.replaceAll("${workspaceFolder}", vscode.workspace.workspaceFolders[0].uri.fsPath);
24-
input = input.replaceAll("${workspaceFolderBasename}", vscode.workspace.workspaceFolders[0].name);
22+
if (workspaceFolder === "guess") {
23+
workspaceFolder = vscode.workspace.workspaceFolders?.length ? vscode.workspace.workspaceFolders[0] : "none";
24+
}
25+
26+
if (workspaceFolder !== "none") {
27+
input = input.replaceAll("${workspaceFolder}", workspaceFolder.uri.fsPath);
28+
input = input.replaceAll("${workspaceFolderBasename}", workspaceFolder.name);
29+
} else {
30+
// This may end up reporting a confusing error message.
2531
}
2632

2733
const document = vscode.window.activeTextEditor?.document;
@@ -68,7 +74,7 @@ export function resolveExePathAndVersion(
6874
assert(cmd.length);
6975

7076
// allow passing predefined variables
71-
cmd = handleConfigOption(cmd);
77+
cmd = handleConfigOption(cmd, "guess");
7278

7379
if (cmd.startsWith("~")) {
7480
cmd = path.join(os.homedir(), cmd.substring(1));

src/zls.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ function configurationMiddleware(params: ConfigurationParams): LSPAny[] | Respon
170170

171171
const scopeUri = param.scopeUri ? client?.protocol2CodeConverter.asUri(param.scopeUri) : undefined;
172172
const configuration = vscode.workspace.getConfiguration("zig", scopeUri);
173+
const workspaceFolder = scopeUri ? vscode.workspace.getWorkspaceFolder(scopeUri) : undefined;
173174

174175
const updateConfigOption = (section: string, value: unknown) => {
175176
if (section === "zls.zigExePath") {
@@ -178,7 +179,7 @@ function configurationMiddleware(params: ConfigurationParams): LSPAny[] | Respon
178179

179180
if (typeof value === "string") {
180181
// Make sure that `""` gets converted to `undefined` and resolve predefined values
181-
value = value ? handleConfigOption(value) : undefined;
182+
value = value ? handleConfigOption(value, workspaceFolder ?? "guess") : undefined;
182183
} else if (typeof value === "object" && value !== null && !Array.isArray(value)) {
183184
// Recursively update the config options
184185
const newValue: Record<string, unknown> = {};

0 commit comments

Comments
 (0)