vscode-bazel icon indicating copy to clipboard operation
vscode-bazel copied to clipboard

${workspaceFolder} is not respected in buildifierExecutable location

Open Toxicable opened this issue 4 years ago • 3 comments

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

Toxicable avatar Sep 24 '19 06:09 Toxicable

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.

allevato avatar Sep 24 '19 15:09 allevato

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

allevato avatar Sep 24 '19 15:09 allevato

Looks like that issue has been closed as a duplicate of https://github.com/microsoft/vscode/issues/46471

lencioni avatar Aug 17 '21 13:08 lencioni

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 avatar Nov 04 '23 14:11 Tom-Newton

@Tom-Newton does #350 which also addresses #329 solve the issue for you?

vogelsgesang avatar Feb 29 '24 04:02 vogelsgesang

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 {

lpulley avatar Feb 29 '24 19:02 lpulley

@Tom-Newton does #350 which also addresses #329 solve the issue for you?

Yes, this works for me. Thank you :slightly_smiling_face:

Tom-Newton avatar Feb 29 '24 20:02 Tom-Newton