From aed08f50762d65500e39c385775c6f9ffca41586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Mon, 15 Jul 2024 18:27:36 -0300 Subject: [PATCH 1/2] lib: improve error message when index not found on cjs --- src/node_file.cc | 9 ++------- test/es-module/test-cjs-legacyMainResolve.js | 4 ++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index c59235b51cca9f..c1590dbfaed1fb 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -3149,14 +3149,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { } } - std::optional module_path = - node::url::FileURLToPath(env, *package_json_url); + std::string module_path = *initial_file_path + ".js"; std::optional module_base; - if (!module_path.has_value()) { - return; - } - if (args.Length() >= 3 && args[2]->IsString()) { Utf8Value utf8_base_path(isolate, args[2]); auto base_url = @@ -3180,7 +3175,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { THROW_ERR_MODULE_NOT_FOUND(isolate, "Cannot find package '%s' imported from %s", - *module_path, + module_path, *module_base); } diff --git a/test/es-module/test-cjs-legacyMainResolve.js b/test/es-module/test-cjs-legacyMainResolve.js index 1dc7d8faafe6eb..089455edf655e6 100644 --- a/test/es-module/test-cjs-legacyMainResolve.js +++ b/test/es-module/test-cjs-legacyMainResolve.js @@ -129,7 +129,7 @@ describe('legacyMainResolve', () => { ); assert.throws( () => legacyMainResolve(packageJsonUrl, { main: null }, packageJsonUrl), - { code: 'ERR_MODULE_NOT_FOUND' }, + { message: /index\.js/, code: 'ERR_MODULE_NOT_FOUND' }, ); }); @@ -137,7 +137,7 @@ describe('legacyMainResolve', () => { const packageJsonUrl = pathToFileURL('/c/file%20with%20percents/package.json'); assert.throws( () => legacyMainResolve(packageJsonUrl, { main: null }, packageJsonUrl), - { code: 'ERR_MODULE_NOT_FOUND' }, + { message: /index\.js/, code: 'ERR_MODULE_NOT_FOUND' }, ); }); From 6090a922cc7888cbeb890b867908baa4eff92b9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Tue, 16 Jul 2024 10:04:18 -0300 Subject: [PATCH 2/2] better error message --- src/node_file.cc | 10 ++++++++-- test/es-module/test-cjs-legacyMainResolve.js | 13 +++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/node_file.cc b/src/node_file.cc index c1590dbfaed1fb..f0cccd9c596f25 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -3072,6 +3072,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { return; } + std::string package_initial_file = ""; + ada::result file_path_url; std::optional initial_file_path; std::string file_path; @@ -3094,6 +3096,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { FromNamespacedPath(&initial_file_path.value()); + package_initial_file = *initial_file_path; + for (int i = 0; i < legacy_main_extensions_with_main_end; i++) { file_path = *initial_file_path + std::string(legacy_main_extensions[i]); @@ -3149,7 +3153,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { } } - std::string module_path = *initial_file_path + ".js"; + if (package_initial_file == "") + package_initial_file = *initial_file_path + ".js"; + std::optional module_base; if (args.Length() >= 3 && args[2]->IsString()) { @@ -3175,7 +3181,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo& args) { THROW_ERR_MODULE_NOT_FOUND(isolate, "Cannot find package '%s' imported from %s", - module_path, + package_initial_file, *module_base); } diff --git a/test/es-module/test-cjs-legacyMainResolve.js b/test/es-module/test-cjs-legacyMainResolve.js index 089455edf655e6..0bfeb567a22b1f 100644 --- a/test/es-module/test-cjs-legacyMainResolve.js +++ b/test/es-module/test-cjs-legacyMainResolve.js @@ -141,6 +141,19 @@ describe('legacyMainResolve', () => { ); }); + it('should report main file on error message when not found', () => { + const packageJsonUrl = pathToFileURL( + path.resolve( + fixtures.path('/es-modules/legacy-main-resolver'), + 'package.json' + ) + ); + assert.throws( + () => legacyMainResolve(packageJsonUrl, { main: './index.node' }, packageJsonUrl), + { message: /index\.node/, code: 'ERR_MODULE_NOT_FOUND' }, + ); + }); + it('should throw when cannot resolve to a file (base not defined)', () => { const packageJsonUrl = pathToFileURL( path.resolve(