rules_go icon indicating copy to clipboard operation
rules_go copied to clipboard

Incompatible shebang for NixOS

Open calmette54 opened this issue 7 months ago • 4 comments

I tried running the example/basic_gazelle but I get errors related to the shebang that is being used (I'm using NixOS).

bazel test fails

 & nix-shell -p bazelisk
 &  bazelisk test :basic_gazelle_test
INFO: Analyzed target //:basic_gazelle_test (0 packages loaded, 0 targets configured).
ERROR: /home/user/.cache/bazel/_bazel_user/24ee5178e539cd7f765d94f612c1781f/external/rules_go++go_sdk+basic_gazelle__download_0/BUILD.bazel:76:15: GoToolchainBinaryBuild external/rules_go++go_sdk+basic_gazelle__download_0/builder [for tool] failed: (Exit 1): bash failed: error executing GoToolchainBinaryBuild command (from target @@rules_go++go_sdk+basic_gazelle__download_0//:builder) /bin/bash -c ... (remaining 1 argument skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
src/main/tools/process-wrapper-legacy.cc:80: "execvp(/bin/bash, ...)": No such file or directory
Target //:basic_gazelle_test failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.078s, Critical Path: 0.59s
INFO: 2 processes: 2 internal.
ERROR: Build did NOT complete successfully
//:basic_gazelle_test                                           FAILED TO BUILD

Executed 0 out of 1 test: 1 fails to build.

bazel run fails

 & nix-shell -p bazelisk
 &  bazelisk run :basic_gazelle
INFO: Analyzed target //:basic_gazelle (0 packages loaded, 0 targets configured).
ERROR: /home/user/.cache/bazel/_bazel_user/24ee5178e539cd7f765d94f612c1781f/external/rules_go++go_sdk+basic_gazelle__download_0/BUILD.bazel:76:15: GoToolchainBinaryBuild external/rules_go++go_sdk+basic_gazelle__download_0/builder [for tool] failed: (Exit 1): bash failed: error executing GoToolchainBinaryBuild command (from target @@rules_go++go_sdk+basic_gazelle__download_0//:builder) /bin/bash -c ... (remaining 1 argument skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
src/main/tools/process-wrapper-legacy.cc:80: "execvp(/bin/bash, ...)": No such file or directory
Target //:basic_gazelle failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.007s, Critical Path: 0.51s
INFO: 2 processes: 2 internal.
ERROR: Build did NOT complete successfully
ERROR: Build failed. Not running target

bazel run //:gazelle fails

 & nix-shell -p bazelisk
 &  bazelisk run //:gazelle
INFO: Analyzed target //:gazelle (0 packages loaded, 0 targets configured).
ERROR: /home/user/.cache/bazel/_bazel_user/24ee5178e539cd7f765d94f612c1781f/external/rules_go++go_sdk+basic_gazelle__download_0/BUILD.bazel:76:15: GoToolchainBinaryBuild external/rules_go++go_sdk+basic_gazelle__download_0/builder [for tool] failed: (Exit 1): bash failed: error executing GoToolchainBinaryBuild command (from target @@rules_go++go_sdk+basic_gazelle__download_0//:builder) /bin/bash -c ... (remaining 1 argument skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
src/main/tools/process-wrapper-legacy.cc:80: "execvp(/bin/bash, ...)": No such file or directory
Target //:gazelle failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.033s, Critical Path: 0.52s
INFO: 2 processes: 2 internal.
ERROR: Build did NOT complete successfully
ERROR: Build failed. Not running target

bazel run @rules_go//go -- mod tidy fails

 & nix-shell -p bazelisk
 &  bazelisk run @rules_go//go -- mod tidy
INFO: Analyzed target @@rules_go+//go:go (2 packages loaded, 12 targets configured).
ERROR: /home/user/.cache/bazel/_bazel_user/24ee5178e539cd7f765d94f612c1781f/external/rules_go++go_sdk+basic_gazelle__download_0/BUILD.bazel:76:15: GoToolchainBinaryBuild external/rules_go++go_sdk+basic_gazelle__download_0/builder [for tool] failed: (Exit 1): bash failed: error executing GoToolchainBinaryBuild command (from target @@rules_go++go_sdk+basic_gazelle__download_0//:builder) /bin/bash -c ... (remaining 1 argument skipped)

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
src/main/tools/process-wrapper-legacy.cc:80: "execvp(/bin/bash, ...)": No such file or directory
Target @@rules_go+//go/tools/go_bin_runner:go_bin_runner failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 3.731s, Critical Path: 3.14s
INFO: 5 processes: 5 internal.
ERROR: Build did NOT complete successfully
ERROR: Build failed. Not running target

calmette54 avatar May 20 '25 15:05 calmette54

Is there a portable shebang that would work? /usr/bin/env bash perhaps? Happy to review a PR.

fmeum avatar May 20 '25 15:05 fmeum

Yes @fmeum , that should work I think.

I'd love to help implement this fix but I don't quite understand how to.

I've tracked the process-wrapper-legacy.cc:80 but that is using a parameter to write the shebang.

I tried to traceback the call using the logs. /home/user/.cache/bazel/_bazel_user/24ee5178e539cd7f765d94f612c1781f/external/rules_go++go_sdk+basic_gazelle__download_0/BUILD.bazel:76:15: seems to correspond to go/private/BUILD.sdk.bazel:78. But that didn't help me either. So, I went to look at the implementation of go_tools_binary where lines 527-551 seem contain the following piece of code:

        ctx.actions.run_shell(
            command = cmd,
            tools = depset(
                ctx.files.srcs + [sdk.go],
                transitive = [sdk.headers, sdk.srcs, sdk.libs, sdk.tools],
            ),
            toolchain = None,
            outputs = [out],
            mnemonic = "GoToolchainBinaryBuild",
        )

This seems to be where the shell script is invoked but again I don't know where the shebang is actually passed as an argument.

calmette54 avatar May 20 '25 15:05 calmette54

Could you try to set --shell_executable to some other path?

fmeum avatar May 20 '25 15:05 fmeum

Ohhh this will help me solve so many problems!! I'm so grateful.

calmette54 avatar May 20 '25 16:05 calmette54

Is it possible to extract the script to a template file and then invoke it via run instead of run_shell, its a bit annoying to provide a path to --shell_executable

TroyKomodo avatar Oct 02 '25 00:10 TroyKomodo

Wouldn't we still have to find and invoke the shell in that case? Could you instead send a change to rules_shell that detects the shell binary on your system?

fmeum avatar Oct 02 '25 04:10 fmeum

We could depend on the tool chain in ruls_shell and replace #!/usr/bin/env bash with the tool chain provided path if some settings flag is enabled.

A perfect solution would be for bazel itself to use a tool chain but getting it back upstream is likely harder than it is to just fix it here.

TroyKomodo avatar Oct 02 '25 12:10 TroyKomodo

There's a proposal for doing this in Bazel, but it's tricky to add a toolchain dependency only if run_shell is used. I think that run_shell should become a Starlark wrapper around run that lives in rules_shell.

fmeum avatar Oct 02 '25 12:10 fmeum

I think aswell, this issue seems to only be a problem when running under processwrapper, before i was using the linux-sandbox and it just worked.

https://github.com/bazelbuild/bazel/issues/27099

TroyKomodo avatar Oct 02 '25 12:10 TroyKomodo