Skip to content

Commit b36c581

Browse files
committed
module: accept Windows relative path
Before this change, require('.\\test.js') failed while require('./test.js') succeeded. This changes _resolveLookupPaths so that both ways are accepted on Windows. Fixes: #21918 PR-URL: #22186 Reviewed-By: Phillip Johnsen <johphi@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 3993793 commit b36c581

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

lib/internal/modules/cjs/loader.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ const {
8080
CHAR_9,
8181
} = require('internal/constants');
8282

83+
const isWindows = process.platform === 'win32';
84+
8385
function stat(filename) {
8486
filename = path.toNamespacedPath(filename);
8587
const cache = stat.cache;
@@ -310,7 +312,7 @@ Module._findPath = function(request, paths, isMain) {
310312
// 'node_modules' character codes reversed
311313
var nmChars = [ 115, 101, 108, 117, 100, 111, 109, 95, 101, 100, 111, 110 ];
312314
var nmLen = nmChars.length;
313-
if (process.platform === 'win32') {
315+
if (isWindows) {
314316
// 'from' is the __dirname of the module.
315317
Module._nodeModulePaths = function(from) {
316318
// guarantee that 'from' is absolute.
@@ -403,11 +405,12 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
403405
return (newReturn ? null : [request, []]);
404406
}
405407

406-
// Check for relative path
408+
// Check for non-relative path
407409
if (request.length < 2 ||
408410
request.charCodeAt(0) !== CHAR_DOT ||
409411
(request.charCodeAt(1) !== CHAR_DOT &&
410-
request.charCodeAt(1) !== CHAR_FORWARD_SLASH)) {
412+
request.charCodeAt(1) !== CHAR_FORWARD_SLASH &&
413+
(!isWindows || request.charCodeAt(1) !== CHAR_BACKWARD_SLASH))) {
411414
var paths = modulePaths;
412415
if (parent) {
413416
if (!parent.paths)
@@ -480,7 +483,9 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
480483

481484
// make sure require('./path') and require('path') get distinct ids, even
482485
// when called from the toplevel js file
483-
if (parentIdPath === '.' && id.indexOf('/') === -1) {
486+
if (parentIdPath === '.' &&
487+
id.indexOf('/') === -1 &&
488+
(!isWindows || id.indexOf('\\') === -1)) {
484489
id = './' + id;
485490
}
486491

@@ -746,8 +751,6 @@ Module.runMain = function() {
746751
};
747752

748753
Module._initPaths = function() {
749-
const isWindows = process.platform === 'win32';
750-
751754
var homeDir;
752755
var nodePath;
753756
if (isWindows) {
+17-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55
const _module = require('module'); // avoid collision with global.module
6-
const lookupResults = _module._resolveLookupPaths('./lodash');
7-
let paths = lookupResults[1];
86

97
// Current directory gets highest priority for local modules
10-
assert.strictEqual(paths[0], '.');
8+
function testFirstInPath(moduleName, isLocalModule) {
9+
const assertFunction = isLocalModule ?
10+
assert.strictEqual :
11+
assert.notStrictEqual;
1112

12-
paths = _module._resolveLookupPaths('./lodash', null, true);
13+
const lookupResults = _module._resolveLookupPaths(moduleName);
1314

14-
// Current directory gets highest priority for local modules
15-
assert.strictEqual(paths && paths[0], '.');
15+
let paths = lookupResults[1];
16+
assertFunction(paths[0], '.');
17+
18+
paths = _module._resolveLookupPaths(moduleName, null, true);
19+
assertFunction(paths && paths[0], '.');
20+
}
21+
22+
testFirstInPath('./lodash', true);
23+
24+
// Relative path on Windows, but a regular file name elsewhere
25+
testFirstInPath('.\\lodash', common.isWindows);

test/parallel/test-preload.js

+20
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,26 @@ childProcess.exec(
132132
}
133133
);
134134

135+
// test that preloading with a relative path works
136+
process.chdir(fixtures.fixturesDir);
137+
childProcess.exec(
138+
`"${nodeBinary}" ${preloadOption(['./printA.js'])} "${fixtureB}"`,
139+
common.mustCall(function(err, stdout, stderr) {
140+
assert.ifError(err);
141+
assert.strictEqual(stdout, 'A\nB\n');
142+
})
143+
);
144+
if (common.isWindows) {
145+
// https://github.com/nodejs/node/issues/21918
146+
childProcess.exec(
147+
`"${nodeBinary}" ${preloadOption(['.\\printA.js'])} "${fixtureB}"`,
148+
common.mustCall(function(err, stdout, stderr) {
149+
assert.ifError(err);
150+
assert.strictEqual(stdout, 'A\nB\n');
151+
})
152+
);
153+
}
154+
135155
// https://github.com/nodejs/node/issues/1691
136156
process.chdir(fixtures.fixturesDir);
137157
childProcess.exec(

0 commit comments

Comments
 (0)