From bd8f41f4be21b2143934f19f18018a7b216ebffc Mon Sep 17 00:00:00 2001 From: David Zbarsky Date: Fri, 17 Oct 2025 12:50:14 -0400 Subject: [PATCH] Allow opting-out of the 0.0.0 crate version fallback --- cargo/private/cargo_build_script.bzl | 9 ++----- rust/private/rustc.bzl | 39 ++++++++++++++++++++-------- rust/settings/BUILD.bazel | 3 +++ rust/settings/settings.bzl | 10 +++++++ rust/toolchain.bzl | 5 ++++ 5 files changed, 48 insertions(+), 18 deletions(-) diff --git a/cargo/private/cargo_build_script.bzl b/cargo/private/cargo_build_script.bzl index bce74b9510..f524ca3c1d 100644 --- a/cargo/private/cargo_build_script.bzl +++ b/cargo/private/cargo_build_script.bzl @@ -11,6 +11,7 @@ load("//rust:rust_common.bzl", "BuildInfo", "CrateGroupInfo", "DepInfo") # buildifier: disable=bzl-visibility load( "//rust/private:rustc.bzl", + "env_vars_from_version", "get_compilation_mode_opts", "get_linker_and_args", ) @@ -407,13 +408,7 @@ def _cargo_build_script_impl(ctx): }) if ctx.attr.version: - version = ctx.attr.version.split("+")[0].split(".") - patch = version[2].split("-") if len(version) > 2 else [""] - env["CARGO_PKG_VERSION_MAJOR"] = version[0] - env["CARGO_PKG_VERSION_MINOR"] = version[1] if len(version) > 1 else "" - env["CARGO_PKG_VERSION_PATCH"] = patch[0] - env["CARGO_PKG_VERSION_PRE"] = patch[1] if len(patch) > 1 else "" - env["CARGO_PKG_VERSION"] = ctx.attr.version + env.update(env_vars_from_version(ctx.attr.version)) # Pull in env vars which may be required for the cc_toolchain to work (e.g. on OSX, the SDK version). # We hope that the linker env is sufficient for the whole cc_toolchain. diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 105352a8b8..b1d26664ee 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -129,24 +129,40 @@ is_proc_macro_dep_enabled = rule( build_setting = config.bool(flag = True), ) -def _get_rustc_env(attr, toolchain, crate_name): - """Gathers rustc environment variables +def env_vars_from_version(version): + """Gathers rustc environment variables corresponding to the version Args: - attr (struct): The current target's attributes - toolchain (rust_toolchain): The current target's rust toolchain context - crate_name (str): The name of the crate to be compiled + version (str): The version of the crate to be compiled Returns: dict: Rustc environment variables """ - version = attr.version if hasattr(attr, "version") else "0.0.0" major, minor, patch = version.split(".", 2) if "-" in patch: patch, pre = patch.split("-", 1) else: pre = "" + return { + "CARGO_PKG_VERSION": version, + "CARGO_PKG_VERSION_MAJOR": major, + "CARGO_PKG_VERSION_MINOR": minor, + "CARGO_PKG_VERSION_PATCH": patch, + "CARGO_PKG_VERSION_PRE": pre, + } + +def _get_rustc_env(attr, toolchain, crate_name): + """Gathers rustc environment variables + + Args: + attr (struct): The current target's attributes + toolchain (rust_toolchain): The current target's rust toolchain context + crate_name (str): The name of the crate to be compiled + + Returns: + dict: Rustc environment variables + """ result = { "CARGO_CFG_TARGET_ARCH": "" if toolchain.target_arch == None else toolchain.target_arch, "CARGO_CFG_TARGET_OS": "" if toolchain.target_os == None else toolchain.target_os, @@ -155,12 +171,13 @@ def _get_rustc_env(attr, toolchain, crate_name): "CARGO_PKG_DESCRIPTION": "", "CARGO_PKG_HOMEPAGE": "", "CARGO_PKG_NAME": attr.name, - "CARGO_PKG_VERSION": version, - "CARGO_PKG_VERSION_MAJOR": major, - "CARGO_PKG_VERSION_MINOR": minor, - "CARGO_PKG_VERSION_PATCH": patch, - "CARGO_PKG_VERSION_PRE": pre, } + + version_default = None if toolchain._incompatible_do_not_inject_degenerate_version_to_rustc_env else "0.0.0" + version = getattr(attr, "version", version_default) + if version: + result.update(env_vars_from_version(version)) + if hasattr(attr, "_is_proc_macro_dep_enabled") and attr._is_proc_macro_dep_enabled[IsProcMacroDepEnabledInfo].enabled: is_proc_macro_dep = "0" if hasattr(attr, "_is_proc_macro_dep") and attr._is_proc_macro_dep[IsProcMacroDepInfo].is_proc_macro_dep: diff --git a/rust/settings/BUILD.bazel b/rust/settings/BUILD.bazel index a93b91cc64..365d6e8a85 100644 --- a/rust/settings/BUILD.bazel +++ b/rust/settings/BUILD.bazel @@ -27,6 +27,7 @@ load( "incompatible_change_clippy_error_format", "incompatible_change_rust_test_compilation_output_directory", "incompatible_do_not_include_data_in_compile_data", + "incompatible_do_not_inject_degenerate_version_to_rustc_env", "lto", "no_std", "pipelined_compilation", @@ -111,6 +112,8 @@ incompatible_change_rust_test_compilation_output_directory() incompatible_do_not_include_data_in_compile_data() +incompatible_do_not_inject_degenerate_version_to_rustc_env() + lto() no_std() diff --git a/rust/settings/settings.bzl b/rust/settings/settings.bzl index abedffa4c0..e3bf101f1c 100644 --- a/rust/settings/settings.bzl +++ b/rust/settings/settings.bzl @@ -478,6 +478,16 @@ def incompatible_do_not_include_data_in_compile_data(): issue = "https://github.com/bazelbuild/rules_rust/issues/2977", ) +# buildifier: disable=unnamed-macro +def incompatible_do_not_inject_degenerate_version_to_rustc_env(): + """A flag to control whether we inject env vars corresponding to version 0.0.0 if version is omitted. + """ + incompatible_flag( + name = "incompatible_do_not_inject_degenerate_version_to_rustc_env", + build_setting_default = False, + issue = "https://github.com/bazelbuild/rules_rust/issues/3674", + ) + def codegen_units(): """The default value for `--codegen-units` which also affects resource allocation for rustc actions. diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index 2253f7f831..a9f784c4c6 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -760,6 +760,7 @@ def _rust_toolchain_impl(ctx): _incompatible_change_rust_test_compilation_output_directory = ctx.attr._incompatible_change_rust_test_compilation_output_directory[IncompatibleFlagInfo].enabled, _toolchain_generated_sysroot = ctx.attr._toolchain_generated_sysroot[BuildSettingInfo].value, _incompatible_do_not_include_data_in_compile_data = ctx.attr._incompatible_do_not_include_data_in_compile_data[IncompatibleFlagInfo].enabled, + _incompatible_do_not_inject_degenerate_version_to_rustc_env = ctx.attr._incompatible_do_not_inject_degenerate_version_to_rustc_env[IncompatibleFlagInfo].enabled, _no_std = no_std, _codegen_units = ctx.attr._codegen_units[BuildSettingInfo].value, _experimental_use_allocator_libraries_with_mangled_symbols = ctx.attr.experimental_use_allocator_libraries_with_mangled_symbols, @@ -986,6 +987,10 @@ rust_toolchain = rule( default = Label("//rust/settings:incompatible_do_not_include_data_in_compile_data"), doc = "Label to a boolean build setting that controls whether to include data files in compile_data.", ), + "_incompatible_do_not_inject_degenerate_version_to_rustc_env": attr.label( + default = Label("//rust/settings:incompatible_do_not_inject_degenerate_version_to_rustc_env"), + doc = "Label to a boolean build setting that controls whether to set rustc env vars based on a crate version of '0.0.0' if one is not explicitly provided. ", + ), "_no_std": attr.label( default = Label("//rust/settings:no_std"), ),