Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 0 additions & 10 deletions cargo/private/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ def _cargo_build_script_impl(ctx):
env.update({
"CARGO_CRATE_NAME": name_to_crate_name(pkg_name),
"CARGO_MANIFEST_DIR": manifest_dir,
"CARGO_PKG_NAME": pkg_name,
"HOST": toolchain.exec_triple.str,
"NUM_JOBS": "1",
"OPT_LEVEL": compilation_mode_opt_level,
Expand All @@ -406,15 +405,6 @@ def _cargo_build_script_impl(ctx):
"PROFILE": {"dbg": "debug", "fastbuild": "debug", "opt": "release"}.get(ctx.var["COMPILATION_MODE"], "unknown"),
})

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

# 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.
cc_toolchain, feature_configuration = find_cc_toolchain(ctx)
Expand Down
2 changes: 0 additions & 2 deletions cargo/private/cargo_build_script_wrapper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ def cargo_build_script(
pkg_name = name_to_pkg_name(name)

rustc_env = dict(rustc_env)
if "CARGO_PKG_NAME" not in rustc_env:
rustc_env["CARGO_PKG_NAME"] = pkg_name
if "CARGO_CRATE_NAME" not in rustc_env:
rustc_env["CARGO_CRATE_NAME"] = name_to_crate_name(name_to_pkg_name(name))

Expand Down
12 changes: 9 additions & 3 deletions crate_universe/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@cui//:defs.bzl", "aliases", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_doc", "rust_doc_test", "rust_library", "rust_test")
load("//crate_universe:version.bzl", "VERSION")
load("//cargo:defs.bzl", "cargo_toml_env_vars")

cargo_toml_env_vars(
name = "cargo_toml_env_vars",
src = "Cargo.toml",
)

exports_files(
glob([
Expand Down Expand Up @@ -72,7 +77,7 @@ rust_library(
# for more information). Set stamp = -1 to indicate that it should respect
# the value of the --stamp commandline flag.
stamp = -1,
version = VERSION,
rustc_env_files = [":cargo_toml_env_vars"],
visibility = ["//visibility:public"],
deps = all_crate_deps(normal = True),
)
Expand All @@ -81,7 +86,6 @@ rust_binary(
name = "cargo_bazel_bin",
srcs = ["src/main.rs"],
edition = "2021",
version = VERSION,
visibility = ["//visibility:public"],
deps = [":cargo_bazel"],
)
Expand Down Expand Up @@ -156,6 +160,8 @@ rust_test(
),
)

# TODO(zbarsky): At this point version.bzl only exists to trigger `cargo_bazel_release` GH workflow.
# Consider swapping that to the main repo's `version.bzl` and remove this.
rust_test(
name = "versions_test",
srcs = ["tests/version_test.rs"],
Expand Down
16 changes: 0 additions & 16 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -140,26 +140,10 @@ def _get_rustc_env(attr, toolchain, crate_name):
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 = ""

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,
"CARGO_CRATE_NAME": crate_name,
"CARGO_PKG_AUTHORS": "",
"CARGO_PKG_DESCRIPTION": "",
"CARGO_PKG_HOMEPAGE": "",
"CARGO_PKG_NAME": attr.name,
"CARGO_PKG_VERSION": version,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Won't this be unset for normal rust_* targets?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will, but on the off chance that they actually need this they can either set env = {"CARGO_PKG_VERSION": version} explicitly or use cargo_toml_env_vars for proper support, like we do for cargo-bazel in this PR. I think it's confusing to have multiple ways to provide this functionality, and this one is more incomplete compared to cargo_toml_env_vars

See also https://bazelbuild.slack.com/archives/CSV56UT0F/p1759051609743919?thread_ts=1758229364.645099&cid=CSV56UT0F

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@UebelAndre another way of looking at it - previously, not setting the 'version' attribute on manually-written rust_library rules would mean they get compiled with CARGO_PKG_VERSION_MAJOR=0 and similar. That's also kinda weird and probably not the desired behavior...

Copy link
Collaborator

Choose a reason for hiding this comment

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

it's useful for things like clap which basically embeds the binary version for free in your crate due to these environment variables. While I can see the argument for separating out cargo environment variables I think the few that are there do offer some convenience folks have already taken advantage of. If this were to change I think it would need an incompatibility flag.

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 to keep the existing defaults CARGO_PKG_* derived from the rust_* rules in skylark. Many random crates in the ecosystem examine these and we have setup that imports them, generating BUILD files, deriving the crate name and version of the target from the Cargo.toml file in the process, and discarding the Cargo.toml files after that. While the defaults are not always correct, these defaults cover a good chunk of crates in our experience.

I agree it's super confusing in the presence of cargo_toml_env_vars, and there I think we shouldn't derive the defaults. Instead of a new incompatibility flag, could we make it that the implementation detects the presence of cargo_toml_env_vars dependency and disables the defaults derivation if that's present?

Copy link
Contributor Author

@dzbarsky dzbarsky Oct 7, 2025

Choose a reason for hiding this comment

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

cargo_toml_env_vars is just a .env file, could your import process write a .env file with the values instead? (That would be pretty similar to what I'm doing in rules_rs)

If we think changing this is too annoying I'm happy to stick it behind an incompatible flag, that would be good enough for my purposes

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see, then adding an incompatible flag sounds good.

We could update the generators, but we can't update all crates' build files all at once (it's like like each crate is updated independently on its own schedule by separate groups of people). So we'll need the flexibility to opt in and out of this feature on a per-target basis if we are to move over.

A few more thoughts just to clarify my mental model:

  • about the rust_library.version attribute: it is documented as an explicit way to stamp out the cargo env var. In the new mode, will this be a no-op?
  • how does the cargo_toml_env_vars feature interact with rust_library.rustc_env when there are duplicate keys? Naively, rustc_env takes precedence right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How would you guys feel if we just stopped defaulting the version to 0.0.0 and left the vars unset in case the version wasn't provided? That would match the handling in the build_script rule and would give users an easy way to opt out of this behavior for all the version vars. It would also allow to share the duplicated parsing implementations that are inconsistent.

That would leave only CARGO_PKG_NAME which is sadly used by clap. (Fun fact, that usage is incorrect and it should use CARGO_BIN_NAME to get the right names, but that requires a clap fix...). We can tackle that one another day

"CARGO_PKG_VERSION_MAJOR": major,
"CARGO_PKG_VERSION_MINOR": minor,
"CARGO_PKG_VERSION_PATCH": patch,
"CARGO_PKG_VERSION_PRE": pre,
}
if hasattr(attr, "_is_proc_macro_dep_enabled") and attr._is_proc_macro_dep_enabled[IsProcMacroDepEnabledInfo].enabled:
is_proc_macro_dep = "0"
Expand Down
4 changes: 0 additions & 4 deletions test/build_env/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
fn main() {
println!(
"cargo:rustc-env=CARGO_PKG_NAME_FROM_BUILD_SCRIPT={}",
env!("CARGO_PKG_NAME")
);
println!(
"cargo:rustc-env=CARGO_CRATE_NAME_FROM_BUILD_SCRIPT={}",
env!("CARGO_CRATE_NAME")
Expand Down
5 changes: 0 additions & 5 deletions test/build_env/tests/cargo.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
#[test]
fn cargo_env_vars() {
assert_eq!(env!("CARGO_PKG_NAME"), "cargo_env-vars_test");
assert_eq!(env!("CARGO_CRATE_NAME"), "cargo_env_vars_test");
assert_eq!(
env!("CARGO_PKG_NAME_FROM_BUILD_SCRIPT"),
"cargo_build_script_env-vars"
);
assert_eq!(
env!("CARGO_CRATE_NAME_FROM_BUILD_SCRIPT"),
"cargo_build_script_env_vars"
Expand Down
4 changes: 0 additions & 4 deletions test/build_env/tests/custom_crate_name.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#[test]
fn cargo_env_vars() {
assert_eq!(
env!("CARGO_PKG_NAME"),
"cargo-env-vars-custom-crate-name-test"
);
assert_eq!(env!("CARGO_CRATE_NAME"), "custom_crate_name");
}
Loading