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

module: fix main lookup regression from #18728 #18788

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 7 additions & 5 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ const PackageConfig& GetPackageConfig(Environment* env,
}

auto entry = env->package_json_cache.emplace(path,
PackageConfig { Exists::Yes, IsValid::Yes, has_main, "" });
PackageConfig { Exists::Yes, IsValid::Yes, has_main, main_std });
return entry.first->second;
}

Expand Down Expand Up @@ -585,13 +585,15 @@ Maybe<URL> ResolveMain(Environment* env, const URL& search) {
GetPackageConfig(env, pkg.ToFilePath());
// Note invalid package.json should throw in resolver
// currently we silently ignore which is incorrect
if (!pjson.exists || !pjson.is_valid || !pjson.has_main) {
if (pjson.exists == Exists::No ||
pjson.is_valid == IsValid::No ||
pjson.has_main == HasMain::No) {
return Nothing<URL>();
}
if (!ShouldBeTreatedAsRelativeOrAbsolutePath(pjson.main)) {
return Resolve(env, "./" + pjson.main, search);
return Resolve(env, "./" + pjson.main, search, IgnoreMain);
}
return Resolve(env, pjson.main, search);
return Resolve(env, pjson.main, search, IgnoreMain);
}

Maybe<URL> ResolveModule(Environment* env,
Expand All @@ -602,7 +604,7 @@ Maybe<URL> ResolveModule(Environment* env,
do {
dir = parent;
Maybe<URL> check =
Resolve(env, "./node_modules/" + specifier, dir, IgnoreMain);
Resolve(env, "./node_modules/" + specifier, dir, CheckMain);
if (!check.IsNothing()) {
const size_t limit = specifier.find('/');
const size_t spec_len =
Expand Down
6 changes: 6 additions & 0 deletions test/es-module/test-esm-main-lookup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Flags: --experimental-modules
/* eslint-disable required-modules */
import assert from 'assert';
import main from '../fixtures/es-modules/pjson-main';

assert.strictEqual(main, 'main');
1 change: 1 addition & 0 deletions test/fixtures/es-modules/pjson-main/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'main';
3 changes: 3 additions & 0 deletions test/fixtures/es-modules/pjson-main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "main.js"
}