@@ -6,7 +6,10 @@ load("//rust:defs.bzl", "rust_binary", "rust_library")
66load (
77 "//test/unit:common.bzl" ,
88 "assert_action_mnemonic" ,
9+ "assert_argv_contains" ,
10+ "assert_argv_contains_not" ,
911 "assert_list_contains_adjacent_elements" ,
12+ "get_bin_dir_from_action" ,
1013)
1114
1215def _remap_path_prefix_test_impl (ctx ):
@@ -42,6 +45,68 @@ def _subst_flags_test_impl(ctx):
4245
4346_subst_flags_test = analysistest .make (_subst_flags_test_impl )
4447
48+ def _coverage_remap_path_prefix_test_impl (ctx ):
49+ """Verify a single `--remap-path-prefix` flag covers the bin directory.
50+
51+ The flag is derived from a `File` via `map_each` so Bazel's path
52+ mapping (`--experimental_output_paths=strip`) rewrites the
53+ `<config>` segment to `cfg` before it reaches rustc — one flag
54+ works for both the path-mapped and un-mapped forms.
55+ """
56+ env = analysistest .begin (ctx )
57+ target = analysistest .target_under_test (env )
58+
59+ action = target .actions [0 ]
60+ assert_action_mnemonic (env , action , "Rustc" )
61+
62+ # If the host toolchain does not support coverage (no `llvm_cov` or
63+ # missing `profiler_builtins`) the instrument-coverage flag will be
64+ # absent and the remap flag is not expected either.
65+ if "--codegen=instrument-coverage" not in action .argv :
66+ return analysistest .end (env )
67+
68+ bin_dir = get_bin_dir_from_action (action )
69+ assert_argv_contains (env , action , "--remap-path-prefix={}/=" .format (bin_dir ))
70+
71+ return analysistest .end (env )
72+
73+ _coverage_remap_path_prefix_test = analysistest .make (
74+ _coverage_remap_path_prefix_test_impl ,
75+ config_settings = {
76+ "//command_line_option:collect_code_coverage" : True ,
77+ },
78+ )
79+
80+ _coverage_remap_path_prefix_path_mapping_test = analysistest .make (
81+ _coverage_remap_path_prefix_test_impl ,
82+ config_settings = {
83+ "//command_line_option:collect_code_coverage" : True ,
84+ "//command_line_option:experimental_output_paths" : "strip" ,
85+ },
86+ )
87+
88+ def _no_coverage_remap_path_prefix_test_impl (ctx ):
89+ """Verify the coverage-specific remap flag is absent without coverage."""
90+ env = analysistest .begin (ctx )
91+ target = analysistest .target_under_test (env )
92+
93+ action = target .actions [0 ]
94+ assert_action_mnemonic (env , action , "Rustc" )
95+
96+ assert_argv_contains_not (env , action , "--codegen=instrument-coverage" )
97+
98+ bin_dir = get_bin_dir_from_action (action )
99+ assert_argv_contains_not (env , action , "--remap-path-prefix={}/=" .format (bin_dir ))
100+
101+ return analysistest .end (env )
102+
103+ _no_coverage_remap_path_prefix_test = analysistest .make (
104+ _no_coverage_remap_path_prefix_test_impl ,
105+ config_settings = {
106+ "//command_line_option:collect_code_coverage" : False ,
107+ },
108+ )
109+
45110def remap_path_prefix_test_suite (name ):
46111 """Entry-point macro called from the BUILD file.
47112
@@ -78,6 +143,38 @@ def remap_path_prefix_test_suite(name):
78143 edition = "2021" ,
79144 )
80145
146+ # A library whose source set contains a generated file — this is the
147+ # case `transform_sources` is designed for, and the one that
148+ # previously lost all coverage data because the records pointed at
149+ # `bazel-out/.../bin/...`.
150+ write_file (
151+ name = "mixed_inline_src" ,
152+ out = "mixed_inline.rs" ,
153+ content = [
154+ "pub fn inline() {}" ,
155+ "" ,
156+ ],
157+ )
158+
159+ write_file (
160+ name = "mixed_generated_src" ,
161+ out = "mixed_generated.rs" ,
162+ content = [
163+ "pub fn generated() {}" ,
164+ "" ,
165+ ],
166+ )
167+
168+ rust_library (
169+ name = "remap_mixed_lib" ,
170+ srcs = [
171+ ":mixed_inline.rs" ,
172+ ":mixed_generated_src" ,
173+ ],
174+ crate_root = ":mixed_inline.rs" ,
175+ edition = "2021" ,
176+ )
177+
81178 _remap_path_prefix_test (
82179 name = "remap_path_prefix_lib_test" ,
83180 target_under_test = ":remap_lib" ,
@@ -98,11 +195,29 @@ def remap_path_prefix_test_suite(name):
98195 target_under_test = ":remap_bin" ,
99196 )
100197
198+ _coverage_remap_path_prefix_test (
199+ name = "coverage_remap_path_prefix_mixed_lib_test" ,
200+ target_under_test = ":remap_mixed_lib" ,
201+ )
202+
203+ _coverage_remap_path_prefix_path_mapping_test (
204+ name = "coverage_remap_path_prefix_path_mapping_mixed_lib_test" ,
205+ target_under_test = ":remap_mixed_lib" ,
206+ )
207+
208+ _no_coverage_remap_path_prefix_test (
209+ name = "no_coverage_remap_path_prefix_lib_test" ,
210+ target_under_test = ":remap_lib" ,
211+ )
212+
101213 tests = [
102214 ":remap_path_prefix_lib_test" ,
103215 ":remap_path_prefix_bin_test" ,
104216 ":subst_flags_lib_test" ,
105217 ":subst_flags_bin_test" ,
218+ ":coverage_remap_path_prefix_mixed_lib_test" ,
219+ ":coverage_remap_path_prefix_path_mapping_mixed_lib_test" ,
220+ ":no_coverage_remap_path_prefix_lib_test" ,
106221 ]
107222
108223 native .test_suite (
0 commit comments