GeezerVison is showing github.com — lib: improve error message when index not found on cjs · nodejs/node@03f3532
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node_file.cc +8-7Lines changed: 8 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3083,6 +3083,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) { | 3083 | 3083 | return; | 3084 | 3084 | } | 3085 | 3085 |
| | 3086 | + std::string package_initial_file = ""; | | 3087 | + | 3086 | 3088 | ada::result<ada::url_aggregator> file_path_url; | 3087 | 3089 | std::optional<std::string> initial_file_path; | 3088 | 3090 | std::string file_path; | @@ -3105,6 +3107,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) { | 3105 | 3107 |
| 3106 | 3108 | FromNamespacedPath(&initial_file_path.value()); | 3107 | 3109 |
| | 3110 | + package_initial_file = *initial_file_path; | | 3111 | + | 3108 | 3112 | for (int i = 0; i < legacy_main_extensions_with_main_end; i++) { | 3109 | 3113 | file_path = *initial_file_path + std::string(legacy_main_extensions[i]); | 3110 | 3114 |
| @@ -3160,13 +3164,10 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) { | 3160 | 3164 | } | 3161 | 3165 | } | 3162 | 3166 |
| 3163 | | - std::optional<std::string> module_path = | 3164 | | - node::url::FileURLToPath(env, *package_json_url); | 3165 | | - std::optional<std::string> module_base; | | 3167 | + if (package_initial_file == "") | | 3168 | + package_initial_file = *initial_file_path + ".js"; | 3166 | 3169 |
| 3167 | | - if (!module_path.has_value()) { | 3168 | | - return; | 3169 | | - } | | 3170 | + std::optional<std::string> module_base; | 3170 | 3171 |
| 3171 | 3172 | if (args.Length() >= 3 && args[2]->IsString()) { | 3172 | 3173 | Utf8Value utf8_base_path(isolate, args[2]); | @@ -3191,7 +3192,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) { | 3191 | 3192 |
| 3192 | 3193 | THROW_ERR_MODULE_NOT_FOUND(isolate, | 3193 | 3194 | "Cannot find package '%s' imported from %s", | 3194 | | - *module_path, | | 3195 | + package_initial_file, | 3195 | 3196 | *module_base); | 3196 | 3197 | } | 3197 | 3198 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/es-module/test-cjs-legacyMainResolve.js +15-2Lines changed: 15 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -129,15 +129,28 @@ describe('legacyMainResolve', () => { | 129 | 129 | ); | 130 | 130 | assert.throws( | 131 | 131 | () => legacyMainResolve(packageJsonUrl, { main: null }, packageJsonUrl), | 132 | | - { code: 'ERR_MODULE_NOT_FOUND' }, | | 132 | + { message: /index\.js/, code: 'ERR_MODULE_NOT_FOUND' }, | 133 | 133 | ); | 134 | 134 | }); | 135 | 135 |
| 136 | 136 | it('should not crash when cannot resolve to a file that contains special chars', () => { | 137 | 137 | const packageJsonUrl = pathToFileURL('/c/file%20with%20percents/package.json'); | 138 | 138 | assert.throws( | 139 | 139 | () => legacyMainResolve(packageJsonUrl, { main: null }, packageJsonUrl), | 140 | | - { code: 'ERR_MODULE_NOT_FOUND' }, | | 140 | + { message: /index\.js/, code: 'ERR_MODULE_NOT_FOUND' }, | | 141 | + ); | | 142 | + }); | | 143 | + | | 144 | + it('should report main file on error message when not found', () => { | | 145 | + const packageJsonUrl = pathToFileURL( | | 146 | + path.resolve( | | 147 | + fixtures.path('/es-modules/legacy-main-resolver'), | | 148 | + 'package.json' | | 149 | + ) | | 150 | + ); | | 151 | + assert.throws( | | 152 | + () => legacyMainResolve(packageJsonUrl, { main: './index.node' }, packageJsonUrl), | | 153 | + { message: /index\.node/, code: 'ERR_MODULE_NOT_FOUND' }, | 141 | 154 | ); | 142 | 155 | }); | 143 | 156 |
| |
|