Skip to content

Commit 2f07767

Browse files
tamasvajkTamas Vajk
andauthored
Include compile_data targets in rustc_env location expansion for rust_test (#4000)
## Summary Include `compile_data` targets in `rustc_env` location expansion for `rust_test`. ## Problem In `rust_test`, `$(location)` and `$(rootpath)` expansions in `rustc_env` only search `data` targets. When a `compile_data` target is referenced in `rustc_env` — for example to pass the path of a generated file via `env!()` and `include_str!()` — the expansion fails because `compile_data` targets are not in the search set. This affects both `rust_test` code paths: the crate-wrapping path (testing an existing `rust_library`) and the standalone path (test with its own sources). ## Fix Add `compile_data` targets to the `data_paths` depset passed to `expand_dict_value_locations` in both `rust_test` code paths. --- > **Note:** This PR was largely AI-generated using Claude Code, with human review and guidance throughout. --------- Co-authored-by: Tamas Vajk <tamas.vajk@databricks.com>
1 parent f31db8b commit 2f07767

6 files changed

Lines changed: 121 additions & 2 deletions

File tree

rust/private/rust.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def _rust_test_impl(ctx):
404404
rustc_env.update(expand_dict_value_locations(
405405
ctx,
406406
ctx.attr.rustc_env,
407-
deduplicate(getattr(ctx.attr, "data", [])),
407+
deduplicate(getattr(ctx.attr, "data", []) + compile_data_targets.to_list()),
408408
{},
409409
))
410410
aliases = dict(crate.aliases)
@@ -461,7 +461,7 @@ def _rust_test_impl(ctx):
461461
rustc_env = expand_dict_value_locations(
462462
ctx,
463463
ctx.attr.rustc_env,
464-
deduplicate(getattr(ctx.attr, "data", [])),
464+
deduplicate(getattr(ctx.attr, "data", []) + getattr(ctx.attr, "compile_data", [])),
465465
{},
466466
)
467467
else:
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
load(":compile_data_env_test.bzl", "compile_data_env_test_suite")
2+
3+
############################ UNIT TESTS #############################
4+
compile_data_env_test_suite(name = "compile_data_env_test_suite")
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
"""Test that $(location) in rustc_env expands compile_data targets in rust_test."""
2+
3+
load("@bazel_skylib//lib:unittest.bzl", "analysistest")
4+
load("@bazel_skylib//rules:write_file.bzl", "write_file")
5+
load("//rust:defs.bzl", "rust_library", "rust_test")
6+
load("//test/unit:common.bzl", "assert_env_value")
7+
8+
def _find_action(tut, mnemonic):
9+
for action in tut.actions:
10+
if action.mnemonic == mnemonic:
11+
return action
12+
return None
13+
14+
# ---------------------------------------------------------------------------
15+
# Test: standalone rust_test with generated compile_data in rustc_env
16+
# ---------------------------------------------------------------------------
17+
18+
def _standalone_test_impl(ctx):
19+
env = analysistest.begin(ctx)
20+
tut = analysistest.target_under_test(env)
21+
action = _find_action(tut, "Rustc")
22+
if not action:
23+
fail("No Rustc action found")
24+
expected = "${pwd}/" + ctx.bin_dir.path + "/test/unit/compile_data_env/generated.txt"
25+
assert_env_value(env, action, "GENERATED_PATH", expected)
26+
return analysistest.end(env)
27+
28+
standalone_compile_data_env_test = analysistest.make(_standalone_test_impl)
29+
30+
# ---------------------------------------------------------------------------
31+
# Test: rust_test wrapping a crate with source compile_data in rustc_env
32+
# ---------------------------------------------------------------------------
33+
34+
def _crate_wrap_test_impl(ctx):
35+
env = analysistest.begin(ctx)
36+
tut = analysistest.target_under_test(env)
37+
action = _find_action(tut, "Rustc")
38+
if not action:
39+
fail("No Rustc action found")
40+
assert_env_value(env, action, "DATA_PATH", "test/unit/compile_data_env/data.txt")
41+
return analysistest.end(env)
42+
43+
crate_wrap_compile_data_env_test = analysistest.make(_crate_wrap_test_impl)
44+
45+
# ---------------------------------------------------------------------------
46+
# Subjects and test suite
47+
# ---------------------------------------------------------------------------
48+
49+
def _test_subjects():
50+
write_file(
51+
name = "gen_file",
52+
out = "generated.txt",
53+
content = ["hello"],
54+
newline = "unix",
55+
)
56+
57+
# Standalone test: compile_data with a generated file
58+
rust_test(
59+
name = "standalone_test",
60+
srcs = ["test.rs"],
61+
compile_data = [":gen_file"],
62+
edition = "2021",
63+
rustc_env = {
64+
"GENERATED_PATH": "$(execpath :gen_file)",
65+
},
66+
)
67+
68+
# Crate-wrap test: compile_data with a source file (not generated,
69+
# to avoid triggering transform_sources which has a separate bug
70+
# with crate= + generated compile_data)
71+
rust_library(
72+
name = "mylib",
73+
srcs = ["lib.rs"],
74+
edition = "2021",
75+
)
76+
77+
rust_test(
78+
name = "crate_wrap_test",
79+
crate = ":mylib",
80+
compile_data = ["data.txt"],
81+
edition = "2021",
82+
rustc_env = {
83+
"DATA_PATH": "$(rootpath data.txt)",
84+
},
85+
)
86+
87+
def compile_data_env_test_suite(name):
88+
"""Entry-point macro called from the BUILD file.
89+
90+
Args:
91+
name: Name of the macro.
92+
"""
93+
_test_subjects()
94+
95+
standalone_compile_data_env_test(
96+
name = "standalone_compile_data_env_test",
97+
target_under_test = ":standalone_test",
98+
)
99+
100+
crate_wrap_compile_data_env_test(
101+
name = "crate_wrap_compile_data_env_test",
102+
target_under_test = ":crate_wrap_test",
103+
)
104+
105+
native.test_suite(
106+
name = name,
107+
tests = [
108+
":standalone_compile_data_env_test",
109+
":crate_wrap_compile_data_env_test",
110+
],
111+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hello

test/unit/compile_data_env/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn lib() {}

test/unit/compile_data_env/test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[test]
2+
fn test() {}

0 commit comments

Comments
 (0)