Skip to content

Commit b0d5e03

Browse files
committed
path: fix posix.relative() on Windows
Fixes: #13683 PR-URL: #37747 Reviewed-By: Matteo Collina <[email protected]>
1 parent e46c680 commit b0d5e03

File tree

4 files changed

+25
-17
lines changed

4 files changed

+25
-17
lines changed

Diff for: lib/path.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@
2323

2424
const {
2525
FunctionPrototypeBind,
26+
RegExp,
2627
StringPrototypeCharCodeAt,
28+
StringPrototypeIndexOf,
2729
StringPrototypeLastIndexOf,
30+
StringPrototypeReplace,
2831
StringPrototypeSlice,
2932
StringPrototypeToLowerCase,
3033
} = primordials;
34+
3135
const {
3236
CHAR_UPPERCASE_A,
3337
CHAR_LOWERCASE_A,
@@ -1014,7 +1018,17 @@ const posix = {
10141018
let resolvedAbsolute = false;
10151019

10161020
for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
1017-
const path = i >= 0 ? args[i] : process.cwd();
1021+
let path;
1022+
if (i >= 0) {
1023+
path = args[i];
1024+
} else {
1025+
const _ = StringPrototypeReplace(
1026+
process.cwd(),
1027+
new RegExp(`\\${module.exports.sep}`, 'g'),
1028+
posix.sep
1029+
);
1030+
path = StringPrototypeSlice(_, StringPrototypeIndexOf(_, posix.sep));
1031+
}
10181032

10191033
validateString(path, 'path');
10201034

Diff for: test/known_issues/known_issues.status

-14
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,18 @@ test-vm-timeout-escape-queuemicrotask: SKIP
1515
[$system==win32]
1616

1717
[$system==linux]
18-
# Windows-specific test
19-
test-path-posix-relative-on-windows: SKIP
2018

2119
[$system==macos]
22-
# Windows-specific test
23-
test-path-posix-relative-on-windows: SKIP
2420

2521
[$system==solaris]
26-
# Windows-specific test
27-
test-path-posix-relative-on-windows: SKIP
2822

2923
[$system==freebsd]
30-
# Windows-specific test
31-
test-path-posix-relative-on-windows: SKIP
3224

3325
[$system==aix]
34-
# Windows-specific test
35-
test-path-posix-relative-on-windows: SKIP
3626

3727
[$arch==arm]
3828
# The Raspberry Pis are too slow to run this test.
3929
# See https://github.com/nodejs/build/issues/2227#issuecomment-608334574
4030
test-crypto-authenticated-stream: SKIP
41-
# Windows-specific test
42-
test-path-posix-relative-on-windows: SKIP
4331

4432
[$system==ibmi]
45-
# Windows-specific test
46-
test-path-posix-relative-on-windows: SKIP

Diff for: test/parallel/test-path-resolve.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ const failures = [];
99
const slashRE = /\//g;
1010
const backslashRE = /\\/g;
1111

12+
const posixyCwd = common.isWindows ?
13+
(() => {
14+
const _ = process.cwd()
15+
.replaceAll(path.sep, path.posix.sep);
16+
return _.slice(_.indexOf(path.posix.sep));
17+
})() :
18+
process.cwd();
19+
1220
const resolveTests = [
1321
[ path.win32.resolve,
1422
// Arguments result
@@ -31,8 +39,8 @@ const resolveTests = [
3139
// Arguments result
3240
[[['/var/lib', '../', 'file/'], '/var/file'],
3341
[['/var/lib', '/../', 'file/'], '/file'],
34-
[['a/b/c/', '../../..'], process.cwd()],
35-
[['.'], process.cwd()],
42+
[['a/b/c/', '../../..'], posixyCwd],
43+
[['.'], posixyCwd],
3644
[['/some/dir', '.', '/absolute/'], '/absolute'],
3745
[['/foo/tmp.3/', '../tmp.3/cycles/root.js'], '/foo/tmp.3/cycles/root.js'],
3846
],

0 commit comments

Comments
 (0)