-
Notifications
You must be signed in to change notification settings - Fork 30k
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
path.posix.resolve, path.posix.relative return wrong path on Windows #13887
Comments
Hope I can find a good way to fix it. 😂 |
I had an idea, add a new "strict mode" to const path = require('path');
path.posix.resolve('a/b/c'); // returns: 'C:\\Users\\fei\\Desktop\\share\\node/a/b/c';
path.strict = true;
path.posix.resolve('a/b/c'); // throws |
Can you clarify why it's wrong? |
path.posix.resolve('a/b/c'); // returns: 'C:\\Users\\fei\\Desktop\\share\\node/a/b/c'; The result is not a valid path nor on windows (the host) and definitely not on POSIX (the expected target). See discussion at: #13683 (comment) |
I am against introducing a strict mode. Instead, I would prefer to always throw if I could put together a PR if this gets approval. |
@tniessen I had the same opinion, but @mscdex and @addaleax are strongly against. The point they both raised is that this is not strictly a bug but a weird behavior that people are used to, and throwing will block "valid" code paths. With that in mind, |
Introducing a global setting for such a "strict mode" sounds like an unnecessary side effect to me (unless you want to make that setting per module or similar), e.g. if an application enables strict mode and a dependency expects non-strict mode or vice versa. If you really want to implement different modes, I would suggest I would like to include @addaleax in this discussion so we don't have to split it and continue parts of it in #13683.
Let's look at something simple: path.posix.resolve('foo/bar') So what would be the "correct result" on Windows? Let's say |
how about starting a vote? |
I also really don’t like the idea of strict mode; all Re: always throwing when It’s a nice thing that explicit
I would accept that; it’s strictly better than the current state of things, and even though
Our process is consensus-seeking, so usually things don’t work that way; we’ll keep discussing the issue (and maybe coming up with new solutions) until we either reach consensus or it becomes clear that we won’t reach consensus. I don’t think we’re at either point yet. If we won’t be able to get consensus, we’ll likely put this on the CTC meeting agenda, where it will be discussed, and we’ll only vote if the CTC won’t get consensus either. I think it’s unlikely that it will come to that. |
Definitely +1!
Good point, we might need to come up with a better solution.
Then let's have a look at If we decide not to throw an |
Offf I forgot about the globalness of modules... How about something like... const path = require('path');
path.strict.posix.resolve('.\\gaga') // throws
const { strict: spath } = path;
spath.posix.resolve('.\\gaga') // throws Again it's opt-in, and gives us a path to deprecation with explicit and opt-out in the future. |
Another idea: an implementation that's pure strings, with an optional const { Pure } = require('path');
const purePath1 = new Pure('C:\\fakepath')
purePath1.resolve('gaga') === 'C:\\fakepath\\gaga';
const purePath2 = new Pure();
purePath1.resolve('gaga') === Pure.resolve('gaga') // throws or returns just 'gaga' [refack: tweaked code] |
I also prefer the pure function, but if this is done, does it mean that the history code needs to be modified? |
@DuanPengfei the idea in not to change the behaviour of the old code, but add a new way to use it: const path = require('path');
path.Pure.resolve('gaga') === path.Pure.posix.resolve('gaga') === path.Pure.windows.resolve('gaga') // either all throw or all returns just 'gaga'
// old code stays the same
path.posix.resolve('gaga') === 'C:\\bin\\dev\\node/gaga'; |
@refack I see |
I will create a new PR do the same thing as the PR #13738. And then we discuss whether it is reasonable? |
With just the improvements? Sounds good. |
https://docs.microsoft.com/windows/desktop/FileIO/naming-a-file#win32-file-namespaces |
@GongT That seems to be correct. You are using the POSIX API, why would it recognize Windows namespaces? |
@tniessen |
I keep coming across this type of issue on github and the wild. The assumption that on Windows, the path separation and parsing desired is not POSIX is a bad assumption. People that develop in the wild use things like bash for windows, mingw, msys, etc. The API constraining you based on the OS used makes |
This is actually a duplicate of #13683 ... closing this one |
Using path.posix.relative is not an option because it returns the wrong path. See: * nodejs/node#13887 * nodejs/node#13738
Using path.posix.relative is not an option because it returns the wrong path. See: * nodejs/node#13887 * nodejs/node#13738
path.posix.resolve
,path.posix.relative
return wrong path on Windows.For example:
Ref: #13683
The text was updated successfully, but these errors were encountered: