mirror of
https://github.com/nodejs/node.git
synced 2026-11-30 11:55:44 -05:00
Revert "module: fix inconsistency between load and _findPath"
This reverts commit 1b92214
from PR #22382.
See the discussion at
https://github.com/nodejs/citgm/issues/604
PR-URL: https://github.com/nodejs/node/pull/23228
Refs: https://github.com/nodejs/node/pull/22382
Fixes: https://github.com/nodejs/node/issues/4778
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
1 parent
0339d3dc36
commit
15b12411e9
3 files changed
+20
-70
No files matched your search
@@ -218,16 +218,6 @@ 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)) {
|
||||
@@ -284,7 +274,7 @@ Module._findPath = function(request, paths, isMain) {
|
||||
if (!filename) {
|
||||
// try it with each of the extensions
|
||||
if (exts === undefined)
|
||||
exts = readExtensions();
|
||||
exts = Object.keys(Module._extensions);
|
||||
filename = tryExtensions(basePath, exts, isMain);
|
||||
}
|
||||
}
|
||||
@@ -292,7 +282,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 = readExtensions();
|
||||
exts = Object.keys(Module._extensions);
|
||||
filename = tryPackage(basePath, exts, isMain);
|
||||
if (!filename) {
|
||||
filename = tryExtensions(path.resolve(basePath, 'index'), exts, isMain);
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
'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'));
|
||||
@@ -1,58 +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['.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);
|
||||
}
|
||||
Reference in new issue
Block a user