Skip to content

path.relative() wrong result in v5.7.0 WindowsΒ #5447

Closed

Description

@frickt

Version: <= 5.6.0:

$ node
> process.version
'v5.6.0'
> path.relative('C:\\git\\foo\\bootstrap-loader','C:\\git\\foo\\bootstrap')
'..\\bootstrap'

Version: 5.7.0


$ node
> process.version
process.version
'v5.7.0'
> path.relative('C:\\git\\foo\\bootstrap-loader','C:\\git\\foo\\bootstrap')
'..'

'..' needs to be '..\bootstrap'

Activity

  1. changed the title [-]path.relative() wrong result in v5.7.0 Windows 10[/-] [+]path.relative() wrong result in v5.7.0 Windows[/+] on Feb 26, 2016
  2. rvagg commented on Feb 26, 2016

    @rvagg
    Member

    dupe of #5383 fixed in #5389, this is a regression that'll be fixed in a 5.7.1 which is due out shortly, thanks for reporting

  3. targos commented on Feb 26, 2016

    @targos
    Member

    It's not a dupe of #5383. The result is really wrong here and I'm quite sure #5389 did not fix this.

    cc @mscdex

  4. reopened this on Feb 26, 2016
  5. added
    path
    confirmed-bug
    on Feb 26, 2016
  6. rvagg commented on Feb 26, 2016

    @rvagg
    Member

    right, sorry for my hastiness, confirmed that it's still borked with #5389

  7. added this to the 5.7.1 milestone on Feb 26, 2016
  8. omsmith commented on Feb 26, 2016

    @omsmith
    Contributor

    Played with it a bit, the following diff passes, but really not sure about it.

    diff --git a/lib/path.js b/lib/path.js
    index d01e9b6..e012441 100644
    --- a/lib/path.js
    +++ b/lib/path.js
    @@ -603,14 +603,28 @@ const win32 = {
         var i = 0;
         for (; i <= length; ++i) {
           if (i === length) {
    -        if (lastCommonSep > 2 && // ignore separator match following device root
    -            toLen > length &&
    -            to.charCodeAt(i) === 92/*\*/) {
    -          // We get here if `from` is the exact base path for `to`.
    -          // For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
    -          return toOrig.slice(i + 1);
    +        if (toLen > length) {
    +          if (to.charCodeAt(toStart + i) === 92/*\*/) {
    +            // We get here if `from` is the exact base path for `to`.
    +            // For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
    +            return toOrig.slice(toStart + i + 1);
    +          } else if (lastCommonSep === 2) {
    +            // We get here if `from` is the device root.
    +            // For example: from='C:\\'; to='C:\\foo'
    +            return toOrig.slice(toStart + i);
    +          }
    +        }
    +        if (fromLen > length) {
    +          if (from.charCodeAt(fromStart + i) === 92/*\*/) {
    +            // We get here if `to` is the exact base path for `from`.
    +            // For example: from:='C:\\foo\\bar'; to='C:\\foo'
    +            lastCommonSep = i;
    +          } else if (lastCommonSep === 2) {
    +            // We get here if `to` is the device root.
    +            // For example: from='C:\\foo\\bar'; to='C:\\'
    +            lastCommonSep = 3;
    +          }
             }
    -        lastCommonSep = i;
             break;
           }
           var fromCode = from.charCodeAt(fromStart + i);
    diff --git a/test/parallel/test-path.js b/test/parallel/test-path.js
    index aeb6ec0..02b280e 100644
    --- a/test/parallel/test-path.js
    +++ b/test/parallel/test-path.js
    @@ -470,7 +470,9 @@ const relativeTests = [
          ['c:/AaAa/bbbb', 'c:/aaaa/bbbb', ''],
          ['c:/aaaaa/', 'c:/aaaa/cccc', '..\\aaaa\\cccc'],
          ['C:\\foo\\bar\\baz\\quux', 'C:\\', '..\\..\\..\\..'],
    -     ['C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json', 'bar\\package.json']
    +     ['C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json', 'bar\\package.json'],
    +     ['C:\\foo\\bar\\baz-quux', 'C:\\foo\\bar\\baz', '..\\baz'],
    +     ['C:\\foo\\bar', 'C:\\baz\\quux', '..\\..\\baz\\quux']
         ]
       ],
       [ path.posix.relative,
  9. mscdex commented on Feb 26, 2016

    @mscdex
    Contributor

    @omsmith That LGTM, mind making a PR?

  10. omsmith commented on Feb 26, 2016

    @omsmith
    Contributor

    @mscdex sure thing

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

    confirmed-bugpath

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions