diff --git a/lib/path.js b/lib/path.js index 0dbd2a641c4d89..fa84485cc3228e 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 411231e709861b..92b0358d8e2323 100644 --- a/test/parallel/test-path.js +++ b/test/parallel/test-path.js @@ -470,7 +470,8 @@ 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'] ] ], [ path.posix.relative,