fix(depinfo): prevent invalid trailing backslash on Windows #16223
+124
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
pkg_rootorbuild_rootitself (seeDepInfoPathType::PackageRootRelativeandDepInfoPathType::BuildRootRelative), causing theEncodedDepInfoto store an empty path, orprintln!("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.ddep file paths.This PR adds the necessary checks and ensures that instead of
foo.join("")we returnfoo(instead of effectivelyfoo + 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.