Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: improve error message when index not found on cjs #53859

Merged
merged 2 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3072,6 +3072,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
return;
}

std::string package_initial_file = "";

ada::result<ada::url_aggregator> file_path_url;
std::optional<std::string> initial_file_path;
std::string file_path;
Expand All @@ -3094,6 +3096,8 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& 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]);

Expand Down Expand Up @@ -3149,13 +3153,10 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
}
}

std::optional<std::string> module_path =
node::url::FileURLToPath(env, *package_json_url);
std::optional<std::string> module_base;
if (package_initial_file == "")
package_initial_file = *initial_file_path + ".js";

if (!module_path.has_value()) {
return;
}
std::optional<std::string> module_base;

if (args.Length() >= 3 && args[2]->IsString()) {
Utf8Value utf8_base_path(isolate, args[2]);
Expand All @@ -3180,7 +3181,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {

THROW_ERR_MODULE_NOT_FOUND(isolate,
"Cannot find package '%s' imported from %s",
*module_path,
package_initial_file,
*module_base);
}

Expand Down
17 changes: 15 additions & 2 deletions test/es-module/test-cjs-legacyMainResolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,28 @@ describe('legacyMainResolve', () => {
);
assert.throws(
() => legacyMainResolve(packageJsonUrl, { main: null }, packageJsonUrl),
{ code: 'ERR_MODULE_NOT_FOUND' },
{ message: /index\.js/, code: 'ERR_MODULE_NOT_FOUND' },
);
});

it('should not crash when cannot resolve to a file that contains special chars', () => {
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' },
);
});

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' },
);
});

Expand Down
Loading