Repository navigation
Add normalize_lexically to Path #396
Description
Activity
- addedapi-change-proposalA proposal to add or alter unstable APIs in the standard librariesA proposal to add or alter unstable APIs in the standard libraries
on Jun 16, 2024 - changed the title
[-]Extend the `Path` API with some useful helper functions.[/-][+]Add normalize_lexically to `Path`[/+]on Jul 23, 2024 I've updated this ACP based on notes from the libs-api meeting. It concentrates on what I'm now calling
normalize_lexicallywhich seemed to have general support although there was some uncertainty about the naming and some of the details.Should this normalize all separators to
MAIN_SEPARATOR?I think that is implied by "in addition to the usual normalization that the
componentsiterator does", since if youpath.components().collect::<PathBuf>()they are joined usingMAIN_SEPARATOR_STR.I've added that link to the ACP. I've also added some links for other languages.
Reacted by kennytmI've checked all linked implementation from other languages and they all behave the same regarding left-over
..:Implementation a/../../b/a/../../b../a/../../bGo path.Clean../b/b../../bJava Path.normalize../b/b../../bNode.js path.normalize../b/b../../bC++ lexically_normal../b/b../../bReacted by Chris Denton, teor and Richard SchneemanSometimes when working within a subdirectory we don't intend to follow .. links.
Depending on application it'd probably be safer to have some sort of
join_beneathor similar where you specify a trusted prefix and some untrusted suffix and it would only normalize the suffix as long as it does not ascend out of the prefix.E.g. https://docs.rs/safe-path/0.1.0/safe_path/fn.scoped_resolve.html
Reacted by Richard SchneemanWe discussed this in the @rust-lang/libs-api meeting today. We're happy to accept this with one slight modification: empty paths should just be allowed as-is rather than erroring. Errors should only be when trying to
..past the root.We recognize that the behavior of erroring differs from other languages, but we believe that this behavior is more useful in practice for path validation.
- addedACP-acceptedAPI Change Proposal is accepted (seconded with no objections)API Change Proposal is accepted (seconded with no objections)
on Sep 17, 2024
Proposal
Problem statement
For Unix platforms, we take pains to warn about the dangers of naively resolving
..components (i.e. resolving /path/to/../file as /path/file). However, that doesn't mean it's never useful. Sometimes when working within a subdirectory we don't intend to follow..links. Also people have a habit of using a literal..when they really did meanpop(). If nothing else, providing a function for this case can be a good hook to add documentation on the issue in a central location.Motivating examples or use cases
Say you have a base path and you want the user to be able to use paths below it.
Solution sketch
Have a function that removes
..components from the path, in addition to the usual normalization that thecomponentsiterator does (such as normalizing separators).Or:
Either way, this would return an error if the
Pathcontains left over..components. I.e.path\..\..\to\fileresolves to..\to\file. It could also error if it resolves to the empty path (less sure about this but unexpectedly empty paths can be a footgun).Alternatives
Result, we could collect any left over..components and place them at the beginning of the path.Links and related work
..components lexically from a user provided path.lexically_normal,lexically_relativeandlexically_proximatenormalizecleannormalizeWhat happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
Second, if there's a concrete solution: