-
Notifications
You must be signed in to change notification settings - Fork 29.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
path.relative on windows returns incorrect result #17413
Comments
It also seems to happen only if the current working directory is in |
I can't confirm that:
Edit: I just noticed that github had replaced all double backslashes by single ones, in my tests all backslashes were properly escaped. |
Hmm. For me, with Node.js 9.2.0 on Windows 7 x64 c:\>node
> process.cwd()
'c:\\'
> path.relative('c:', 'c:\\test')
'test'
c:\Windows>node
> process.cwd()
'c:\\Windows'
> path.relative('c:', 'c:\\test')
'..\\test'
d:\>node
> process.cwd()
'd:\\'
> path.relative('c:', 'c:\\test')
'test'
d:\Boot>node
> process.cwd()
'd:\\Boot'
> path.relative('c:', 'c:\\test')
'test' |
Oooh, wait, now I get it. When the first parameter is "c:" or "d:" or ":" it uses the cwd in the corresponding drive as reference, not the root directory.
(remember: windows has a separate cwd for each drive) |
That's correct. You may want to instead use See also: #14405 and #14440 (only applied to v8.3.0+ and v6.11.3 (and later v6.x)). |
I realize this would be an api change but I believe it would be more intuitive to have C: be treated as the root. Windows api calls like findfirstfile or ntquerydirectoryfile do it. No win api call afaik uses drive specific cwds. And anyone coming from unixoid oses will also be surprised. |
While that may be the case, as a platform-level API we cannot go against well-established and documented traditions on a specific OS. Python, for example, follows the current Node.js behavior: >>> os.getcwd()
'C:\\Users\\Timothy'
>>> ntpath.relpath('C:\\abc\\def', r'C:')
'..\\..\\abc\\def'
>>> os.chdir('C:\\')
>>> os.getcwd()
'C:\\'
>>> ntpath.relpath('C:\\abc\\def', r'C:')
'abc\\def'
On this, I wholeheartedly agree. |
Ok, I'll withdraw my report. To my surprise, FindFirstFile also works this way. It's utterly insane but yeah, that's windows alright. :( |
Code says it best:
Not sure why c: is treated differently here, but for all other volumes it returns the right result:
also it appears to happen only on the root level:
and only if the colon is the last character of the base path
The text was updated successfully, but these errors were encountered: