Skip to content

Commit 1ca326c

Browse files
author
Tamas Vajk
committed
Fix bin-dir remap to work with --experimental_output_paths=strip
Use add_all with the crate root File and a map_each callback instead of a hardcoded ctx.bin_dir.path string. This lets Bazel's path mapping rewrite the config portion of the bin-dir prefix (e.g. k8-fastbuild -> cfg), so the remap flag matches the actual source file path at execution time. Co-authored-by: Isaac
1 parent 022e197 commit 1ca326c

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

rust/private/rustc.bzl

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,11 @@ def construct_arguments(
12001200
# line. The ${pwd} remap above only matches absolute paths in
12011201
# debug info, so a second remap without ${pwd} is needed to
12021202
# strip the bin-dir prefix from these compile-time paths.
1203-
rustc_flags.add("--remap-path-prefix={}/=".format(ctx.bin_dir.path))
1203+
#
1204+
# Use add_all with the crate root File and a map_each callback
1205+
# so Bazel's path mapping (--experimental_output_paths=strip)
1206+
# can rewrite the config portion of the bin-dir prefix.
1207+
rustc_flags.add_all([crate_info.root], map_each = _get_bin_dir_remap_prefix)
12041208

12051209
emit_without_paths = []
12061210
for kind in emit:
@@ -2714,6 +2718,25 @@ def _add_native_link_flags(
27142718
format_each = "-lstatic=%s",
27152719
)
27162720

2721+
def _get_bin_dir_remap_prefix(file):
2722+
"""Derives a --remap-path-prefix flag that strips the bin-dir prefix.
2723+
2724+
For a generated file, file.path is "bazel-out/<config>/bin/<pkg>/<name>"
2725+
and file.short_path is "<pkg>/<name>". The difference is the bin-dir
2726+
prefix that needs to be stripped from file!() and similar macros.
2727+
2728+
Using the File object with add_all/map_each lets Bazel apply path mapping
2729+
(--experimental_output_paths=strip) to the config portion of the path.
2730+
2731+
Args:
2732+
file (File): The crate root file (must be a generated file).
2733+
2734+
Returns:
2735+
str: A --remap-path-prefix flag that maps the bin-dir prefix to empty.
2736+
"""
2737+
prefix = file.path[:len(file.path) - len(file.short_path)]
2738+
return "--remap-path-prefix={}=".format(prefix)
2739+
27172740
def _get_dirname(file):
27182741
"""A helper function for `_add_native_link_flags`.
27192742

0 commit comments

Comments
 (0)