Skip to content

Commit 598787d

Browse files
guybedfordMylesBorins
authored andcommitted
module: fix main resolution and not found updates
This simplifies the top-level load when ES modules are enabled as we can entirely delegate the module resolver, which will hand over to CommonJS where appropriate. All not found errors are made consistent to throw during resolve and have the MODULE_NOT_FOUND code. Includes the test case from #15736. Fixes: #15732 PR-URL: #16147 Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent fad7637 commit 598787d

File tree

9 files changed

+104
-33
lines changed

9 files changed

+104
-33
lines changed

lib/internal/loader/Loader.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Loader {
4747
throw new errors.TypeError('ERR_INVALID_ARG_TYPE',
4848
'parentURL', 'string');
4949
}
50+
5051
const { url, format } = await this.resolver(specifier, parentURL,
5152
ModuleRequest.resolve);
5253

lib/internal/loader/ModuleRequest.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,14 @@ exports.resolve = (specifier, parentURL) => {
8888
};
8989
}
9090

91-
let url = search(specifier, parentURL);
91+
let url;
92+
try {
93+
url = search(specifier, parentURL);
94+
} catch (e) {
95+
if (e.message && e.message.startsWith('Cannot find module'))
96+
e.code = 'MODULE_NOT_FOUND';
97+
throw e;
98+
}
9299

93100
if (url.protocol !== 'file:') {
94101
throw new errors.Error('ERR_INVALID_PROTOCOL',

lib/module.js

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -440,39 +440,28 @@ Module._load = function(request, parent, isMain) {
440440
debug('Module._load REQUEST %s parent: %s', request, parent.id);
441441
}
442442

443-
var filename = null;
444-
445-
if (isMain) {
446-
try {
447-
filename = Module._resolveFilename(request, parent, isMain);
448-
} catch (e) {
449-
// try to keep stack
450-
e.stack;
451-
throw e;
452-
}
453-
if (experimentalModules) {
454-
(async () => {
455-
// loader setup
456-
if (!ESMLoader) {
457-
ESMLoader = new Loader();
458-
const userLoader = process.binding('config').userLoader;
459-
if (userLoader) {
460-
const hooks = await new Loader().import(userLoader);
461-
ESMLoader.hook(hooks);
462-
}
443+
if (isMain && experimentalModules) {
444+
(async () => {
445+
// loader setup
446+
if (!ESMLoader) {
447+
ESMLoader = new Loader();
448+
const userLoader = process.binding('config').userLoader;
449+
if (userLoader) {
450+
const hooks = await new Loader().import(userLoader);
451+
ESMLoader.hook(hooks);
463452
}
464-
await ESMLoader.import(getURLFromFilePath(filename).href);
465-
})()
466-
.catch((e) => {
467-
console.error(e);
468-
process.exit(1);
469-
});
470-
return;
471-
}
472-
} else {
473-
filename = Module._resolveFilename(request, parent, isMain);
453+
}
454+
await ESMLoader.import(getURLFromFilePath(request).href);
455+
})()
456+
.catch((e) => {
457+
console.error(e);
458+
process.exit(1);
459+
});
460+
return;
474461
}
475462

463+
var filename = Module._resolveFilename(request, parent, isMain);
464+
476465
var cachedModule = Module._cache[filename];
477466
if (cachedModule) {
478467
updateChildren(parent, cachedModule, true);

src/module_wrap.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@ URL resolve_directory(const URL& search, bool read_pkg_json) {
442442
URL Resolve(std::string specifier, const URL* base, bool read_pkg_json) {
443443
URL pure_url(specifier);
444444
if (!(pure_url.flags() & URL_FLAGS_FAILED)) {
445+
// just check existence, without altering
446+
auto check = check_file(pure_url, true);
447+
if (check.failed) {
448+
return URL("");
449+
}
445450
return pure_url;
446451
}
447452
if (specifier.length() == 0) {
@@ -493,9 +498,8 @@ void ModuleWrap::Resolve(const FunctionCallbackInfo<Value>& args) {
493498

494499
URL result = node::loader::Resolve(*specifier_utf, &url, true);
495500
if (result.flags() & URL_FLAGS_FAILED) {
496-
std::string msg = "module ";
501+
std::string msg = "Cannot find module ";
497502
msg += *specifier_utf;
498-
msg += " not found";
499503
env->ThrowError(msg.c_str());
500504
return;
501505
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/not-found-assert-loader.mjs
2+
/* eslint-disable required-modules */
3+
import './not-found.js';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/not-found-assert-loader.mjs
2+
/* eslint-disable required-modules */
3+
import './not-found';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import assert from 'assert';
2+
3+
// a loader that asserts that the defaultResolve will throw "not found"
4+
// (skipping the top-level main of course)
5+
let mainLoad = true;
6+
export async function resolve (specifier, base, defaultResolve) {
7+
if (mainLoad) {
8+
mainLoad = false;
9+
return defaultResolve(specifier, base);
10+
}
11+
try {
12+
await defaultResolve(specifier, base);
13+
}
14+
catch (e) {
15+
assert.equal(e.code, 'MODULE_NOT_FOUND');
16+
return {
17+
format: 'builtin',
18+
url: 'fs'
19+
};
20+
}
21+
assert.fail(`Module resolution for ${specifier} should be throw MODULE_NOT_FOUND`);
22+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const { execFileSync } = require('child_process');
5+
6+
const entryPoints = ['iDoNotExist', 'iDoNotExist.js', 'iDoNotExist.mjs'];
7+
const flags = [[], ['--experimental-modules']];
8+
const node = process.argv[0];
9+
10+
for (const args of flags) {
11+
for (const entryPoint of entryPoints) {
12+
try {
13+
execFileSync(node, args.concat(entryPoint), { stdio: 'pipe' });
14+
} catch (e) {
15+
assert(e.toString().match(/Error: Cannot find module/));
16+
continue;
17+
}
18+
assert.fail('Executing node with inexistent entry point should ' +
19+
`fail. Entry point: ${entryPoint}, Flags: [${args}]`);
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
const { execFileSync } = require('child_process');
5+
6+
const entryPoints = ['iDoNotExist', 'iDoNotExist.js', 'iDoNotExist.mjs'];
7+
const flags = [[], ['--experimental-modules', '--preserve-symlinks']];
8+
const node = process.argv[0];
9+
10+
for (const args of flags) {
11+
for (const entryPoint of entryPoints) {
12+
try {
13+
execFileSync(node, args.concat(entryPoint));
14+
} catch (e) {
15+
assert(e.toString().match(/Error: Cannot find module/));
16+
continue;
17+
}
18+
assert.fail('Executing node with inexistent entry point should ' +
19+
`fail. Entry point: ${entryPoint}, Flags: [${args}]`);
20+
}
21+
}

0 commit comments

Comments
 (0)