From 4bece07740878577be9570efe47fde66d289b5ff Mon Sep 17 00:00:00 2001 From: Rishabh Singh Date: Mon, 30 Nov 2020 05:21:47 +0530 Subject: [PATCH] [Fix] throw an error for an invalid explicit `main` with a missing `./index.js` file Fixes #223. Fixes #143. --- lib/async.js | 8 ++++- lib/sync.js | 12 +++++-- test/resolver.js | 46 ++++++++++++++++++++++++ test/resolver/empty_main/index.js | 0 test/resolver/empty_main/package.json | 3 ++ test/resolver/missing_index/package.json | 3 ++ test/resolver/missing_main/index.js | 0 test/resolver/missing_main/package.json | 3 ++ test/resolver/null_main/index.js | 0 test/resolver/null_main/package.json | 3 ++ 10 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 test/resolver/empty_main/index.js create mode 100644 test/resolver/empty_main/package.json create mode 100644 test/resolver/missing_index/package.json create mode 100644 test/resolver/missing_main/index.js create mode 100644 test/resolver/missing_main/package.json create mode 100644 test/resolver/null_main/index.js create mode 100644 test/resolver/null_main/package.json diff --git a/lib/async.js b/lib/async.js index d192c102..ed7cc7c1 100644 --- a/lib/async.js +++ b/lib/async.js @@ -267,7 +267,13 @@ module.exports = function resolve(x, options, callback) { loadAsDirectory(dir, pkg, function (err, n, pkg) { if (err) return cb(err); if (n) return cb(null, n, pkg); - loadAsFile(path.join(x, 'index'), pkg, cb); + loadAsFile(path.join(x, 'index'), pkg, function (err, m, pkg) { + if (err) return cb(err); + if (m) return cb(null, m, pkg); + var incorrectMainError = new Error("Cannot find module '" + path.resolve(x, pkg.main) + "'. Please verify that the package.json has a valid \"main\" entry"); + incorrectMainError.code = 'INCORRECT_PACKAGE_MAIN'; + return cb(incorrectMainError); + }); }); }); return; diff --git a/lib/sync.js b/lib/sync.js index b9b9a380..b7c5c380 100644 --- a/lib/sync.js +++ b/lib/sync.js @@ -168,11 +168,17 @@ module.exports = function resolveSync(x, options) { pkg.main = 'index'; } try { - var m = loadAsFileSync(path.resolve(x, pkg.main)); + var mainPath = path.resolve(x, pkg.main); + var m = loadAsFileSync(mainPath); if (m) return m; - var n = loadAsDirectorySync(path.resolve(x, pkg.main)); + var n = loadAsDirectorySync(mainPath); if (n) return n; - } catch (e) {} + var checkIndex = loadAsFileSync(path.resolve(x, 'index')); + if (checkIndex) return checkIndex; + } catch (e) { } + var incorrectMainError = new Error("Cannot find module '" + path.resolve(x, pkg.main) + "'. Please verify that the package.json has a valid \"main\" entry"); + incorrectMainError.code = 'INCORRECT_PACKAGE_MAIN'; + throw incorrectMainError; } } diff --git a/test/resolver.js b/test/resolver.js index e7575834..95e3af87 100644 --- a/test/resolver.js +++ b/test/resolver.js @@ -272,6 +272,18 @@ test('path iterator', function (t) { }); }); +test('empty main', function (t) { + t.plan(1); + + var resolverDir = path.join(__dirname, 'resolver'); + var dir = path.join(resolverDir, 'empty_main'); + + resolve('./empty_main', { basedir: resolverDir }, function (err, res, pkg) { + if (err) t.fail(err); + t.equal(res, path.join(dir, 'index.js')); + }); +}); + test('incorrect main', function (t) { t.plan(1); @@ -284,6 +296,40 @@ test('incorrect main', function (t) { }); }); +test('missing index', function (t) { + t.plan(2); + + var resolverDir = path.join(__dirname, 'resolver'); + resolve('./missing_index', { basedir: resolverDir }, function (err, res, pkg) { + t.ok(err instanceof Error); + t.equal(err && err.code, 'INCORRECT_PACKAGE_MAIN', 'error has correct error code'); + }); +}); + +test('missing main', function (t) { + t.plan(1); + + var resolverDir = path.join(__dirname, 'resolver'); + var dir = path.join(resolverDir, 'missing_main'); + + resolve('./missing_main', { basedir: resolverDir }, function (err, res, pkg) { + if (err) t.fail(err); + t.equal(res, path.join(dir, 'index.js')); + }); +}); + +test('null main', function (t) { + t.plan(1); + + var resolverDir = path.join(__dirname, 'resolver'); + var dir = path.join(resolverDir, 'null_main'); + + resolve('./null_main', { basedir: resolverDir }, function (err, res, pkg) { + if (err) t.fail(err); + t.equal(res, path.join(dir, 'index.js')); + }); +}); + test('without basedir', function (t) { t.plan(1); diff --git a/test/resolver/empty_main/index.js b/test/resolver/empty_main/index.js new file mode 100644 index 00000000..e69de29b diff --git a/test/resolver/empty_main/package.json b/test/resolver/empty_main/package.json new file mode 100644 index 00000000..dbb176e6 --- /dev/null +++ b/test/resolver/empty_main/package.json @@ -0,0 +1,3 @@ +{ + "main": "" +} \ No newline at end of file diff --git a/test/resolver/missing_index/package.json b/test/resolver/missing_index/package.json new file mode 100644 index 00000000..f2c75cf8 --- /dev/null +++ b/test/resolver/missing_index/package.json @@ -0,0 +1,3 @@ +{ + "main": "index.js" +} \ No newline at end of file diff --git a/test/resolver/missing_main/index.js b/test/resolver/missing_main/index.js new file mode 100644 index 00000000..e69de29b diff --git a/test/resolver/missing_main/package.json b/test/resolver/missing_main/package.json new file mode 100644 index 00000000..0bf896ce --- /dev/null +++ b/test/resolver/missing_main/package.json @@ -0,0 +1,3 @@ +{ + "notmain": "index.js" +} \ No newline at end of file diff --git a/test/resolver/null_main/index.js b/test/resolver/null_main/index.js new file mode 100644 index 00000000..e69de29b diff --git a/test/resolver/null_main/package.json b/test/resolver/null_main/package.json new file mode 100644 index 00000000..b82890e2 --- /dev/null +++ b/test/resolver/null_main/package.json @@ -0,0 +1,3 @@ +{ + "main": null +} \ No newline at end of file