diff --git a/rust/extensions.bzl b/rust/extensions.bzl index 2ce6a5226a..751603953d 100644 --- a/rust/extensions.bzl +++ b/rust/extensions.bzl @@ -145,6 +145,7 @@ def _rust_impl(module_ctx): rustfmt_toolchain_triples = toolchain.rustfmt_toolchain_triples if toolchain.rustfmt_toolchain_triples else DEFAULT_TOOLCHAIN_TRIPLES, target_settings = [str(v) for v in toolchain.target_settings], extra_toolchain_infos = extra_toolchain_infos, + use_default_shell_env = use_default_shell_env, ) metadata_kwargs = {} if bazel_features.external_deps.extension_metadata_has_reproducible: @@ -284,6 +285,10 @@ _RUST_TOOLCHAIN_TAG = tag_class( ), default = _RUST_TOOLCHAIN_VERSIONS, ), + "use_default_shell_env": attr.bool( + doc = "Controls whether rustc actions inherit the shell environment", + default = False, + ), } | _COMMON_TAG_KWARGS, ) diff --git a/rust/private/repositories.bzl b/rust/private/repositories.bzl index 29bfb9bc1c..11a86cec79 100644 --- a/rust/private/repositories.bzl +++ b/rust/private/repositories.bzl @@ -72,7 +72,8 @@ def rust_register_toolchains( toolchain_triples = DEFAULT_TOOLCHAIN_TRIPLES, rustfmt_toolchain_triples = DEFAULT_TOOLCHAIN_TRIPLES, target_settings = [], - extra_toolchain_infos = None): + extra_toolchain_infos = None, + use_default_shell_env = False): """Emits a default set of toolchains for Linux, MacOS, and Freebsd Skip this macro and call the `rust_repository_set` macros directly if you need a compiler for \ @@ -112,6 +113,7 @@ def rust_register_toolchains( rustfmt_toolchain_triples (dict[str, str], optional): Like toolchain_triples, but for rustfmt toolchains. target_settings (list of labels as strings, optional): A list of `config_settings` that must be satisfied by the target configuration in order for this toolchain to be selected during toolchain resolution. extra_toolchain_infos: (dict[str, dict], optional): Mapping of information about extra toolchains which were created outside of this call, which should be added to the hub repo. + use_default_shell_env (bool, optional): Whether rustc actions inherit the shell environment (defaults to False). """ if not rustfmt_version: if len(versions) == 1: @@ -185,6 +187,7 @@ def rust_register_toolchains( urls = urls, versions = versions, aliases = dict(aliases), + use_default_shell_env = use_default_shell_env, ) for toolchain in _get_toolchain_repositories( @@ -318,6 +321,10 @@ _RUST_TOOLCHAIN_REPOSITORY_ATTRS = { doc = "The version of the tool among \"nightly\", \"beta\", or an exact version.", mandatory = True, ), + "use_default_shell_env": attr.bool( + doc = "Controls whether rustc actions inherit the shell environment", + default = False, + ), } def _include_llvm_tools(version, iso_date): @@ -480,6 +487,7 @@ def _rust_toolchain_tools_repository_impl(ctx): version = toolchain_version, channel = channel, iso_date = iso_date, + use_default_shell_env = ctx.attr.use_default_shell_env, )) # Not all target triples are expected to have dev components @@ -593,7 +601,8 @@ def rust_toolchain_repository( urls = DEFAULT_STATIC_RUST_URL_TEMPLATES, auth = None, netrc = None, - auth_patterns = None): + auth_patterns = None, + use_default_shell_env = None): """Assembles a remote repository for the given toolchain params, produces a proxy repository \ to contain the toolchain declaration, and registers the toolchains. @@ -628,6 +637,7 @@ def rust_toolchain_repository( See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. netrc (str, optional): .netrc file to use for authentication; mirrors the eponymous attribute from http_archive auth_patterns (list, optional): A list of patterns to match against urls for which the auth object should be used. + use_default_shell_env (bool, optional): Whether rustc actions inherit the shell environment (defaults to False). Returns: dict[str, str]: Information about the registerable toolchain created by this rule. @@ -659,6 +669,7 @@ def rust_toolchain_repository( auth = auth, netrc = netrc, auth_patterns = auth_patterns, + use_default_shell_env = use_default_shell_env, ) channel_target_settings = ["@rules_rust//rust/toolchain/channel:{}".format(channel)] if channel else [] @@ -995,7 +1006,8 @@ def rust_repository_set( exec_compatible_with = None, default_target_compatible_with = None, aliases = {}, - compact_windows_names = _COMPACT_WINDOWS_NAMES): + compact_windows_names = _COMPACT_WINDOWS_NAMES, + use_default_shell_env = False): """Assembles a remote repository for the given toolchain params, produces a proxy repository \ to contain the toolchain declaration. @@ -1033,6 +1045,7 @@ def rust_repository_set( aliases (dict): Replacement names to use for toolchains created by this macro. compact_windows_names (bool): Whether or not to produce compact repository names for windows toolchains. This is to avoid MAX_PATH issues. + use_default_shell_env (bool, optional): Whether rustc actions inherit the shell environment (defaults to False). Returns: dict[str, dict]: A dict of information about all generated toolchains. @@ -1082,6 +1095,7 @@ def rust_repository_set( auth = auth, netrc = netrc, auth_patterns = auth_patterns, + use_default_shell_env = use_default_shell_env, ) channel_target_settings = ["@rules_rust//rust/toolchain/channel:{}".format(toolchain.channel.name)] diff --git a/rust/private/repository_utils.bzl b/rust/private/repository_utils.bzl index c1084bf60d..6985e09353 100644 --- a/rust/private/repository_utils.bzl +++ b/rust/private/repository_utils.bzl @@ -372,6 +372,7 @@ rust_toolchain( channel = "{channel}", iso_date = {iso_date}, tags = ["rust_version={version}"], + use_default_shell_env = {use_default_shell_env}, ) """ @@ -393,7 +394,8 @@ def BUILD_for_rust_toolchain( extra_rustc_flags = None, extra_exec_rustc_flags = None, opt_level = None, - strip_level = None): + strip_level = None, + use_default_shell_env = False): """Emits a toolchain declaration to match an existing compiler and stdlib. Args: @@ -419,6 +421,7 @@ def BUILD_for_rust_toolchain( extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration. opt_level (dict, optional): Optimization level config for this toolchain. strip_level (dict, optional): Strip level config for this toolchain. + use_default_shell_env (bool, optional): Whether rustc actions inherit the shell environment (defaults to False). Returns: str: A rendered template of a `rust_toolchain` declaration @@ -484,6 +487,7 @@ def BUILD_for_rust_toolchain( version = version, channel = channel, iso_date = repr(iso_date), + use_default_shell_env = use_default_shell_env, ) _build_file_for_toolchain_template = """\ diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 3f03e895f6..4b0216168f 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1596,6 +1596,7 @@ def rustc_compile_action( ), toolchain = "@rules_rust//rust:toolchain_type", resource_set = get_rustc_resource_set(toolchain), + use_default_shell_env = toolchain.use_default_shell_env, ) if args_metadata: ctx.actions.run( @@ -1613,6 +1614,7 @@ def rustc_compile_action( "" if len(srcs) == 1 else "s", ), toolchain = "@rules_rust//rust:toolchain_type", + use_default_shell_env = toolchain.use_default_shell_env, ) elif hasattr(ctx.executable, "_bootstrap_process_wrapper"): # Run without process_wrapper @@ -1634,6 +1636,7 @@ def rustc_compile_action( ), toolchain = "@rules_rust//rust:toolchain_type", resource_set = get_rustc_resource_set(toolchain), + use_default_shell_env = toolchain.use_default_shell_env, ) else: fail("No process wrapper was defined for {}".format(ctx.label)) diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index 2db8da00f1..65d1cfa824 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -633,6 +633,7 @@ def _rust_toolchain_impl(ctx): target_triple = target_triple, version = ctx.attr.version, require_explicit_unstable_features = _require_explicit_unstable_features(ctx), + use_default_shell_env = ctx.attr.use_default_shell_env, # Experimental and incompatible flags _rename_first_party_crates = rename_first_party_crates, @@ -875,6 +876,10 @@ rust_toolchain = rule( doc = "The version of the Rust compiler (e.g. `1.94.1`).", default = "", ), + "use_default_shell_env": attr.bool( + doc = "Controls whether rustc actions inherit the shell environment", + default = False, + ), "_codegen_units": attr.label( default = Label("//rust/settings:codegen_units"), ),