Skip to content

armgit / codex

Public Imported from GitHub
0

Lightweight coding agent that runs in your terminal

Rust

bazel: fix snapshot parity for tests/*.rs rust_test targets (#11893)

Anton Panasenko1d956561490f
diff --git a/codex-rs/core/tests/common/lib.rs b/codex-rs/core/tests/common/lib.rs
index d2f9c9ac59..a7fda23cd0 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 db09ef0133..b3fe9903ea 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,
)