vscode-bazel
vscode-bazel copied to clipboard
${workspaceFolder} is not respected in buildifierExecutable location
In your settings.json
"bazel.buildifierExecutable": "/home/f.wiles/wksp/node_modules/.bin/buildifier",
"bazel.buildifierExecutable": "${workspaceFolder}/node_modules/.bin/buildifier",
the first one works, the second however throws an error:
Error: Command failed: ${workspaceFolder}/node_modules/.bin/buildifier --mode=fix --type=build --lint=fix /bin/sh: 1: /node_modules/.bin/buildifier: not found
This is because we don't use the vscode.Task
API to launch buildifier, but instead spawn it directly with Node's child_process
.
Unfortunately, I don't think there's a clean way to resolve this yet:
- There doesn't appear to be an API that lets us manually perform the same substitutions for variables that would occur in a task invocation so that we can use it in a
child_process
call (unless I've missed it somewhere). - We can't switch to
vscode.Task
for buildifier because we need finer control over the process, like capturing the output.
We could hack it by looking for a couple of specific strings like ${workspaceFolder}
but I really don't like that approach because it deceives the user into thinking that the full set of task variable substitutions are available when they're really not.
It looks like there's an open feature request on VS Code to expose an API for manual variable resolution: https://github.com/microsoft/vscode/issues/70769
Looks like that issue has been closed as a duplicate of https://github.com/microsoft/vscode/issues/46471
Is there any update on this? It would be very useful. https://github.com/microsoft/vscode/issues/46471#issuecomment-762773662 sounds like it might unblock this?
@Tom-Newton does #350 which also addresses #329 solve the issue for you?
It looks like it should; we've been using this patch on 0.9.0:
diff --git a/src/buildifier/buildifier.ts b/src/buildifier/buildifier.ts
index c9192a5..e07b8d9 100644
--- a/src/buildifier/buildifier.ts
+++ b/src/buildifier/buildifier.ts
@@ -188,9 +188,13 @@ function executeBuildifier(
acceptNonSevereErrors: boolean,
): Promise<{ stdout: string; stderr: string }> {
return new Promise((resolve, reject) => {
- const execOptions = {
+ let execOptions = {
maxBuffer: Number.MAX_SAFE_INTEGER,
};
+ const workspaceFolders = vscode.workspace.workspaceFolders;
+ if (workspaceFolders) {
+ execOptions["cwd"] = workspaceFolders[0].uri.fsPath;
+ }
const process = child_process.execFile(
getDefaultBuildifierExecutablePath(),
args,
diff --git a/src/buildifier/buildifier_availability.ts b/src/buildifier/buildifier_availability.ts
index 9821545..a0ac7ce 100644
--- a/src/buildifier/buildifier_availability.ts
+++ b/src/buildifier/buildifier_availability.ts
@@ -39,10 +39,15 @@ export async function checkBuildifierIsAvailable() {
// If we found it, make sure it's a compatible version by running
// buildifier on an empty input and see if it exits successfully and the
// output parses.
+ let execOptions = {}
+ const workspaceFolders = vscode.workspace.workspaceFolders;
+ if (workspaceFolders) {
+ execOptions["cwd"] = workspaceFolders[0].uri.fsPath;
+ }
const process = child_process.execFile(
buildifierExecutable,
["--format=json", "--mode=check"],
- {},
+ execOptions,
(error: Error, stdout: string) => {
if (!error && stdout) {
try {
@Tom-Newton does #350 which also addresses #329 solve the issue for you?
Yes, this works for me. Thank you :slightly_smiling_face: