Skip to content

Use relative-path for logical path traversal #4831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

Use relative-path for logical path traversal #4831

wants to merge 2 commits into from

Conversation

udoprog
Copy link

@udoprog udoprog commented May 6, 2021

Fixes #4477

This fixes the longstanding issue where you get the following error in case a relative path is specified on Windows which contains . or .. components:

error: couldn't read \\?\D:\Repo\tokio\tokio\tests\..\src\fs\file.rs: The filename, directory name, or volume label syntax is incorrect. (os error 123)
  --> \\?\D:\Repo\tokio\tokio\tests\fs_file_mocked.rs:28:1
   |
28 | mod file;
   | ^^^^^^^^^

Error writing files: failed to resolve mod `file`: \\?\D:\Repo\tokio\tokio\tests\..\src\fs\file.rs does not exist

If you doubt if this issue is still present, you can checkout the Tokio project and give it a spin.

The long version is that paths beginning with \\?\ on Windows are "raw paths" which turns of automatic expansion of . and .. causing them to be treated as files and not current and parent directory. Joining them using the regular method as provided by Path causes . and .. to be incorrectly included in the absolute path.

These kinds of paths are returned by e.g. the fs::canonicalize implementation on Windows.

For more, see: https://github.com/udoprog/patterns/blob/main/examples/relative-path.rs

Note that an alternative is to avoid path canonicalization in how rustfmt processes things, allowing it to work like rustc since that basically uses the same implementation in how the path attribute is expanded.

@udoprog udoprog changed the title Use relative-path for relative path traversal Use relative-path for logical path traversal May 6, 2021
@calebcartwright
Copy link
Member

Thank you for the PR and apologies for the delay. I am going to close this though as I don't want to go in the direction of adding a new dependency and diverging from the logic used within the compiler (we mimic the expansion logic intentionally but don't use it directly since rustfmt operates pre-expansion).

This is actually an issue that was fixed in source on a different branch, and I want to go that route and pull that solution back into the mainline branch.

@udoprog
Copy link
Author

udoprog commented Nov 15, 2021

Cheers. Mind linking to the fix so I can stop using my fork?

@calebcartwright
Copy link
Member

Sure, but will have to get back to you. There was some odd branch divergence for a while as there were plans for a breaking release of rustfmt that never came to fruition, so I'll need to do a little digging.

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.

Rustfmt cannot find mod with #[path] on windows
2 participants