Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup! module: fix inconsistency between load and _findPath
  • Loading branch information
lundibundi committed Aug 22, 2018
commit 1b51f7e9d47c20a39114ad728473a9045dee9914
7 changes: 5 additions & 2 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,11 @@ function tryExtensions(p, exts, isMain) {

function readExtensions() {
const exts = Object.keys(Module._extensions);
for (var i = 0; i < exts.length; ++i)
exts[i] = path.extname(exts[i]) || exts[i];
for (var i = 0, j = 0; i < exts.length; ++i) {
if (path.extname(exts[i]) === '')
exts[j++] = exts[i];
}
exts.length = j;
return exts;
}
Copy link
Copy Markdown
Member

@jdalton jdalton Aug 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the example case of require.extensions of .foo.bar and .bar this will return an array of ['.bar', '.bar]?

So if you delete require.extensions[".bar"] files like test-extensions.foo.bar will still be found but their compiler at require.extensions[".foo.bar"] won't be used for the module since _compile will use extname(filename) to and get .bar which doesn't exist on require.extensions and so will default to .js.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, though I'm not sure what's the correct behavior here. I'd say to just filter-out all extensions that path.extname() !== '' and use the remaining ones, as they shouldn't be used anyway, wdyt?


Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-module-deleted-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ fs.writeFileSync(file, '', 'utf8');
);
}

{
require.extensions['.bar'] = common.mustNotCall();
require.extensions['.foo.bar'] = common.mustNotCall();
delete require.extensions['.bar'];
const modulePath = path.join(tmpdir.path, 'test-extensions');
assert.throws(
() => require(modulePath),
new Error(`Cannot find module '${modulePath}'`)
);
}

{
require.extensions['.foo.bar'] = common.mustNotCall();
delete require.extensions['.foo.bar'];
Expand Down