Skip to content

Commit a2b653f

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 hardcoding ctx.bin_dir.path in remap flag strings. This lets Bazel's path mapping rewrite the config portion of the bin-dir prefix (e.g. k8-fastbuild -> cfg), so the remap flags match the actual source file paths at execution time.
1 parent 4941326 commit a2b653f

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

rust/private/rustc.bzl

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,8 +1211,19 @@ def construct_arguments(
12111211
rustc_flags.add("--remap-path-prefix=${{pwd}}={}".format(remap_path_prefix))
12121212
rustc_flags.add("--remap-path-prefix=${{exec_root}}={}".format(remap_path_prefix))
12131213
else:
1214-
rustc_flags.add("--remap-path-prefix=${{pwd}}/{}={}".format(ctx.bin_dir.path, remap_path_prefix))
1215-
rustc_flags.add("--remap-path-prefix=${{exec_root}}/{}={}".format(ctx.bin_dir.path, remap_path_prefix))
1214+
# Use add_all with the crate root File and a map_each callback
1215+
# so Bazel's path mapping (--experimental_output_paths=strip)
1216+
# can rewrite the config portion of the bin-dir prefix.
1217+
rustc_flags.add_all(
1218+
[crate_info.root],
1219+
map_each = _get_bin_dir_prefix,
1220+
format_each = "--remap-path-prefix=${pwd}/%s=" + remap_path_prefix,
1221+
)
1222+
rustc_flags.add_all(
1223+
[crate_info.root],
1224+
map_each = _get_bin_dir_prefix,
1225+
format_each = "--remap-path-prefix=${exec_root}/%s=" + remap_path_prefix,
1226+
)
12161227

12171228
emit_without_paths = []
12181229
for kind in emit:
@@ -2727,6 +2738,22 @@ def _add_native_link_flags(
27272738
format_each = "-lstatic=%s",
27282739
)
27292740

2741+
def _get_bin_dir_prefix(file):
2742+
"""Returns the bin-dir prefix (without trailing slash) from a generated file.
2743+
2744+
For a generated file, file.path is "bazel-out/<config>/bin/<pkg>/<name>"
2745+
and file.short_path is "<pkg>/<name>". The difference is the bin-dir
2746+
prefix. Using the File object with add_all/map_each lets Bazel apply
2747+
path mapping (--experimental_output_paths=strip) to the config portion.
2748+
2749+
Args:
2750+
file (File): A generated file (e.g. the crate root).
2751+
2752+
Returns:
2753+
str: The bin-dir prefix, e.g. "bazel-out/k8-fastbuild/bin".
2754+
"""
2755+
return file.path[:len(file.path) - len(file.short_path)].rstrip("/")
2756+
27302757
def _get_dirname(file):
27312758
"""A helper function for `_add_native_link_flags`.
27322759

0 commit comments

Comments
 (0)