Skip to content

Commit c667fbd

Browse files
H4admarco-ippolito
authored andcommittedAug 19, 2024
lib: improve error message when index not found on cjs
PR-URL: #53859 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Marco Ippolito <[email protected]>
1 parent a2d74f4 commit c667fbd

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed
 

‎src/node_file.cc

+8-7
Original file line numberDiff line numberDiff line change
@@ -3143,6 +3143,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
31433143
return;
31443144
}
31453145

3146+
std::string package_initial_file = "";
3147+
31463148
ada::result<ada::url_aggregator> file_path_url;
31473149
std::optional<std::string> initial_file_path;
31483150
std::string file_path;
@@ -3165,6 +3167,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
31653167

31663168
node::url::FromNamespacedPath(&initial_file_path.value());
31673169

3170+
package_initial_file = *initial_file_path;
3171+
31683172
for (int i = 0; i < legacy_main_extensions_with_main_end; i++) {
31693173
file_path = *initial_file_path + std::string(legacy_main_extensions[i]);
31703174

@@ -3220,13 +3224,10 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
32203224
}
32213225
}
32223226

3223-
std::optional<std::string> module_path =
3224-
node::url::FileURLToPath(env, *package_json_url);
3225-
std::optional<std::string> module_base;
3227+
if (package_initial_file == "")
3228+
package_initial_file = *initial_file_path + ".js";
32263229

3227-
if (!module_path.has_value()) {
3228-
return;
3229-
}
3230+
std::optional<std::string> module_base;
32303231

32313232
if (args.Length() >= 3 && args[2]->IsString()) {
32323233
Utf8Value utf8_base_path(isolate, args[2]);
@@ -3251,7 +3252,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
32513252

32523253
THROW_ERR_MODULE_NOT_FOUND(isolate,
32533254
"Cannot find package '%s' imported from %s",
3254-
*module_path,
3255+
package_initial_file,
32553256
*module_base);
32563257
}
32573258

‎test/es-module/test-cjs-legacyMainResolve.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,28 @@ describe('legacyMainResolve', () => {
129129
);
130130
assert.throws(
131131
() => legacyMainResolve(packageJsonUrl, { main: null }, packageJsonUrl),
132-
{ code: 'ERR_MODULE_NOT_FOUND' },
132+
{ message: /index\.js/, code: 'ERR_MODULE_NOT_FOUND' },
133133
);
134134
});
135135

136136
it('should not crash when cannot resolve to a file that contains special chars', () => {
137137
const packageJsonUrl = pathToFileURL('/c/file%20with%20percents/package.json');
138138
assert.throws(
139139
() => 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' },
141154
);
142155
});
143156

0 commit comments

Comments
 (0)
Please sign in to comment.