Skip to content

path.relative on windows returns incorrect result #17413

Closed
@TanninOne

Description

@TanninOne
  • Version: tested with v7.7.4 and v9.2.0
  • Platform: Windows 10 (64-bit)
  • Subsystem: path

Code says it best:

> path.relative('c:', 'c:\\test')
'..\\..\\test'
> path.relative('c:', 'c:/test') 
'..\\..\\test'

Not sure why c: is treated differently here, but for all other volumes it returns the right result:

> path.relative('d:', 'd:\\test')
'test'
> path.relative('d:', 'd:/test') 
'test'

also it appears to happen only on the root level:

> path.relative('c:\\test', 'c:\\test\\sub')
'sub'

and only if the colon is the last character of the base path

> path.relative('c:\\', 'c:\\test')
'test'

Activity

added
pathIssues and PRs related to the path subsystem.
on Dec 1, 2017
vsemozhetbyt

vsemozhetbyt commented on Dec 1, 2017

@vsemozhetbyt
Contributor

It also seems to happen only if the current working directory is in c: drive.

TanninOne

TanninOne commented on Dec 1, 2017

@TanninOne
Author

I can't confirm that:

> process.cwd()
'D:\\'
> path.relative('c:', 'c:\\test')
'..\\..\\test'
> path.relative('d:', 'd:\\test')
'test'

Edit: I just noticed that github had replaced all double backslashes by single ones, in my tests all backslashes were properly escaped.

vsemozhetbyt

vsemozhetbyt commented on Dec 1, 2017

@vsemozhetbyt
Contributor

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'
TanninOne

TanninOne commented on Dec 1, 2017

@TanninOne
Author

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.

> process.cwd()
'D:\\Temp'
> path.relative('d:', 'd:\\test')
'..\\test'

(remember: windows has a separate cwd for each drive)

added
windowsIssues and PRs related to the Windows platform.
on Dec 1, 2017
TimothyGu

TimothyGu commented on Dec 2, 2017

@TimothyGu
Member

When the first parameter is "c:" or "d:" or ":" it uses the cwd in the corresponding drive as reference, not the root directory.

That's correct. You may want to instead use C:\ as root. Do you think this issue could now be closed?

See also: #14405 and #14440 (only applied to v8.3.0+ and v6.11.3 (and later v6.x)).

TanninOne

TanninOne commented on Dec 2, 2017

@TanninOne
Author

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.
This behaviour is maybe not a Bug but it‘s a BugFactory.
Drive-specific cwds are an abomination, especially since Windows has another, drive independent cwd, and I somehow don‘t believe anyone is writing code expecting this behaviour.

TimothyGu

TimothyGu commented on Dec 2, 2017

@TimothyGu
Member

@TanninOne

I believe it would be more intuitive to have C: be treated as the root.
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'

Drive-specific cwds are an abomination

On this, I wholeheartedly agree.

TanninOne

TanninOne commented on Dec 3, 2017

@TanninOne
Author

Ok, I'll withdraw my report. To my surprise, FindFirstFile also works this way. It's utterly insane but yeah, that's windows alright. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pathIssues and PRs related to the path subsystem.windowsIssues and PRs related to the Windows platform.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mscdex@TimothyGu@TanninOne@vsemozhetbyt

        Issue actions

          path.relative on windows returns incorrect result · Issue #17413 · nodejs/node