From 1d956561490f4e5d87687b49126339676f417cd8 Mon Sep 17 00:00:00 2001 From: Anton Panasenko Date: Sun, 15 Feb 2026 23:11:59 -0800 Subject: [PATCH] bazel: fix snapshot parity for tests/*.rs rust_test targets (#11893) ## Summary - make `rust_test` targets generated from `tests/*.rs` use Cargo-style crate names (file stem) so snapshot names match Cargo (`all__...` instead of Bazel-derived names) - split lib vs `tests/*.rs` test env wiring in `codex_rust_crate` to keep existing lib snapshot behavior while applying Bazel runfiles-compatible workspace root for `tests/*.rs` - compute the `tests/*.rs` snapshot workspace root from package depth so `insta` resolves committed snapshots under Bazel `--noenable_runfiles` ## Validation - `bazelisk test //codex-rs/core:core-all-test --test_arg=suite::compact:: --cache_test_results=no` - `bazelisk test //codex-rs/core:core-all-test --test_arg=suite::compact_remote:: --cache_test_results=no` --- codex-rs/core/tests/common/lib.rs | 20 ++++++++++++++++++++ defs.bzl | 14 +++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/codex-rs/core/tests/common/lib.rs b/codex-rs/core/tests/common/lib.rs index d2f9c9ac5..a7fda23cd 100644 --- a/codex-rs/core/tests/common/lib.rs +++ b/codex-rs/core/tests/common/lib.rs @@ -26,6 +26,26 @@ fn enable_deterministic_unified_exec_process_ids_for_tests() { codex_core::test_support::set_deterministic_process_ids(true); } +#[ctor] +fn configure_insta_workspace_root_for_snapshot_tests() { + if std::env::var_os("INSTA_WORKSPACE_ROOT").is_some() { + return; + } + + let workspace_root = codex_utils_cargo_bin::repo_root() + .ok() + .map(|root| root.join("codex-rs")); + + if let Some(workspace_root) = workspace_root + && let Ok(workspace_root) = workspace_root.canonicalize() + { + // Safety: this ctor runs at process startup before test threads begin. + unsafe { + std::env::set_var("INSTA_WORKSPACE_ROOT", workspace_root); + } + } +} + #[track_caller] pub fn assert_regex_match<'s>(pattern: &str, actual: &'s str) -> regex_lite::Captures<'s> { let regex = Regex::new(pattern).unwrap_or_else(|err| { diff --git a/defs.bzl b/defs.bzl index db09ef013..b3fe9903e 100644 --- a/defs.bzl +++ b/defs.bzl @@ -170,20 +170,28 @@ def codex_rust_crate( cargo_env["CARGO_BIN_EXE_" + binary] = "$(rlocationpath %s)" % binary_label for test in native.glob(["tests/*.rs"], allow_empty = True): - test_name = name + "-" + test.removeprefix("tests/").removesuffix(".rs").replace("/", "-") + test_file_stem = test.removeprefix("tests/").removesuffix(".rs") + test_crate_name = test_file_stem.replace("-", "_") + test_name = name + "-" + test_file_stem.replace("/", "-") if not test_name.endswith("-test"): test_name += "-test" rust_test( name = test_name, + crate_name = test_crate_name, crate_root = test, srcs = [test], data = native.glob(["tests/**"], allow_empty = True) + sanitized_binaries + test_data_extra, compile_data = native.glob(["tests/**"], allow_empty = True) + integration_compile_data_extra, deps = maybe_lib + deps + dev_deps + integration_deps_extra, proc_macro_deps = proc_macro_deps + proc_macro_dev_deps, - rustc_flags = rustc_flags_extra, + # Keep `file!()` paths Cargo-like (`core/tests/...`) instead of + # Bazel workspace-prefixed (`codex-rs/core/tests/...`) for snapshot parity. + rustc_flags = rustc_flags_extra + ["--remap-path-prefix=codex-rs="], rustc_env = rustc_env, - env = test_env | cargo_env, + # Important: do not merge `test_env` here. Its unit-test-only + # `INSTA_WORKSPACE_ROOT="."` can point integration tests at the + # runfiles cwd and cause false `.snap.new` churn on Linux. + env = cargo_env, tags = test_tags, )