Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: fix inconsistency between load and _findPath #22382

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,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)) {
Expand Down Expand Up @@ -272,15 +282,15 @@ 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);
}
}

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);
Expand Down
18 changes: 0 additions & 18 deletions test/known_issues/test-module-deleted-extensions.js

This file was deleted.

58 changes: 58 additions & 0 deletions test/parallel/test-module-deleted-extensions.js
Original file line number Diff line number Diff line change
@@ -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);
}