From 39e032fe86d826e34dc5690c092ac0c0f6fda8a3 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Wed, 14 Feb 2018 22:55:49 +0200 Subject: [PATCH] module: fix main lookup regression from #18728 Backport-PR-URL: https://github.com/nodejs/node/pull/18923 PR-URL: https://github.com/nodejs/node/pull/18788 Refs: https://github.com/nodejs/node/pull/18728 Reviewed-By: Benjamin Gruenbaum Reviewed-By: James M Snell --- src/module_wrap.cc | 12 +++++++----- test/es-module/test-esm-main-lookup.mjs | 6 ++++++ test/fixtures/es-modules/pjson-main/main.js | 1 + test/fixtures/es-modules/pjson-main/package.json | 3 +++ 4 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 test/es-module/test-esm-main-lookup.mjs create mode 100644 test/fixtures/es-modules/pjson-main/main.js create mode 100644 test/fixtures/es-modules/pjson-main/package.json diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 6cf792deef76e0..175475b738b2aa 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -534,7 +534,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; } @@ -575,13 +575,15 @@ Maybe 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(); } 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 ResolveModule(Environment* env, @@ -592,7 +594,7 @@ Maybe ResolveModule(Environment* env, do { dir = parent; Maybe 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 = diff --git a/test/es-module/test-esm-main-lookup.mjs b/test/es-module/test-esm-main-lookup.mjs new file mode 100644 index 00000000000000..7c81cb647cff38 --- /dev/null +++ b/test/es-module/test-esm-main-lookup.mjs @@ -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'); diff --git a/test/fixtures/es-modules/pjson-main/main.js b/test/fixtures/es-modules/pjson-main/main.js new file mode 100644 index 00000000000000..dfdd47b877319c --- /dev/null +++ b/test/fixtures/es-modules/pjson-main/main.js @@ -0,0 +1 @@ +module.exports = 'main'; diff --git a/test/fixtures/es-modules/pjson-main/package.json b/test/fixtures/es-modules/pjson-main/package.json new file mode 100644 index 00000000000000..c13b8cf6acfd33 --- /dev/null +++ b/test/fixtures/es-modules/pjson-main/package.json @@ -0,0 +1,3 @@ +{ + "main": "main.js" +}