Skip to content

Commit

Permalink
module: fix main lookup regression from nodejs#18728
Browse files Browse the repository at this point in the history
PR-URL: nodejs#18788
Refs: nodejs#18728
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
guybedford authored and BridgeAR committed Feb 16, 2018
1 parent 61e3e6d commit 7748865
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
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"
}

0 comments on commit 7748865

Please sign in to comment.