From adee9bd6fb92a788f4b61b427effbca03ba5552b Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sun, 6 Aug 2023 01:37:55 +0000 Subject: [PATCH 1/3] esm: import.meta.resolve exact module not found errors should return --- lib/internal/errors.js | 11 ++++++++++- .../modules/esm/initialize_import_meta.js | 16 +++++++++------- test/es-module/test-esm-import-meta-resolve.mjs | 6 +++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 2206994e9277ee..e8332adc68f944 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -195,6 +195,11 @@ function lazyUtilColors() { utilColors ??= require('internal/util/colors'); return utilColors; } +let url; +function lazyUrl() { + url ??= require('internal/url'); + return url; +} let buffer; function lazyBuffer() { @@ -1450,7 +1455,11 @@ 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') => { +E('ERR_MODULE_NOT_FOUND', function(path, base, type = 'package') { + if (type === 'module') { + const { pathToFileURL } = lazyUrl(); + this.url = pathToFileURL(path).href; + } return `Cannot find ${type} '${path}' imported from ${base}`; }, Error); E('ERR_MULTIPLE_CALLBACK', 'Callback called multiple times', Error); diff --git a/lib/internal/modules/esm/initialize_import_meta.js b/lib/internal/modules/esm/initialize_import_meta.js index 010f4d1a858690..c19050ade018d0 100644 --- a/lib/internal/modules/esm/initialize_import_meta.js +++ b/lib/internal/modules/esm/initialize_import_meta.js @@ -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; }; } diff --git a/test/es-module/test-esm-import-meta-resolve.mjs b/test/es-module/test-esm-import-meta-resolve.mjs index ec6cd37ab01e10..5ae4d256886a1f 100644 --- a/test/es-module/test-esm-import-meta-resolve.mjs +++ b/test/es-module/test-esm-import-meta-resolve.mjs @@ -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'); From e2f007bf0109ec61b9b613dc9d9ba07c9f683386 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 7 Aug 2023 17:38:37 +0000 Subject: [PATCH 2/3] explicit url arg --- lib/internal/errors.js | 15 +++++---------- lib/internal/modules/esm/fetch_module.js | 2 +- lib/internal/modules/esm/resolve.js | 4 ++-- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index e8332adc68f944..1d48a07a09ae98 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -195,11 +195,6 @@ function lazyUtilColors() { utilColors ??= require('internal/util/colors'); return utilColors; } -let url; -function lazyUrl() { - url ??= require('internal/url'); - return url; -} let buffer; function lazyBuffer() { @@ -1455,12 +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', function(path, base, type = 'package') { - if (type === 'module') { - const { pathToFileURL } = lazyUrl(); - this.url = pathToFileURL(path).href; +E('ERR_MODULE_NOT_FOUND', function(path, base, exactUrl) { + if (exactUrl) { + this.url = exactUrl; } - return `Cannot find ${type} '${path}' imported from ${base}`; + 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); diff --git a/lib/internal/modules/esm/fetch_module.js b/lib/internal/modules/esm/fetch_module.js index 74d2d2599dbd45..ca5c9c83c316de 100644 --- a/lib/internal/modules/esm/fetch_module.js +++ b/lib/internal/modules/esm/fetch_module.js @@ -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; } diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index ce5e4e27fd5128..f50b01165ce33e 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -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) { @@ -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); } /** From ebcde3d68f3d3eed5e16aa10313e03da4caf0ddc Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Mon, 7 Aug 2023 21:54:26 +0000 Subject: [PATCH 3/3] pr feedback --- lib/internal/errors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/errors.js b/lib/internal/errors.js index 1d48a07a09ae98..b37c28e46959cc 100644 --- a/lib/internal/errors.js +++ b/lib/internal/errors.js @@ -1452,7 +1452,7 @@ E('ERR_MISSING_ARGS', E('ERR_MISSING_OPTION', '%s is required', TypeError); E('ERR_MODULE_NOT_FOUND', function(path, base, exactUrl) { if (exactUrl) { - this.url = exactUrl; + lazyInternalUtil().setOwnProperty(this, 'url', exactUrl); } return `Cannot find ${ exactUrl ? 'module' : 'package'} '${path}' imported from ${base}`;