Skip to content

Remap relative bin-dir prefix for file!() and log macros#4017

Open
tamasvajk wants to merge 2 commits into
bazelbuild:mainfrom
tamasvajk:remap-file-macro-symlinked-sources
Open

Remap relative bin-dir prefix for file!() and log macros#4017
tamasvajk wants to merge 2 commits into
bazelbuild:mainfrom
tamasvajk:remap-file-macro-symlinked-sources

Conversation

@tamasvajk
Copy link
Copy Markdown
Contributor

Summary

Companion to #4016. While #4016 fixes --remap-path-prefix for absolute paths in debug info (backtraces, panic locations), this PR adds a remap for the relative bazel-out/<config>/bin/ prefix so that file!(), Location::caller(), and tracing/log macros also produce clean workspace-relative paths when sources are symlinked by transform_sources().

Problem

When a target mixes generated and non-generated sources, transform_sources() symlinks all source files into bazel-out/<config>/bin/.... The existing --remap-path-prefix=${pwd}=. only affects absolute paths embedded in debug info. However, file!(), Location::caller(), and log macros embed the literal relative path that rustc received on the command line. These are not matched by the ${pwd} remap, so they produce paths like:

bazel-out/k8-fastbuild/bin/my/pkg/lib.rs

instead of:

my/pkg/lib.rs

Fix

When crate_info.root.is_source is False (symlinked sources), add a second remap that strips the relative bin-dir prefix:

--remap-path-prefix=bazel-out/<config>/bin/=

This complements the existing absolute-path remaps and ensures compile-time paths are also clean.

Tests

  • Analysis tests: Verify the relative bin-dir remap flag is present for generated-source targets and absent for plain source targets.
  • Runtime test: A library with generated sources exposes file!() and a test asserts the returned path does not contain bazel-out.

@tamasvajk tamasvajk force-pushed the remap-file-macro-symlinked-sources branch from f0549c1 to d784ae6 Compare May 5, 2026 09:30
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.
@tamasvajk tamasvajk force-pushed the remap-file-macro-symlinked-sources branch from d784ae6 to 022e197 Compare May 8, 2026 07:48
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.
@tamasvajk tamasvajk force-pushed the remap-file-macro-symlinked-sources branch from 1ca326c to 905bedc Compare May 8, 2026 08:14
@tamasvajk
Copy link
Copy Markdown
Contributor Author

@UebelAndre Could you please take a look at this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant