mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
module: fix inconsistency between load and _findPath
Files with multiple extensions are not handled by require-module system therefore if you have file 'file.foo.bar' and require('./file') it won't be found even while using require.extensions['foo.bar'] but before this commit if you have require.extensions['foo.bar'] and require.extensions['bar'] set then the latter will be called if you do require('./file') but if you remove the former the former ('foo.bar') property it will fail. This commit makes it always fail in such cases. Fixes: https://github.com/nodejs/node/issues/4778 PR-URL: https://github.com/nodejs/node/pull/22382 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
parent
3c2aa4b9f3
commit
1b92214d09
@ -217,6 +217,16 @@ function tryExtensions(p, exts, isMain) {
|
||||
return false;
|
||||
}
|
||||
|
||||
function readExtensions() {
|
||||
const exts = Object.keys(Module._extensions);
|
||||
for (var i = 0, j = 0; i < exts.length; ++i) {
|
||||
if (path.extname(exts[i]) === '')
|
||||
exts[j++] = exts[i];
|
||||
}
|
||||
exts.length = j;
|
||||
return exts;
|
||||
}
|
||||
|
||||
var warned = false;
|
||||
Module._findPath = function(request, paths, isMain) {
|
||||
if (path.isAbsolute(request)) {
|
||||
@ -273,7 +283,7 @@ Module._findPath = function(request, paths, isMain) {
|
||||
if (!filename) {
|
||||
// try it with each of the extensions
|
||||
if (exts === undefined)
|
||||
exts = Object.keys(Module._extensions);
|
||||
exts = readExtensions();
|
||||
filename = tryExtensions(basePath, exts, isMain);
|
||||
}
|
||||
}
|
||||
@ -281,7 +291,7 @@ Module._findPath = function(request, paths, isMain) {
|
||||
if (!filename && rc === 1) { // Directory.
|
||||
// try it with each of the extensions at "index"
|
||||
if (exts === undefined)
|
||||
exts = Object.keys(Module._extensions);
|
||||
exts = readExtensions();
|
||||
filename = tryPackage(basePath, exts, isMain);
|
||||
if (!filename) {
|
||||
filename = tryExtensions(path.resolve(basePath, 'index'), exts, isMain);
|
||||
|
@ -1,18 +0,0 @@
|
||||
'use strict';
|
||||
// Refs: https://github.com/nodejs/node/issues/4778
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
const file = path.join(tmpdir.path, 'test-extensions.foo.bar');
|
||||
|
||||
tmpdir.refresh();
|
||||
fs.writeFileSync(file, '', 'utf8');
|
||||
require.extensions['.foo.bar'] = (module, path) => {};
|
||||
delete require.extensions['.foo.bar'];
|
||||
require.extensions['.bar'] = common.mustCall((module, path) => {
|
||||
assert.strictEqual(module.id, file);
|
||||
assert.strictEqual(path, file);
|
||||
});
|
||||
require(path.join(tmpdir.path, 'test-extensions'));
|
58
test/parallel/test-module-deleted-extensions.js
Normal file
58
test/parallel/test-module-deleted-extensions.js
Normal file
@ -0,0 +1,58 @@
|
||||
'use strict';
|
||||
|
||||
// Refs: https://github.com/nodejs/node/issues/4778
|
||||
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const tmpdir = require('../common/tmpdir');
|
||||
const file = path.join(tmpdir.path, 'test-extensions.foo.bar');
|
||||
|
||||
tmpdir.refresh();
|
||||
fs.writeFileSync(file, '', 'utf8');
|
||||
|
||||
{
|
||||
require.extensions['.bar'] = common.mustNotCall();
|
||||
require.extensions['.foo.bar'] = common.mustNotCall();
|
||||
const modulePath = path.join(tmpdir.path, 'test-extensions');
|
||||
assert.throws(
|
||||
() => require(modulePath),
|
||||
new Error(`Cannot find module '${modulePath}'`)
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
delete require.extensions['.bar'];
|
||||
require.extensions['.foo.bar'] = common.mustNotCall();
|
||||
const modulePath = path.join(tmpdir.path, 'test-extensions');
|
||||
assert.throws(
|
||||
() => require(modulePath),
|
||||
new Error(`Cannot find module '${modulePath}'`)
|
||||
);
|
||||
assert.throws(
|
||||
() => require(modulePath + '.foo'),
|
||||
new Error(`Cannot find module '${modulePath}.foo'`)
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
delete require.extensions['.bar'];
|
||||
delete require.extensions['.foo.bar'];
|
||||
const modulePath = path.join(tmpdir.path, 'test-extensions');
|
||||
assert.throws(
|
||||
() => require(modulePath),
|
||||
new Error(`Cannot find module '${modulePath}'`)
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
delete require.extensions['.foo.bar'];
|
||||
require.extensions['.bar'] = common.mustCall((module, path) => {
|
||||
assert.strictEqual(module.id, file);
|
||||
assert.strictEqual(path, file);
|
||||
});
|
||||
|
||||
const modulePath = path.join(tmpdir.path, 'test-extensions.foo');
|
||||
require(modulePath);
|
||||
}
|
Loading…
Reference in New Issue
Block a user