Skip to content

Commit 91782f1

Browse files
omsmithrvagg
authored andcommitted
path: fix win32 relative() when "to" is a prefix
when the basename of "to" was a prefix of the basename of "from" win32 relative() would miss including it in the result Fixes: #5447 PR-URL: #5456 Reviewed-By: Brian White <[email protected]> Reviewed-By: Roman Reiss <[email protected]>
1 parent dfe45f1 commit 91782f1

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lib/path.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -603,14 +603,28 @@ const win32 = {
603603
var i = 0;
604604
for (; i <= length; ++i) {
605605
if (i === length) {
606-
if (lastCommonSep > 2 && // ignore separator match following device root
607-
toLen > length &&
608-
to.charCodeAt(i) === 92/*\*/) {
609-
// We get here if `from` is the exact base path for `to`.
610-
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
611-
return toOrig.slice(i + 1);
606+
if (toLen > length) {
607+
if (to.charCodeAt(toStart + i) === 92/*\*/) {
608+
// We get here if `from` is the exact base path for `to`.
609+
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
610+
return toOrig.slice(toStart + i + 1);
611+
} else if (lastCommonSep === 2) {
612+
// We get here if `from` is the device root.
613+
// For example: from='C:\\'; to='C:\\foo'
614+
return toOrig.slice(toStart + i);
615+
}
616+
}
617+
if (fromLen > length) {
618+
if (from.charCodeAt(fromStart + i) === 92/*\*/) {
619+
// We get here if `to` is the exact base path for `from`.
620+
// For example: from='C:\\foo\\bar'; to='C:\\foo'
621+
lastCommonSep = i;
622+
} else if (lastCommonSep === 2) {
623+
// We get here if `to` is the device root.
624+
// For example: from='C:\\foo\\bar'; to='C:\\'
625+
lastCommonSep = 3;
626+
}
612627
}
613-
lastCommonSep = i;
614628
break;
615629
}
616630
var fromCode = from.charCodeAt(fromStart + i);

test/parallel/test-path.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,8 @@ const relativeTests = [
470470
['c:/AaAa/bbbb', 'c:/aaaa/bbbb', ''],
471471
['c:/aaaaa/', 'c:/aaaa/cccc', '..\\aaaa\\cccc'],
472472
['C:\\foo\\bar\\baz\\quux', 'C:\\', '..\\..\\..\\..'],
473-
['C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json', 'bar\\package.json']
473+
['C:\\foo\\test', 'C:\\foo\\test\\bar\\package.json', 'bar\\package.json'],
474+
['C:\\foo\\bar\\baz-quux', 'C:\\foo\\bar\\baz', '..\\baz']
474475
]
475476
],
476477
[ path.posix.relative,

0 commit comments

Comments
 (0)