-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Adding Path.normalize() method #47363
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joshtriplett (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
I don't see any tests for |
@retep998: Those are good points, I can add those tests. What would be some other good Windows-specific tests to include? I must note, I don't have access to a Windows dev box, so it's tricky to run these Windows-specific tests. |
At the very least, we should have every single example from the tables on https://googleprojectzero.blogspot.com/2016/02/the-definitive-guide-on-win32-to-nt.html |
You should also include test cases for filenames containing spaces, tabs, newlines, and all three. Please use raw strings for filenames containing backslashes, so that the test cases don't need to escape backslashes. |
@rust-lang/libs Do we want to use |
@retep998 If we don't use |
Does |
@ThatsGobbles According to the MSDN documentation, "This function does not verify that the resulting path and file name are valid, or that they see an existing file on the associated volume." It does, however, look at the process's current working directory. |
I managed to get a Windows 10 VM up and running, and am trying out tests. The normalization does not seem to like the examples in The Definitive Guide to NT and Win32 Paths involving trailing dots and spaces at the end of a path (the |
It appears that this Windows shell function does what I'm aiming to do with my function! Now the question would be, how do I get access to it in my Rust code? |
As it turns out, I'm not really sure what I should be doing to get this working. I don't know anything about Windows C FFI in Rust, which is stopping me from using the PathCch methods (not to mention, I'd like for normalize to be a pure function that can't fail, and relying on a Windows API seems like it introduces the possibility of failure). Are there any guides or reading I can look at to get my feet wet with this? |
I'm going to defer to @retep998 here. |
Ping @retep998! Do you have any advice for @ThatsGobbles ? |
Hello all, I concur with the name change: As for tests, I've been incorporating the examples from the linked page, but I'm running into issues with Windows paths. It seems that |
src/libstd/path.rs
Outdated
/// ``` | ||
#[unstable(feature = "path normalize", issue = "47402")] | ||
pub fn normalize(&self) -> PathBuf { | ||
#[unstable(feature = "path cleaning", issue = "47402")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feature names usually don't contain spaces.
tc!("/../../", "/"); | ||
} else { | ||
// Drive-absolute paths. | ||
tc!(r#"X:\ABC\DEF"#, r#"X:\ABC\DEF"#); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit pick. Unless your path contains a "
you could just lose the #
s
tc!(r"X:\ABC\DEF", r"X:\ABC\DEF");
I think this is much less noisy.
Ping from triage! @kennytm and @joshtriplett , it looks like the author needs some help dealing with Windows paths. Can either of you recommend someone who might be able to help? Or should we tag a team? I've also asked on IRC. |
@ThatsGobbles Could you list the test cases which tripped up |
@ThatsGobbles Ping from the release team :) Did you have a chance to take a look into what @kennytm proposed to do? |
Hi @kennytm and @arazabishov, apologies for the slow response, I found myself with little free time this week! Here's the current set of problem cases that I've discovered:
All of the cases ending in '. .' failed due to those not getting normalized away. Most of the UNC paths did not trip up |
@ThatsGobbles Hi sorry for long delay. I'm not totally familiar with Windows path, so perhaps @retep998 can give some more mentoring. (The failing test cases are in the source file already)
|
Ping from triage, @ThatsGobbles ! You've got some feedback from @kennytm; will you be able to address it in the near future? |
Thank you so much for this PR @ThatsGobbles! Unfortunately, we haven't heard from you in a while, so I'm closing this to keep the list of PRs clean. If you have time to work on this again please reopen the pull request though! We will be happy to review the new changes. |
As per the discussion here: rust-lang/rfcs#2208
I found myself with a need to normalize file paths. By "normalize", I refer to the lexical cleanup of a path, following a similar algorithm as Python's os.path.normpath() and Go's filepath.Clean(). These in turn follow the methodology outlined in Rob Pike's paper: Lexical File Names in Plan 9.
I was able to write a method that works for my use case, and I felt it could be a helpful addition to stdlib! However, I am very new to both Rust and contributing to OSS projects. Please do let me know if there are any changes that could/should be made in those regards.