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

esm: import.meta.resolve exact module not found errors should return #49038

Merged
Merged
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
8 changes: 6 additions & 2 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1450,8 +1450,12 @@ E('ERR_MISSING_ARGS',
return `${msg} must be specified`;
}, TypeError);
E('ERR_MISSING_OPTION', '%s is required', TypeError);
E('ERR_MODULE_NOT_FOUND', (path, base, type = 'package') => {
return `Cannot find ${type} '${path}' imported from ${base}`;
E('ERR_MODULE_NOT_FOUND', function(path, base, exactUrl) {
if (exactUrl) {
lazyInternalUtil().setOwnProperty(this, 'url', exactUrl);
}
return `Cannot find ${
exactUrl ? 'module' : 'package'} '${path}' imported from ${base}`;
}, Error);
E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times', Error);
E('ERR_NAPI_CONS_FUNCTION', 'Constructor must be a function', TypeError);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/esm/fetch_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function fetchWithRedirects(parsed) {
return entry;
}
if (res.statusCode === 404) {
const err = new ERR_MODULE_NOT_FOUND(parsed.href, null);
const err = new ERR_MODULE_NOT_FOUND(parsed.href, null, parsed);
err.message = `Cannot find module '${parsed.href}', HTTP 404`;
throw err;
}
Expand Down
16 changes: 9 additions & 7 deletions lib/internal/modules/esm/initialize_import_meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ const experimentalImportMetaResolve = getOptionValue('--experimental-import-meta
function createImportMetaResolve(defaultParentURL, loader) {
return function resolve(specifier, parentURL = defaultParentURL) {
let url;

try {
({ url } = loader.resolveSync(specifier, parentURL));
return url;
} catch (error) {
if (error?.code === 'ERR_UNSUPPORTED_DIR_IMPORT') {
({ url } = error);
} else {
throw error;
switch (error?.code) {
case 'ERR_UNSUPPORTED_DIR_IMPORT':
case 'ERR_MODULE_NOT_FOUND':
({ url } = error);
if (url) {
return url;
}
}
throw error;
}

return url;
};
}

Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
process.send({ 'watch:require': [path || resolved.pathname] });
}
throw new ERR_MODULE_NOT_FOUND(
path || resolved.pathname, base && fileURLToPath(base), 'module');
path || resolved.pathname, base && fileURLToPath(base), resolved);
}

if (!preserveSymlinks) {
Expand Down Expand Up @@ -779,7 +779,7 @@ function packageResolve(specifier, base, conditions) {

// eslint can't handle the above code.
// eslint-disable-next-line no-unreachable
throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base));
throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base), null);
Qard marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
6 changes: 5 additions & 1 deletion test/es-module/test-esm-import-meta-resolve.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ const fixtures = dirname.slice(0, dirname.lastIndexOf('/', dirname.length - 2) +

assert.strictEqual(import.meta.resolve('./test-esm-import-meta.mjs'),
dirname + 'test-esm-import-meta.mjs');
const notFound = import.meta.resolve('./notfound.mjs');
assert.strictEqual(new URL(notFound).href, new URL('./notfound.mjs', import.meta.url).href);
const noExtension = import.meta.resolve('./asset');
assert.strictEqual(new URL(noExtension).href, new URL('./asset', import.meta.url).href);
try {
import.meta.resolve('./notfound.mjs');
import.meta.resolve('does-not-exist');
assert.fail();
} catch (e) {
assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND');
Expand Down