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 1 commit
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
9 changes: 2 additions & 7 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3149,14 +3149,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
}
}

std::optional<std::string> module_path =
node::url::FileURLToPath(env, *package_json_url);
std::string module_path = *initial_file_path + ".js";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the .js correct here? If there was an index.json, or index.node that would also work

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to change to use the main value when it's defined, otherwise, fallback to index.js.

std::optional<std::string> 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 =
Expand All @@ -3180,7 +3175,7 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {

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

Expand Down
4 changes: 2 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,15 @@ 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' },
);
});

Expand Down
Loading