diff --git a/cargo/private/cargo_build_script.bzl b/cargo/private/cargo_build_script.bzl index 2644cdc2b6..63df45d54a 100644 --- a/cargo/private/cargo_build_script.bzl +++ b/cargo/private/cargo_build_script.bzl @@ -348,9 +348,16 @@ def _cargo_build_script_impl(ctx): env = dict({}) + if ctx.attr.use_default_shell_env == -1: + use_default_shell_env = ctx.attr._default_use_default_shell_env[BuildSettingInfo].value + elif ctx.attr.use_default_shell_env == 0: + use_default_shell_env = False + else: + use_default_shell_env = True + # 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: + if use_default_shell_env: env.update(ctx.configuration.default_shell_env) env.update({ @@ -537,9 +544,7 @@ def _cargo_build_script_impl(ctx): progress_message = "Running Cargo build script {}".format(pkg_name), env = env, toolchain = None, - # 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, + use_default_shell_env = use_default_shell_env, ) return [ @@ -628,13 +633,14 @@ cargo_build_script = rule( allow_files = True, cfg = "exec", ), - "use_default_shell_env": attr.bool( + "use_default_shell_env": attr.int( 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, + default = -1, + values = [-1, 0, 1], ), "version": attr.string( doc = "The semantic version (semver) of the crate", @@ -654,6 +660,9 @@ cargo_build_script = rule( "_debug_std_streams_output_group": attr.label( default = Label("//cargo/settings:debug_std_streams_output_group"), ), + "_default_use_default_shell_env": attr.label( + default = Label("//cargo/settings:use_default_shell_env"), + ), "_experimental_symlink_execroot": attr.label( default = Label("//cargo/settings:experimental_symlink_execroot"), ), diff --git a/cargo/private/cargo_build_script_wrapper.bzl b/cargo/private/cargo_build_script_wrapper.bzl index c42f8819db..54446c0d85 100644 --- a/cargo/private/cargo_build_script_wrapper.bzl +++ b/cargo/private/cargo_build_script_wrapper.bzl @@ -10,6 +10,7 @@ load( load("//rust:defs.bzl", "rust_binary") def cargo_build_script( + *, name, edition = None, crate_name = None, @@ -21,6 +22,7 @@ def cargo_build_script( link_deps = [], proc_macro_deps = [], build_script_env = {}, + use_default_shell_env = None, data = [], compile_data = [], tools = [], @@ -107,6 +109,8 @@ def cargo_build_script( links attribute and therefore provide environment variables to this build script. proc_macro_deps (list of label, optional): List of rust_proc_macro targets used to build the script. build_script_env (dict, optional): Environment variables for build scripts. + use_default_shell_env (bool, optional): Whether or not to include the default shell environment for the build script action. If unset the global + setting `@rules_rust//cargo/settings:use_default_shell_env` will be used to determine this value. data (list, optional): Files needed by the build script. compile_data (list, optional): Files needed for the compilation of the build script. tools (list, optional): Tools (executables) needed by the build script. @@ -191,6 +195,13 @@ def cargo_build_script( **wrapper_kwargs ) + if use_default_shell_env == None: + sanitized_use_default_shell_env = -1 + elif type(use_default_shell_env) == "bool": + sanitized_use_default_shell_env = 1 if use_default_shell_env else 0 + else: + sanitized_use_default_shell_env = use_default_shell_env + # This target executes the build script. _build_script_run( name = name, @@ -198,6 +209,7 @@ def cargo_build_script( crate_features = crate_features, version = version, build_script_env = build_script_env, + use_default_shell_env = sanitized_use_default_shell_env, links = links, deps = deps, link_deps = link_deps, diff --git a/cargo/settings/BUILD.bazel b/cargo/settings/BUILD.bazel index efee58e6d2..63ca4b5dc2 100644 --- a/cargo/settings/BUILD.bazel +++ b/cargo/settings/BUILD.bazel @@ -34,3 +34,10 @@ bool_flag( name = "debug_std_streams_output_group", build_setting_default = False, ) + +# A flag which controls the global default of `ctx.actions.run.use_default_shell_env` +# for `cargo_build_script` targets. +bool_flag( + name = "use_default_shell_env", + build_setting_default = True, +) diff --git a/crate_universe/private/crate.bzl b/crate_universe/private/crate.bzl index afbe874f3c..d4066c9725 100644 --- a/crate_universe/private/crate.bzl +++ b/crate_universe/private/crate.bzl @@ -97,6 +97,7 @@ def _annotation( build_script_rundir = None, build_script_rustc_env = None, build_script_toolchains = None, + build_script_use_default_shell_env = None, compile_data = None, compile_data_glob = None, crate_features = None, @@ -140,6 +141,8 @@ def _annotation( build_script_rustc_env (dict, optional): Additional environment variables to set on a crate's `cargo_build_script::env` attribute. build_script_toolchains (list, optional): A list of labels to set on a crates's `cargo_build_script::toolchains` attribute. + build_script_use_default_shell_env (int, optional): Whether or not to include the default shell environment for the build + script action. compile_data (list, optional): A list of labels to add to a crate's `rust_library::compile_data` attribute. compile_data_glob (list, optional): A list of glob patterns to add to a crate's `rust_library::compile_data` attribute. @@ -197,6 +200,7 @@ def _annotation( build_script_rundir = build_script_rundir, build_script_rustc_env = build_script_rustc_env, build_script_toolchains = _stringify_list(build_script_toolchains), + build_script_use_default_shell_env = build_script_use_default_shell_env, compile_data = _stringify_list(compile_data), compile_data_glob = compile_data_glob, crate_features = crate_features, diff --git a/crate_universe/src/config.rs b/crate_universe/src/config.rs index 92a4a21e71..83106c330e 100644 --- a/crate_universe/src/config.rs +++ b/crate_universe/src/config.rs @@ -303,6 +303,10 @@ pub(crate) struct CrateAnnotations { /// [toolchains](https://bazel.build/reference/be/common-definitions#common-attributes) attribute. pub(crate) build_script_toolchains: Option>, + /// Additional rustc_env flags to pass to a build script's + /// [use_default_shell_env](https://bazelbuild.github.io/rules_rust/cargo.html#cargo_build_script-use_default_shell_env) attribute. + pub(crate) build_script_use_default_shell_env: Option, + /// Directory to run the crate's build script in. If not set, will run in the manifest directory, otherwise a directory relative to the exec root. pub(crate) build_script_rundir: Option>, @@ -398,6 +402,7 @@ impl Add for CrateAnnotations { build_script_env: select_merge(self.build_script_env, rhs.build_script_env), build_script_rustc_env: select_merge(self.build_script_rustc_env, rhs.build_script_rustc_env), build_script_toolchains: joined_extra_member!(self.build_script_toolchains, rhs.build_script_toolchains, BTreeSet::new, BTreeSet::extend), + build_script_use_default_shell_env: self.build_script_use_default_shell_env.or(rhs.build_script_use_default_shell_env), build_script_rundir: self.build_script_rundir.or(rhs.build_script_rundir), additive_build_file_content: joined_extra_member!(self.additive_build_file_content, rhs.additive_build_file_content, String::new, concat_string), shallow_since: self.shallow_since.or(rhs.shallow_since), diff --git a/crate_universe/src/context/crate_context.rs b/crate_universe/src/context/crate_context.rs index 519ea8776d..8cc8e0bdca 100644 --- a/crate_universe/src/context/crate_context.rs +++ b/crate_universe/src/context/crate_context.rs @@ -242,6 +242,9 @@ pub(crate) struct BuildScriptAttributes { #[serde(skip_serializing_if = "BTreeSet::is_empty")] pub(crate) toolchains: BTreeSet