Skip to content

Commit 03f3532

Browse files
H4adtargos
authored andcommitted
lib: improve error message when index not found on cjs
PR-URL: #53859 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 8bf9960 commit 03f3532

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
@@ -3083,6 +3083,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
30833083
return;
30843084
}
30853085

3086+
std::string package_initial_file = "";
3087+
30863088
ada::result<ada::url_aggregator> file_path_url;
30873089
std::optional<std::string> initial_file_path;
30883090
std::string file_path;
@@ -3105,6 +3107,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
31053107

31063108
FromNamespacedPath(&initial_file_path.value());
31073109

3110+
package_initial_file = *initial_file_path;
3111+
31083112
for (int i = 0; i < legacy_main_extensions_with_main_end; i++) {
31093113
file_path = *initial_file_path + std::string(legacy_main_extensions[i]);
31103114

@@ -3160,13 +3164,10 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
31603164
}
31613165
}
31623166

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";
31663169

3167-
if (!module_path.has_value()) {
3168-
return;
3169-
}
3170+
std::optional<std::string> module_base;
31703171

31713172
if (args.Length() >= 3 && args[2]->IsString()) {
31723173
Utf8Value utf8_base_path(isolate, args[2]);
@@ -3191,7 +3192,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
31913192

31923193
THROW_ERR_MODULE_NOT_FOUND(isolate,
31933194
"Cannot find package '%s' imported from %s",
3194-
*module_path,
3195+
package_initial_file,
31953196
*module_base);
31963197
}
31973198

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)