From b9b7c21821025b0c3835e2ff703eba25e29bde4d Mon Sep 17 00:00:00 2001 From: "Adam Perry @ OpenAI" Date: Wed, 29 Jul 2026 00:55:22 +0000 Subject: [PATCH] Add Bazel unit test targets for Rust binaries (#35857) ## Why `codex_rust_crate` generated unit test targets for libraries but not for the binary targets declared by a crate. ## What changed - Generate a `-bin-unit-tests` target for every Rust binary. - Run binary unit tests through the repository-root wrapper so test arguments, timeouts, sharding, retries, environment, data, and tags match library unit tests. - Add `binary_test_target_compatible_with` for constraining generated binary unit test targets to supported platforms. GitOrigin-RevId: 7f3c5bff0162c82d19eff80d5d668c9e91b2fe97 --- defs.bzl | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/defs.bzl b/defs.bzl index 76a82ab362..ca8b014133 100644 --- a/defs.bzl +++ b/defs.bzl @@ -195,6 +195,7 @@ def codex_rust_crate( integration_compile_data_extra = [], integration_test_args = [], unit_test_args = [], + binary_test_target_compatible_with = [], integration_test_timeout = None, test_data_extra = [], test_shard_counts = {}, @@ -232,6 +233,7 @@ def codex_rust_crate( integration_compile_data_extra: Extra compile_data for integration tests. integration_test_args: Optional args for integration test binaries. unit_test_args: Optional args for the unit test binary. + binary_test_target_compatible_with: Platform constraints for binary unit tests. integration_test_timeout: Optional Bazel timeout for integration test targets generated from `tests/*.rs`. test_data_extra: Extra runtime data for tests. @@ -388,6 +390,45 @@ def codex_rust_crate( visibility = ["//visibility:public"], ) + binary_unit_test_name = binary + "-bin-unit-tests" + binary_unit_test_binary = binary_unit_test_name + "-bin" + binary_unit_test_shard_count = _test_shard_count(test_shard_counts, binary_unit_test_name) + + # Keep the Rust test manual so the repo-root wrapper owns filtering and + # sharding while Clippy can still discover the underlying test crate. + rust_test( + name = binary_unit_test_binary, + crate = ":" + binary, + crate_features = crate_features, + deps = all_crate_deps(normal_dev = True), + rustc_flags = rustc_flags_extra + WINDOWS_RUSTC_LINK_FLAGS + [ + "--remap-path-prefix=../codex-rs=", + "--remap-path-prefix=codex-rs=", + ], + rustc_env = rustc_env, + data = test_data_extra, + tags = test_tags + ["manual"], + ) + + binary_unit_test_kwargs = {} + if unit_test_args: + binary_unit_test_kwargs["args"] = unit_test_args + if unit_test_timeout: + binary_unit_test_kwargs["timeout"] = unit_test_timeout + if binary_unit_test_shard_count: + binary_unit_test_kwargs["shard_count"] = binary_unit_test_shard_count + binary_unit_test_kwargs["flaky"] = True + + workspace_root_test( + name = binary_unit_test_name, + env = test_env, + test_bin = ":" + binary_unit_test_binary, + workspace_root_marker = "//codex-rs/utils/cargo-bin:repo_root.marker", + target_compatible_with = binary_test_target_compatible_with, + tags = test_tags, + **binary_unit_test_kwargs + ) + for binary_label in extra_binaries: sanitized_binaries.append(binary_label) binary = Label(binary_label).name