Skip to content

Commit f0549c1

Browse files
author
Tamas Vajk
committed
Remap relative bin-dir prefix for file!() and log macros
When transform_sources() symlinks sources into bazel-out/<config>/bin/, file!(), Location::caller(), and tracing/log macros embed the literal relative path rustc received on the command line. The existing ${pwd} remap only matches absolute paths in debug info. Add a second --remap-path-prefix for the relative bazel-out/<config>/bin/ prefix (without ${pwd}) so these compile-time paths are also clean. Add analysis tests verifying the flag is present for generated sources and absent for plain sources, plus a runtime test confirming file!() returns a clean path.
1 parent d7490c8 commit f0549c1

4 files changed

Lines changed: 97 additions & 0 deletions

File tree

rust/private/rustc.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,14 @@ def construct_arguments(
11181118
rustc_flags.add("--remap-path-prefix=${{pwd}}={}".format(remap_path_prefix))
11191119
rustc_flags.add("--remap-path-prefix=${{exec_root}}={}".format(remap_path_prefix))
11201120
rustc_flags.add("--remap-path-prefix=${{output_base}}={}".format(remap_path_prefix))
1121+
if not crate_info.root.is_source:
1122+
# Also remap the relative bazel-out/<config>/bin/ prefix.
1123+
# file!(), Location::caller(), and tracing/log macros embed
1124+
# the literal relative path that rustc received on the command
1125+
# line. The ${pwd} remap above only matches absolute paths in
1126+
# debug info, so a second remap without ${pwd} is needed to
1127+
# strip the bin-dir prefix from these compile-time paths.
1128+
rustc_flags.add(ctx.bin_dir.path + "/", format = "--remap-path-prefix=%s=")
11211129

11221130
emit_without_paths = []
11231131
for kind in emit:

test/unit/remap_path_prefix/BUILD.bazel

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
load("@bazel_skylib//rules:write_file.bzl", "write_file")
12
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
23
load(":debug_transition.bzl", "dbg_rust_binary")
34
load(":remap_path_prefix_test.bzl", "remap_path_prefix_test_suite")
@@ -37,6 +38,32 @@ rust_test(
3738
deps = ["//rust/runfiles"],
3839
)
3940

41+
# Library with generated source to trigger transform_sources().
42+
# Exposes file!() so a runtime test can verify the path is clean.
43+
write_file(
44+
name = "remap_file_macro_lib_src",
45+
out = "remap_file_macro_lib.rs",
46+
content = [
47+
"pub fn get_file_path() -> &'static str {",
48+
" file!()",
49+
"}",
50+
"",
51+
],
52+
)
53+
54+
rust_library(
55+
name = "remap_file_macro_lib",
56+
srcs = [":remap_file_macro_lib.rs"],
57+
edition = "2021",
58+
)
59+
60+
rust_test(
61+
name = "file_macro_test",
62+
srcs = ["file_macro_test.rs"],
63+
edition = "2021",
64+
deps = [":remap_file_macro_lib"],
65+
)
66+
4067
remap_path_prefix_test_suite(
4168
name = "remap_path_prefix_test_suite",
4269
)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[test]
2+
fn file_macro_has_no_bazel_out_prefix() {
3+
let path = remap_file_macro_lib::get_file_path();
4+
assert!(
5+
!path.contains("bazel-out"),
6+
"file!() should not contain 'bazel-out' prefix, got: {path}",
7+
);
8+
}

test/unit/remap_path_prefix/remap_path_prefix_test.bzl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ load(
77
"//test/unit:common.bzl",
88
"assert_action_mnemonic",
99
"assert_argv_contains",
10+
"assert_argv_contains_prefix_not",
11+
"assert_argv_contains_prefix_suffix",
1012
"assert_list_contains_adjacent_elements",
1113
)
1214

@@ -25,6 +27,38 @@ def _remap_path_prefix_test_impl(ctx):
2527

2628
_remap_path_prefix_test = analysistest.make(_remap_path_prefix_test_impl)
2729

30+
def _remap_path_prefix_source_test_impl(ctx):
31+
"""Verify that source targets do NOT get the relative bin-dir remap."""
32+
env = analysistest.begin(ctx)
33+
target = analysistest.target_under_test(env)
34+
35+
action = target.actions[0]
36+
assert_action_mnemonic(env, action, "Rustc")
37+
38+
assert_argv_contains(env, action, "--remap-path-prefix=${pwd}=.")
39+
assert_argv_contains(env, action, "--remap-path-prefix=${exec_root}=.")
40+
assert_argv_contains(env, action, "--remap-path-prefix=${output_base}=.")
41+
assert_argv_contains_prefix_not(env, action, "--remap-path-prefix=bazel-out/")
42+
43+
return analysistest.end(env)
44+
45+
_remap_path_prefix_source_test = analysistest.make(_remap_path_prefix_source_test_impl)
46+
47+
def _remap_file_macro_generated_test_impl(ctx):
48+
"""Verify that generated-source targets get a relative bin-dir remap for file!()."""
49+
env = analysistest.begin(ctx)
50+
target = analysistest.target_under_test(env)
51+
52+
action = target.actions[0]
53+
assert_action_mnemonic(env, action, "Rustc")
54+
55+
# Should have the relative bin-dir remap (e.g. --remap-path-prefix=bazel-out/k8-fastbuild/bin/=)
56+
assert_argv_contains_prefix_suffix(env, action, "--remap-path-prefix=bazel-out/", "/bin/=")
57+
58+
return analysistest.end(env)
59+
60+
_remap_file_macro_generated_test = analysistest.make(_remap_file_macro_generated_test_impl)
61+
2862
def _subst_flags_test_impl(ctx):
2963
"""Verify that process wrapper --subst flags are present."""
3064
env = analysistest.begin(ctx)
@@ -87,6 +121,23 @@ def remap_path_prefix_test_suite(name):
87121
target_under_test = ":remap_bin",
88122
)
89123

124+
# Verify source targets do NOT get the relative bin-dir remap.
125+
_remap_path_prefix_source_test(
126+
name = "remap_path_prefix_source_lib_test",
127+
target_under_test = ":dep",
128+
)
129+
130+
# Verify generated-source targets get the relative bin-dir remap for file!().
131+
_remap_file_macro_generated_test(
132+
name = "remap_file_macro_generated_lib_test",
133+
target_under_test = ":remap_lib",
134+
)
135+
136+
_remap_file_macro_generated_test(
137+
name = "remap_file_macro_generated_bin_test",
138+
target_under_test = ":remap_bin",
139+
)
140+
90141
_subst_flags_test(
91142
name = "subst_flags_lib_test",
92143
target_under_test = ":remap_lib",
@@ -100,6 +151,9 @@ def remap_path_prefix_test_suite(name):
100151
tests = [
101152
":remap_path_prefix_lib_test",
102153
":remap_path_prefix_bin_test",
154+
":remap_path_prefix_source_lib_test",
155+
":remap_file_macro_generated_lib_test",
156+
":remap_file_macro_generated_bin_test",
103157
":subst_flags_lib_test",
104158
":subst_flags_bin_test",
105159
]

0 commit comments

Comments
 (0)