Preserve Cargo package names in Bazel Rust builds (#51847)

## What changed

Expose resolved Cargo package names in `DEP_DATA`, set `CARGO_PKG_NAME` for Rust compilation, and pass the package name to build scripts. Propagate `rustc_env` to generated binary targets so they receive the same package metadata as libraries.

## Testing

Strengthen the packaged CLI smoke test to require `--version` to print exactly `codex-cli <version>`, using the workspace package version from `codex-rs/Cargo.toml`.

GitOrigin-RevId: 274b741067af76313358f44c1bd9b2521938fa9c
This commit is contained in:
Dmitriy Kovalenko
2026-10-07 22:08:04 +00:00
committed by copyberry
parent cba716c0f1
commit 6ea82c59c6
3 changed files with 23 additions and 7 deletions
+4 -1
View File
@@ -299,8 +299,10 @@ def codex_rust_crate(
visibility = ["//visibility:public"],
)
package_data = DEP_DATA.get(native.package_name())
rustc_env = {
"BAZEL_PACKAGE": native.package_name(),
"CARGO_PKG_NAME": package_data["package_name"],
} | rustc_env
manifest_relpath = native.package_name()
@@ -308,7 +310,6 @@ def codex_rust_crate(
manifest_relpath = manifest_relpath[len("codex-rs/"):]
manifest_path = manifest_relpath + "/Cargo.toml"
package_data = DEP_DATA.get(native.package_name())
crate_version = package_data["version"]
binaries = package_data["binaries"]
@@ -319,6 +320,7 @@ def codex_rust_crate(
if build_script_enabled and native.glob(["build.rs"], allow_empty = True):
cargo_build_script(
name = name + "-build-script",
pkg_name = package_data["package_name"],
srcs = ["build.rs"],
deps = all_crate_deps(build = True),
data = build_script_data,
@@ -416,6 +418,7 @@ def codex_rust_crate(
# generated rust_binary instead of leaking it to sibling binaries.
compile_data = binary_compile_data_extra.get(binary, []),
rustc_flags = rustc_flags_extra + binary_rustc_flags_extra.get(binary, []) + WINDOWS_RUSTC_LINK_FLAGS,
rustc_env = rustc_env,
# Keep stamp = 0: rules_rust otherwise makes stable-status.txt and
# volatile-status.txt compiler inputs, even though we only consume
# STABLE_GIT_COMMIT. BUILD_USER and BUILD_HOST vary across developers
@@ -1,14 +1,15 @@
# What: expose resolved Cargo workspace package versions in DEP_DATA.
# What: expose resolved Cargo workspace package names and versions in DEP_DATA.
# Scope: rules_rs workspace metadata only.
diff --git a/rs/private/cargo_workspace_graph.bzl b/rs/private/cargo_workspace_graph.bzl
--- a/rs/private/cargo_workspace_graph.bzl
+++ b/rs/private/cargo_workspace_graph.bzl
@@ -762,6 +762,7 @@ def workspace_dep_data(
@@ -762,6 +762,8 @@ def workspace_dep_data(
"deps": deps,
"deps_by_platform": deps_by_platform,
"dev_deps": dev_deps,
"dev_deps_by_platform": dev_deps_by_platform,
+ "package_name": package["name"],
"shared_libraries": shared_libraries,
+ "version": package["version"],
}
@@ -22,6 +22,7 @@ import json
import shutil
import subprocess
import sys
import tomllib
from pathlib import Path
import pytest
@@ -40,7 +41,6 @@ from fixtures import SmokePackage
("arguments", "expected"),
[
pytest.param(("--help",), "Usage:", id="help"),
pytest.param(("--version",), None, id="version"),
pytest.param(("features", "list"), "code_mode", id="features"),
pytest.param(("completion", "bash"), "codex", id="completion"),
],
@@ -48,11 +48,23 @@ from fixtures import SmokePackage
def test_cli_public_commands(
package: SmokePackage,
arguments: tuple[str, ...],
expected: str | None,
expected: str,
) -> None:
"""Packaged CLI remains usable for discovery, version, features, and completions."""
"""Packaged CLI remains usable for discovery, features, and completions."""
output = package.run(*arguments).stdout
assert expected in output if expected is not None else output.strip()
assert expected in output
def test_cli_version_matches_release_sources(package: SmokePackage) -> None:
"""Verify the compiled banner against the sources used to package the release."""
root = next(
parent
for parent in Path(__file__).resolve().parents
if (parent / "codex-rs/Cargo.toml").is_file()
)
manifest = tomllib.loads((root / "codex-rs/Cargo.toml").read_text())
version = manifest["workspace"]["package"]["version"]
assert package.run("--version").stdout == f"codex-cli {version}\n"
@pytest.mark.parametrize("entrypoint", ["codex", "codex-app-server"])