Skip to content

Commit 022e197

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 175bf94 commit 022e197

4 files changed

Lines changed: 98 additions & 0 deletions

File tree

rust/private/rustc.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,14 @@ def construct_arguments(
11931193
rustc_flags.add("--remap-path-prefix=${{output_base}}={}".format(remap_path_prefix))
11941194
rustc_flags.add("--remap-path-prefix=${{pwd}}={}".format(remap_path_prefix))
11951195
rustc_flags.add("--remap-path-prefix=${{exec_root}}={}".format(remap_path_prefix))
1196+
if not crate_info.root.is_source:
1197+
# Also remap the relative bazel-out/<config>/bin/ prefix.
1198+
# file!(), Location::caller(), and tracing/log macros embed
1199+
# the literal relative path that rustc received on the command
1200+
# line. The ${pwd} remap above only matches absolute paths in
1201+
# debug info, so a second remap without ${pwd} is needed to
1202+
# strip the bin-dir prefix from these compile-time paths.
1203+
rustc_flags.add("--remap-path-prefix={}/=".format(ctx.bin_dir.path))
11961204

11971205
emit_without_paths = []
11981206
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: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ load("//rust:defs.bzl", "rust_binary", "rust_library")
66
load(
77
"//test/unit:common.bzl",
88
"assert_action_mnemonic",
9+
"assert_argv_contains",
10+
"assert_argv_contains_prefix_not",
11+
"assert_argv_contains_prefix_suffix",
912
"assert_list_contains_adjacent_elements",
1013
)
1114

@@ -26,6 +29,38 @@ def _remap_path_prefix_test_impl(ctx):
2629

2730
_remap_path_prefix_test = analysistest.make(_remap_path_prefix_test_impl)
2831

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

126+
# Verify source targets do NOT get the relative bin-dir remap.
127+
_remap_path_prefix_source_test(
128+
name = "remap_path_prefix_source_lib_test",
129+
target_under_test = ":dep",
130+
)
131+
132+
# Verify generated-source targets get the relative bin-dir remap for file!().
133+
_remap_file_macro_generated_test(
134+
name = "remap_file_macro_generated_lib_test",
135+
target_under_test = ":remap_lib",
136+
)
137+
138+
_remap_file_macro_generated_test(
139+
name = "remap_file_macro_generated_bin_test",
140+
target_under_test = ":remap_bin",
141+
)
142+
91143
_subst_flags_test(
92144
name = "subst_flags_lib_test",
93145
target_under_test = ":remap_lib",
@@ -101,6 +153,9 @@ def remap_path_prefix_test_suite(name):
101153
tests = [
102154
":remap_path_prefix_lib_test",
103155
":remap_path_prefix_bin_test",
156+
":remap_path_prefix_source_lib_test",
157+
":remap_file_macro_generated_lib_test",
158+
":remap_file_macro_generated_bin_test",
104159
":subst_flags_lib_test",
105160
":subst_flags_bin_test",
106161
]

0 commit comments

Comments
 (0)