Skip to content
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
Prev Previous commit
better error message
  • Loading branch information
H4ad committed Jul 16, 2024
commit 6090a922cc7888cbeb890b867908baa4eff92b9b
10 changes: 8 additions & 2 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,7 +3153,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
}
}

std::string module_path = *initial_file_path + ".js";
if (package_initial_file == "")
package_initial_file = *initial_file_path + ".js";

std::optional<std::string> module_base;

if (args.Length() >= 3 && args[2]->IsString()) {
Expand All @@ -3175,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
13 changes: 13 additions & 0 deletions test/es-module/test-cjs-legacyMainResolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ describe('legacyMainResolve', () => {
);
});

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

it('should throw when cannot resolve to a file (base not defined)', () => {
const packageJsonUrl = pathToFileURL(
path.resolve(
Expand Down