Skip to content

Conversation

@seritools
Copy link
Contributor

What does this PR try to resolve?

Fixes #16096.

Finally found some time to trace the issue to its roots:

cargo tries to convert dep paths to relative ones. If a path is either

  1. pkg_root or build_root itself (see DepInfoPathType::PackageRootRelative and DepInfoPathType::BuildRootRelative), causing the EncodedDepInfo to store an empty path, or
  2. an explicitly empty path is provided (e.g. via println!("cargo::rerun-if-changed=");),

then cargo will join the respecive root paths with an empty path. Joining with an empty path adds a trailing path separator. On systems with a / main separator, this works fine. On Windows, however, this adds a trailing backslash. Trailing backslashes are incompatible with .d dep file paths.

This PR adds the necessary checks and ensures that instead of foo.join("") we return foo (instead of effectively foo + std::path::MAIN_SEPARATOR_STR).

Importantly, this PR does not change the behavior for any other paths passed in (e.g. paths explicitly ending in a backslash), and only focuses on unintentional backslashes outside of the user's control.

How to test and review this PR?

The first commit shows the unintended behavior in two tests; the second commit fixes the issue and alters the tests to reflect the new behavior.

Joining an empty path adds a trailing path separator.
On Windows, this causes cargo to emit an invalid depfile,
where a dependency ends in a backslash, which is invalid.

This patch fixes the two known cases where this issue can happen
_without_ the user passing in any path with a trailing backslash.
@rustbot rustbot added A-dep-info Area: dep-info, .d files A-rebuild-detection Area: rebuild detection and fingerprinting S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 7, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 7, 2025

r? @ehuss

rustbot has assigned @ehuss.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Copy link
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. So nice to see tests covering both different scenarios!

View changes since this review

@weihanglo weihanglo added this pull request to the merge queue Nov 7, 2025
Merged via the queue into rust-lang:master with commit 445fe4a Nov 7, 2025
25 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 7, 2025
@seritools seritools deleted the fix-depinfo-trailing-backslash branch November 7, 2025 18:41
bors added a commit to rust-lang/rust that referenced this pull request Nov 8, 2025
Update cargo submodule

22 commits in 6368002885a04cbeae39a82cf5118f941559a40a..445fe4a68f469bf936b2fd81de2c503b233a7f4f
2025-10-31 14:31:52 +0000 to 2025-11-07 18:08:19 +0000
- fix(depinfo): prevent invalid trailing backslash on Windows (rust-lang/cargo#16223)
- refactor: Remove lazycell (rust-lang/cargo#16224)
- refactor: extract ConfigValue to its own module (rust-lang/cargo#16222)
- fix(config): non-mergeable list from cli should take priority (rust-lang/cargo#16220)
- fix(compile): build.warnings=deny shouldn't block hard warnings (rust-lang/cargo#16213)
- fix: display absolute path in the `missing in PATH` warning (rust-lang/cargo#16125)
- fix: non-mergeable list from config cli merge the same way  (rust-lang/cargo#16219)
- docs(contrib): Link out to rustc diagnostic style guide (rust-lang/cargo#16216)
- fix: Remove build-plan (rust-lang/cargo#16212)
- Add native completions for `--package` on various commands (rust-lang/cargo#16210)
- fix(completions): don't wrap completion item help in parenthesis (rust-lang/cargo#16215)
- refactor(locking): Make disabling locking on NFS mounts explicit (rust-lang/cargo#16177)
- docs(unstable): Move compile-time-deps out of Stabilized section (rust-lang/cargo#16211)
- docs(ref): Rename DEP_NAME_KEY to DEP_LINKS_KEY (rust-lang/cargo#16205)
- feat(build-analysis): emit rebuild reason log entry (rust-lang/cargo#16203)
- chore: Update dependencies (rust-lang/cargo#16200)
- chore(deps): update cargo-semver-checks to v0.45.0 (rust-lang/cargo#16190)
- chore(deps): update msrv (rust-lang/cargo#16178)
- refactor: embed deserialize validation logic in ProgressConfig (rust-lang/cargo#16194)
- refactor(gctx): extract config schema to a module (rust-lang/cargo#16195)
- chore: bump to 0.94.0; update changelog (rust-lang/cargo#16191)
- chore(deps): update rust crate gix to 0.74.0 (rust-lang/cargo#16186)

r? ghost
@rustbot rustbot added this to the 1.93.0 milestone Nov 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-dep-info Area: dep-info, .d files A-rebuild-detection Area: rebuild detection and fingerprinting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect .d depfile escaping on Windows in edge case

4 participants