Skip to content

Commit 1b92214

Browse files
lundibundiaddaleax
authored andcommittedSep 17, 2018
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: #4778 PR-URL: #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>
1 parent 3c2aa4b commit 1b92214

File tree

3 files changed

+70
-20
lines changed

3 files changed

+70
-20
lines changed
 

‎lib/internal/modules/cjs/loader.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,16 @@ function tryExtensions(p, exts, isMain) {
217217
return false;
218218
}
219219

220+
function readExtensions() {
221+
const exts = Object.keys(Module._extensions);
222+
for (var i = 0, j = 0; i < exts.length; ++i) {
223+
if (path.extname(exts[i]) === '')
224+
exts[j++] = exts[i];
225+
}
226+
exts.length = j;
227+
return exts;
228+
}
229+
220230
var warned = false;
221231
Module._findPath = function(request, paths, isMain) {
222232
if (path.isAbsolute(request)) {
@@ -273,15 +283,15 @@ Module._findPath = function(request, paths, isMain) {
273283
if (!filename) {
274284
// try it with each of the extensions
275285
if (exts === undefined)
276-
exts = Object.keys(Module._extensions);
286+
exts = readExtensions();
277287
filename = tryExtensions(basePath, exts, isMain);
278288
}
279289
}
280290

281291
if (!filename && rc === 1) { // Directory.
282292
// try it with each of the extensions at "index"
283293
if (exts === undefined)
284-
exts = Object.keys(Module._extensions);
294+
exts = readExtensions();
285295
filename = tryPackage(basePath, exts, isMain);
286296
if (!filename) {
287297
filename = tryExtensions(path.resolve(basePath, 'index'), exts, isMain);

‎test/known_issues/test-module-deleted-extensions.js

-18
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
'use strict';
2+
3+
// Refs: https://github.com/nodejs/node/issues/4778
4+
5+
const common = require('../common');
6+
const assert = require('assert');
7+
const fs = require('fs');
8+
const path = require('path');
9+
const tmpdir = require('../common/tmpdir');
10+
const file = path.join(tmpdir.path, 'test-extensions.foo.bar');
11+
12+
tmpdir.refresh();
13+
fs.writeFileSync(file, '', 'utf8');
14+
15+
{
16+
require.extensions['.bar'] = common.mustNotCall();
17+
require.extensions['.foo.bar'] = common.mustNotCall();
18+
const modulePath = path.join(tmpdir.path, 'test-extensions');
19+
assert.throws(
20+
() => require(modulePath),
21+
new Error(`Cannot find module '${modulePath}'`)
22+
);
23+
}
24+
25+
{
26+
delete require.extensions['.bar'];
27+
require.extensions['.foo.bar'] = common.mustNotCall();
28+
const modulePath = path.join(tmpdir.path, 'test-extensions');
29+
assert.throws(
30+
() => require(modulePath),
31+
new Error(`Cannot find module '${modulePath}'`)
32+
);
33+
assert.throws(
34+
() => require(modulePath + '.foo'),
35+
new Error(`Cannot find module '${modulePath}.foo'`)
36+
);
37+
}
38+
39+
{
40+
delete require.extensions['.bar'];
41+
delete require.extensions['.foo.bar'];
42+
const modulePath = path.join(tmpdir.path, 'test-extensions');
43+
assert.throws(
44+
() => require(modulePath),
45+
new Error(`Cannot find module '${modulePath}'`)
46+
);
47+
}
48+
49+
{
50+
delete require.extensions['.foo.bar'];
51+
require.extensions['.bar'] = common.mustCall((module, path) => {
52+
assert.strictEqual(module.id, file);
53+
assert.strictEqual(path, file);
54+
});
55+
56+
const modulePath = path.join(tmpdir.path, 'test-extensions.foo');
57+
require(modulePath);
58+
}

0 commit comments

Comments
 (0)