Skip to content

[POC] {cwd} placeholder in --remap-path-prefix#157405

Draft
ashi009 wants to merge 1 commit into
rust-lang:mainfrom
ashi009:remap-path-prefix-template
Draft

[POC] {cwd} placeholder in --remap-path-prefix#157405
ashi009 wants to merge 1 commit into
rust-lang:mainfrom
ashi009:remap-path-prefix-template

Conversation

@ashi009
Copy link
Copy Markdown

@ashi009 ashi009 commented Jun 4, 2026

Warning

Proof-of-concept, do not merge. Validates feasibility + CI ahead of a Major Change Proposal, since it adds placeholder semantics to the stable --remap-path-prefix flag. Companion to #157348 (the minimal, unstable-only fix that can land now).

Idea

Let a --remap-path-prefix FROM contain a {cwd} placeholder:

--remap-path-prefix={cwd}=.
--remap-path-prefix={cwd}/lib/std=std

The FROM is stored verbatim in remap_path_prefix, and the placeholder is expanded to the working directory only when the FilePathMapping is built (apply time). So:

  • The stored value is the placeholder text ({cwd}/sub), not the absolute cwd → its dependency hash is stable across build directories → the incremental cache survives (fixes the cwd half of no-op remap_path_prefix change invalidates incremental cache #132132).
  • No new type and no change to the field, tracking, decoder, or rustdoc — storing the template verbatim keeps {cwd} (placeholder) and {{cwd}} (escaped literal) distinct in storage, so a plain PathBuf is sufficient.

-Zremap-cwd-prefix=V is reimplemented as exactly sugar for --remap-path-prefix={cwd}=V, giving a path to sunset that unstable flag.

Escaping

format!-style: a path component {cwd} is the placeholder; {{/}} are literal {/}, so a literal {cwd} directory is written {{cwd}}.

Why {cwd} (vs <cwd>, %cwd%, \(cwd))

  • format! syntax — native to Rust, self-escaping ({{/}}), doesn't look path-like.
  • <cwd> / \(cwd): shell redirection / backslash issues (\ is the Windows path separator).
  • %cwd%: collides with cmd.exe %VAR% (%CD%).

(Hand-typed it's quoted like any tool-level token; build systems pass argv with no shell, so it arrives verbatim.)

Open questions for the MCP

  • Placeholder syntax + escaping ({cwd}, {{/}} here).
  • Unknown placeholder handling — kept literal here; erroring may be better.
  • Whole-component vs substring placeholders (whole-component here).
  • Interaction with --remap-path-scope; relationship to RFC 3127.
  • Stable-surface change → requires team sign-off (MCP).

Tests

{cwd}/sub stored-verbatim + applied; escaped {{cwd}} stays literal; -Zremap-cwd-prefix{cwd} equivalence.

r? compiler

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Jun 4, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 4, 2026

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @chenyukang (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • compiler expanded to 73 candidates
  • Random selection from 18 candidates

@rust-log-analyzer

This comment has been minimized.

@ashi009 ashi009 force-pushed the remap-path-prefix-template branch 2 times, most recently from 761963b to 4a201d0 Compare June 4, 2026 06:24
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@ashi009 ashi009 force-pushed the remap-path-prefix-template branch from c115f28 to e655909 Compare June 4, 2026 08:00
@rust-log-analyzer

This comment has been minimized.

@ashi009 ashi009 force-pushed the remap-path-prefix-template branch from e655909 to 5ea39ed Compare June 4, 2026 09:25
@rust-log-analyzer

This comment has been minimized.

Proof-of-concept for an alternative to the incremental-cache invalidation caused
by remapping the current working directory. Validates feasibility ahead of a
Major Change Proposal; not meant to merge as-is.

Today `-Zremap-cwd-prefix=V` resolves the absolute cwd at parse time and stores
it in `remap_path_prefix` (`[TRACKED_NO_CRATE_HASH]`), so the working directory
enters the incremental command-line hash and a build from a different directory
(e.g. a sandbox or per-task worktree) purges the cache even though the remapped
output is identical.

This lets a `--remap-path-prefix` FROM contain a `{cwd}` placeholder, recognised
only as a whole path component, with `format!`-style `{{`/`}}` escaping. The FROM
is stored verbatim (so `{cwd}` and the escaped literal `{{cwd}}` stay distinct
with no new type), and the placeholder is expanded only when the `FilePathMapping`
is built. The stored, tracked value is therefore stable across build directories
and keeps the incremental cache valid. Unknown placeholders are rejected during
parsing. `-Zremap-cwd-prefix=V` becomes exactly sugar for
`--remap-path-prefix={cwd}=V`, providing a path to sunset it.

Adding placeholder semantics to a stable flag is a stable-surface change and needs
sign-off (MCP).
@ashi009 ashi009 force-pushed the remap-path-prefix-template branch from 5ea39ed to fd46a6a Compare June 4, 2026 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants