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: https://github.com/nodejs/node/issues/21918 PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
39937938d7
commit
b36c581d5b
@ -80,6 +80,8 @@ const {
|
|||||||
CHAR_9,
|
CHAR_9,
|
||||||
} = require('internal/constants');
|
} = require('internal/constants');
|
||||||
|
|
||||||
|
const isWindows = process.platform === 'win32';
|
||||||
|
|
||||||
function stat(filename) {
|
function stat(filename) {
|
||||||
filename = path.toNamespacedPath(filename);
|
filename = path.toNamespacedPath(filename);
|
||||||
const cache = stat.cache;
|
const cache = stat.cache;
|
||||||
@ -310,7 +312,7 @@ Module._findPath = function(request, paths, isMain) {
|
|||||||
// 'node_modules' character codes reversed
|
// 'node_modules' character codes reversed
|
||||||
var nmChars = [ 115, 101, 108, 117, 100, 111, 109, 95, 101, 100, 111, 110 ];
|
var nmChars = [ 115, 101, 108, 117, 100, 111, 109, 95, 101, 100, 111, 110 ];
|
||||||
var nmLen = nmChars.length;
|
var nmLen = nmChars.length;
|
||||||
if (process.platform === 'win32') {
|
if (isWindows) {
|
||||||
// 'from' is the __dirname of the module.
|
// 'from' is the __dirname of the module.
|
||||||
Module._nodeModulePaths = function(from) {
|
Module._nodeModulePaths = function(from) {
|
||||||
// guarantee that 'from' is absolute.
|
// guarantee that 'from' is absolute.
|
||||||
@ -403,11 +405,12 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
|
|||||||
return (newReturn ? null : [request, []]);
|
return (newReturn ? null : [request, []]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for relative path
|
// Check for non-relative path
|
||||||
if (request.length < 2 ||
|
if (request.length < 2 ||
|
||||||
request.charCodeAt(0) !== CHAR_DOT ||
|
request.charCodeAt(0) !== CHAR_DOT ||
|
||||||
(request.charCodeAt(1) !== CHAR_DOT &&
|
(request.charCodeAt(1) !== CHAR_DOT &&
|
||||||
request.charCodeAt(1) !== CHAR_FORWARD_SLASH)) {
|
request.charCodeAt(1) !== CHAR_FORWARD_SLASH &&
|
||||||
|
(!isWindows || request.charCodeAt(1) !== CHAR_BACKWARD_SLASH))) {
|
||||||
var paths = modulePaths;
|
var paths = modulePaths;
|
||||||
if (parent) {
|
if (parent) {
|
||||||
if (!parent.paths)
|
if (!parent.paths)
|
||||||
@ -480,7 +483,9 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
|
|||||||
|
|
||||||
// make sure require('./path') and require('path') get distinct ids, even
|
// make sure require('./path') and require('path') get distinct ids, even
|
||||||
// when called from the toplevel js file
|
// when called from the toplevel js file
|
||||||
if (parentIdPath === '.' && id.indexOf('/') === -1) {
|
if (parentIdPath === '.' &&
|
||||||
|
id.indexOf('/') === -1 &&
|
||||||
|
(!isWindows || id.indexOf('\\') === -1)) {
|
||||||
id = './' + id;
|
id = './' + id;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -746,8 +751,6 @@ Module.runMain = function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Module._initPaths = function() {
|
Module._initPaths = function() {
|
||||||
const isWindows = process.platform === 'win32';
|
|
||||||
|
|
||||||
var homeDir;
|
var homeDir;
|
||||||
var nodePath;
|
var nodePath;
|
||||||
if (isWindows) {
|
if (isWindows) {
|
||||||
|
@ -1,15 +1,25 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const _module = require('module'); // avoid collision with global.module
|
const _module = require('module'); // avoid collision with global.module
|
||||||
const lookupResults = _module._resolveLookupPaths('./lodash');
|
|
||||||
let paths = lookupResults[1];
|
|
||||||
|
|
||||||
// Current directory gets highest priority for local modules
|
// Current directory gets highest priority for local modules
|
||||||
assert.strictEqual(paths[0], '.');
|
function testFirstInPath(moduleName, isLocalModule) {
|
||||||
|
const assertFunction = isLocalModule ?
|
||||||
|
assert.strictEqual :
|
||||||
|
assert.notStrictEqual;
|
||||||
|
|
||||||
paths = _module._resolveLookupPaths('./lodash', null, true);
|
const lookupResults = _module._resolveLookupPaths(moduleName);
|
||||||
|
|
||||||
// Current directory gets highest priority for local modules
|
let paths = lookupResults[1];
|
||||||
assert.strictEqual(paths && paths[0], '.');
|
assertFunction(paths[0], '.');
|
||||||
|
|
||||||
|
paths = _module._resolveLookupPaths(moduleName, null, true);
|
||||||
|
assertFunction(paths && paths[0], '.');
|
||||||
|
}
|
||||||
|
|
||||||
|
testFirstInPath('./lodash', true);
|
||||||
|
|
||||||
|
// Relative path on Windows, but a regular file name elsewhere
|
||||||
|
testFirstInPath('.\\lodash', common.isWindows);
|
||||||
|
@ -132,6 +132,26 @@ childProcess.exec(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// test that preloading with a relative path works
|
||||||
|
process.chdir(fixtures.fixturesDir);
|
||||||
|
childProcess.exec(
|
||||||
|
`"${nodeBinary}" ${preloadOption(['./printA.js'])} "${fixtureB}"`,
|
||||||
|
common.mustCall(function(err, stdout, stderr) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.strictEqual(stdout, 'A\nB\n');
|
||||||
|
})
|
||||||
|
);
|
||||||
|
if (common.isWindows) {
|
||||||
|
// https://github.com/nodejs/node/issues/21918
|
||||||
|
childProcess.exec(
|
||||||
|
`"${nodeBinary}" ${preloadOption(['.\\printA.js'])} "${fixtureB}"`,
|
||||||
|
common.mustCall(function(err, stdout, stderr) {
|
||||||
|
assert.ifError(err);
|
||||||
|
assert.strictEqual(stdout, 'A\nB\n');
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// https://github.com/nodejs/node/issues/1691
|
// https://github.com/nodejs/node/issues/1691
|
||||||
process.chdir(fixtures.fixturesDir);
|
process.chdir(fixtures.fixturesDir);
|
||||||
childProcess.exec(
|
childProcess.exec(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user