Skip to content

Conversation

RyanTorok
Copy link

@RyanTorok RyanTorok commented Sep 10, 2025

Some Linux distributions, such as NixOS, do not provide /bin/bash. This PR aims to make the LLVM toolchain compatible with these platforms by only using POSIX-compatible features in the shell script and replacing the shebang with #!/bin/sh.

Prior to this change, attempting to use the LLVM toolchain to build a C/C++ target on NixOS causes an error like:

src/main/tools/linux-sandbox-pid1.cc:548: "execvp(external/toolchains_llvm~~llvm~llvm_toolchain/bin/cc_wrapper.sh, 0x1ce8a310)": No such file or directory

where the No such file or directory error is referring to /bin/bash, not the cc_wrapper.sh script itself. In fact, trying to run bazel-REPONAME/external/toolchains_llvm~~llvm~llvm_toolchain/bin/cc_wrapper.sh manually afterwards gives a Required file not found error.

This PR emerged out of a discussion on #543, where it was noticed that replacing the #!/bin/bash shebang with #!/usr/bin/env bash causes issues when the calling rule does not include /usr/bin in PATH (rules_rust hits this case).

This PR does not update the corresponding MacOS script, as all MacOS platforms should have /bin/bash.

Supercedes #543.

@RyanTorok RyanTorok changed the title [wrapper] Make cc_wrapper.sh POSIX-compliant and use /bin/sh [wrapper] Make cc_wrapper.sh POSIX-compliant and use #!/bin/sh Sep 10, 2025
@RyanTorok RyanTorok changed the title [wrapper] Make cc_wrapper.sh POSIX-compliant and use #!/bin/sh [wrapper] Make cc_wrapper.sh POSIX-compliant and use #!/bin/sh Sep 10, 2025
@RyanTorok RyanTorok marked this pull request as ready for review September 10, 2025 02:25
@fmeum fmeum requested a review from Copilot September 10, 2025 05:38
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR makes the cc_wrapper.sh shell script POSIX-compliant to support Linux distributions like NixOS that don't provide /bin/bash. The change replaces bash-specific features with POSIX shell equivalents while maintaining the same functionality.

Key changes:

  • Replace #!/bin/bash shebang with #!/bin/sh
  • Convert bash arrays and associative constructs to POSIX-compatible alternatives
  • Replace bash-specific syntax with POSIX shell equivalents

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@fmeum
Copy link
Member

fmeum commented Sep 10, 2025

This is a case in which Copilot comments are highly relevant and much more thorough than what I could provide. Please take a look.

@RyanTorok
Copy link
Author

RyanTorok commented Sep 11, 2025

This is a case in which Copilot comments are highly relevant and much more thorough than what I could provide. Please take a look.

Copilot was quick to remind me that POSIX shells don't support the same nice syntax for regular expressions as bash. After fixing the issues it pointed out, I was able to get several of the test scripts to pass in an Ubuntu container, so hopefully CI will pass this time :)

Some Linux distributions, such as NixOS, do not provide /bin/bash. This
commit aims to make the LLVM toolchain compatible with these platforms
by only using POSIX-compatible features in the shell script and
replacing the shebang with #!/bin/sh .

This commit does not update the corresponding MacOS script, as all MacOS
builds should have /bin/bash.

Supercedes bazel-contrib#543.
@RyanTorok
Copy link
Author

RyanTorok commented Sep 17, 2025

Trying to rewrite cc_wrapper.sh.tpl to be POSIX-compliant caused too many bugs, and more importantly, required eliding a bunch of safety checks. The original script uses arrays and regular expressions everywhere, and replacing these with POSIX-compatible equivalents was very unidiomatic and unsafe.

As an alternative, I moved the current cc_wrapper.sh.tpl to cc_wrapper_inner.sh.tpl, and created a new cc_wrapper.sh.tpl that searches for an instance of bash on the host, either directly at /bin/bash or through /usr/bin/env/PATH. Once a valid instance of bash is found, it is used to call cc_wrapper_inner.sh.

@RyanTorok
Copy link
Author

RyanTorok commented Sep 29, 2025

Can a maintainer please re-run CI on this PR?

@helly25
Copy link
Collaborator

helly25 commented Oct 2, 2025

Can a maintainer please re-run CI on this PR?

I checked the errors and it appears that the new inner script is still not present. So somewhere it is not being copied as it needs to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants