Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo: Allow disabling the default_shell_env for build scripts #3020

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions cargo/private/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,12 @@ def _cargo_build_script_impl(ctx):

cc_toolchain = find_cpp_toolchain(ctx)

# Start with the default shell env, which contains any --action_env
# settings passed in on the command line.
env = dict(ctx.configuration.default_shell_env)
env = dict({})

# If enabled, start with the default shell env, which contains any --action_env
# settings passed in on the command line and defaults like $PATH.
if ctx.attr.use_default_shell_env:
env.update(ctx.configuration.default_shell_env)

env.update({
"CARGO_CRATE_NAME": name_to_crate_name(pkg_name),
Expand Down Expand Up @@ -534,9 +537,9 @@ def _cargo_build_script_impl(ctx):
progress_message = "Running Cargo build script {}".format(pkg_name),
env = env,
toolchain = None,
# Set use_default_shell_env so that $PATH is set, as tools like Cmake
# may want to probe $PATH for helper tools.
use_default_shell_env = True,
# If enabled, sets the $PATH environment variable so tools like `cmake`
# can probe $PATH for helper tools. Defaults to `True`.
use_default_shell_env = ctx.attr.use_default_shell_env,
)

return [
Expand Down Expand Up @@ -625,6 +628,14 @@ cargo_build_script = rule(
allow_files = True,
cfg = "exec",
),
"use_default_shell_env": attr.bool(
doc = dedent("""\
Whether or not to include the default shell environment for the build
script action. By default Bazel's `default_shell_env` is set for build
script actions so crates like `cmake` can probe $PATH to find tools.
"""),
default = True,
),
"version": attr.string(
doc = "The semantic version (semver) of the crate",
),
Expand Down