module: handle relative paths in resolve paths
This commit adds support for relative paths in require.resolve()'s paths option. PR-URL: https://github.com/nodejs/node/pull/27598 Fixes: https://github.com/nodejs/node/issues/27583 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
c30ef3cbd2
commit
dac1143fc8
@ -575,18 +575,25 @@ Module._resolveFilename = function(request, parent, isMain, options) {
|
||||
|
||||
if (typeof options === 'object' && options !== null &&
|
||||
Array.isArray(options.paths)) {
|
||||
const fakeParent = new Module('', null);
|
||||
const isRelative = request.startsWith('./') || request.startsWith('../') ||
|
||||
(isWindows && request.startsWith('.\\') || request.startsWith('..\\'));
|
||||
|
||||
paths = [];
|
||||
if (isRelative) {
|
||||
paths = options.paths;
|
||||
} else {
|
||||
const fakeParent = new Module('', null);
|
||||
|
||||
for (var i = 0; i < options.paths.length; i++) {
|
||||
const path = options.paths[i];
|
||||
fakeParent.paths = Module._nodeModulePaths(path);
|
||||
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
|
||||
paths = [];
|
||||
|
||||
for (var j = 0; j < lookupPaths.length; j++) {
|
||||
if (!paths.includes(lookupPaths[j]))
|
||||
paths.push(lookupPaths[j]);
|
||||
for (var i = 0; i < options.paths.length; i++) {
|
||||
const path = options.paths[i];
|
||||
fakeParent.paths = Module._nodeModulePaths(path);
|
||||
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
|
||||
|
||||
for (var j = 0; j < lookupPaths.length; j++) {
|
||||
if (!paths.includes(lookupPaths[j]))
|
||||
paths.push(lookupPaths[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
32
test/fixtures/require-resolve.js
vendored
32
test/fixtures/require-resolve.js
vendored
@ -1,5 +1,5 @@
|
||||
'use strict';
|
||||
require('../common');
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const path = require('path');
|
||||
const nodeModules = path.join(__dirname, 'node_modules');
|
||||
@ -54,3 +54,33 @@ assert.throws(() => {
|
||||
path.join(nodeModules, 'bar.js')
|
||||
);
|
||||
}
|
||||
|
||||
// Verify that relative request paths work properly.
|
||||
{
|
||||
const searchIn = './' + path.relative(process.cwd(), nestedIndex);
|
||||
|
||||
// Search in relative paths.
|
||||
assert.strictEqual(
|
||||
require.resolve('./three.js', { paths: [searchIn] }),
|
||||
path.join(nestedIndex, 'three.js')
|
||||
);
|
||||
|
||||
// Search in absolute paths.
|
||||
assert.strictEqual(
|
||||
require.resolve('./three.js', { paths: [nestedIndex] }),
|
||||
path.join(nestedIndex, 'three.js')
|
||||
);
|
||||
|
||||
// Repeat the same tests with Windows slashes in the request path.
|
||||
if (common.isWindows) {
|
||||
assert.strictEqual(
|
||||
require.resolve('.\\three.js', { paths: [searchIn] }),
|
||||
path.join(nestedIndex, 'three.js')
|
||||
);
|
||||
|
||||
assert.strictEqual(
|
||||
require.resolve('.\\three.js', { paths: [nestedIndex] }),
|
||||
path.join(nestedIndex, 'three.js')
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user