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

Throwing error for an 'invalid main entry' #234

Merged
merged 1 commit into from
Dec 23, 2020

Conversation

Cheesetouched
Copy link
Contributor

@Cheesetouched Cheesetouched commented Nov 30, 2020

Fixes #223 & Possibly Fixes #143 too.

Fix

The thrown error now suggests the possibility of an invalid main entry in the package.json.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This will need some regression tests - and I'd want to be convinced (ideally, via tests) that the swallowed errors are only coming from path.resolve(x, pkg.main) and not from loadAsDirectorySync, etc.

@ljharb
Copy link
Member

ljharb commented Nov 30, 2020

You can also see that tests are currently failing - https://github.com/browserify/resolve/pull/234/checks?check_run_id=1471328733 - which implies that the intentional behavior with an incorrect "main" is to silently fall back to index.js, when present.

Specifically, when I put this directory inside a node_modules folder, and hop into the node repl and do require.resolve('incorrect_main'), i get $PWD/node_modules/incorrect_main/index.js, not an error. When I remove $PWD/node_modules/incorrect_main/index.js, however, I get Error: Cannot find module '$PWD/node_modules/incorrect_main/wrong.js'. Please verify that the package.json has a valid "main" entry.

So, I think you specifically have to only error in the case where an explicit main and an implicit main are not found.

@Cheesetouched
Copy link
Contributor Author

Yep, makes sense to me. I'll focus on isolating explicit and implicit main conditions first. Will fallback here if something doesn't add up.

@Cheesetouched
Copy link
Contributor Author

@ljharb There. That should do the trick. Thoughts?

P.S Not sure why that 1 test is failing. Could you please explain?

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need an async fix also.

lib/sync.js Outdated
Comment on lines 170 to 182
var m = loadAsFileSync(path.resolve(x, pkg.main));
if (m) return m;
var n = loadAsDirectorySync(path.resolve(x, pkg.main));
if (n) return n;
else {
var checkIndex = loadAsFileSync(path.resolve(x, 'index.js'));
if (checkIndex) return checkIndex;
else {
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;
}
}
Copy link
Member

@ljharb ljharb Dec 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var m = loadAsFileSync(path.resolve(x, pkg.main));
if (m) return m;
var n = loadAsDirectorySync(path.resolve(x, pkg.main));
if (n) return n;
else {
var checkIndex = loadAsFileSync(path.resolve(x, 'index.js'));
if (checkIndex) return checkIndex;
else {
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;
}
}
try {
var mainPath = path.resolve(x, pkg.main);
var m = loadAsFileSync(mainPath);
if (m) return m;
var n = loadAsDirectorySync(mainPath);
if (n) return n;
var checkIndex = loadAsFileSync(path.resolve(x, 'index.js'));
if (checkIndex) return checkIndex;
} catch (e) {}
var incorrectMainError = new Error("Cannot find module '" + mainPath + "'. Please verify that the package.json has a valid \"main\" entry");
incorrectMainError.code = 'INCORRECT_PACKAGE_MAIN';
throw incorrectMainError;

@Cheesetouched
Copy link
Contributor Author

Cheesetouched commented Dec 4, 2020

@ljharb I implemented the changes for async.js as well. Can you do a quick review?

@ljharb
Copy link
Member

ljharb commented Dec 4, 2020

@Cheesetouched seems good, but tests would really be persuasive :-)

@Cheesetouched
Copy link
Contributor Author

Absolutely. I am on it.

@Cheesetouched
Copy link
Contributor Author

@ljharb See if this looks good to you? Feel free to suggest more changes.

test/resolver.js Outdated Show resolved Hide resolved
@Cheesetouched
Copy link
Contributor Author

@ljharb Added relevant test cases. Ready for review.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good! I'm still hesitant to backport this to v1, and will think about it more, but at least we can land this in the next v2 prerelease :-)

@ljharb ljharb merged commit 4bece07 into browserify:master Dec 23, 2020
@ljharb ljharb deleted the invalid-main-entry-error branch December 23, 2020 22:28
@ljharb ljharb restored the invalid-main-entry-error branch December 23, 2020 22:28
@ljharb ljharb deleted the invalid-main-entry-error branch December 23, 2020 22:29
@ljharb
Copy link
Member

ljharb commented Dec 23, 2020

whoops, merged a little early, but i think we're ok :-)

@Cheesetouched
Copy link
Contributor Author

@ljharb Any issues? Happy to take to care of it. Do let me know.

@ljharb
Copy link
Member

ljharb commented Dec 24, 2020

nah, i just meant to wait til all the status checks had finished, and i pulled the trigger a bit prematurely. everything still passed tho.

ljharb added a commit to ljharb/resolve that referenced this pull request Feb 11, 2021
Changes since v2.0.0-next.2:

 - [New] add `readPackage` and `readPackageSync` (browserify#236)
 - [Fix] throw an error for an invalid explicit `main` with a missing `./index.js` file (browserify#234)
 - [meta] create SECURITY.md
 - [Deps] update `is-core-module`
 - [Dev Deps] update `eslint`, `@ljharb/eslitn-config`, `array.prototype.map`, `aud`, `tape`

Including all changes in v1.15.1 - v1.19.0:

v1.19.0

 - [New] `sync`/`async`: add 'includeCoreModules' option (browserify#233)
 - [readme] Add possible error types (browserify#232)
 - [Deps] update `is-core-module`
 - [Dev Deps] update `aud`, `eslint`
 - [meta] add Automatic Rebase and Require Allow Edits workflows
 - [Tests] comment out node 15 in appveyor; it’s not available yet
 - [Tests] add node 15 to appveyor, fix "latest npm" logic
 - [Tests] migrate tests to Github Actions

v1.18.1

 - [Fix] `core`: remove console warning on require, since the main entry point requires it
 - [Refactor] avoid using extensions in require paths
 - [meta] update auto-generated `core.json`
 - [meta] add `eclint` and `.editorconfig`
 - [Dev Deps] update `eslint`
 - [Dev Deps] add `aud` in `posttest`

v1.18.0

 - [New] extract `isCore` implementation to `is-core-module`
 - [New] add new core modules that will be in node v15
 - [readme] soft-deprecate `resolve.isCore`
 - [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `tape`

v1.17.0

 - [New] `sync`/`async`: add `realpath`/`realpathSync` options (browserify#218)
 - [Dev Deps] update `tape`

v1.16.1

 - [patch] when a non-node `fs` is broken and lacks `realpath`/`realpathSync`, do not crash (browserify#220)

v1.16.0

 - [New] `core`: `fs/promises` is a core module again in node 14+ (f6473e2)
 - [patch] `sync`/`async`: use native `realpath` if available to unwrap symlinks (browserify#217)

v1.15.1

 - [Fix] correct behavior when requiring `.` with same name (browserify#212)
 - [Dev Deps] update `@ljharb/eslint-config`
 - [Tests] allow node 5 on windows to fail due to npm registry bug
ljharb added a commit that referenced this pull request Feb 11, 2021
Changes since v2.0.0-next.2:

 - [New] add `readPackage` and `readPackageSync` (#236)
 - [Fix] throw an error for an invalid explicit `main` with a missing `./index.js` file (#234)
 - [meta] create SECURITY.md
 - [meta] do not publish github action workflow files
 - [Deps] update `is-core-module`
 - [Dev Deps] update `eslint`, `@ljharb/eslitn-config`, `array.prototype.map`, `aud`, `tape`

Including all changes in v1.15.1 - v1.19.0:

v1.19.0

 - [New] `sync`/`async`: add 'includeCoreModules' option (#233)
 - [readme] Add possible error types (#232)
 - [Deps] update `is-core-module`
 - [Dev Deps] update `aud`, `eslint`
 - [meta] add Automatic Rebase and Require Allow Edits workflows
 - [Tests] comment out node 15 in appveyor; it’s not available yet
 - [Tests] add node 15 to appveyor, fix "latest npm" logic
 - [Tests] migrate tests to Github Actions

v1.18.1

 - [Fix] `core`: remove console warning on require, since the main entry point requires it
 - [Refactor] avoid using extensions in require paths
 - [meta] update auto-generated `core.json`
 - [meta] add `eclint` and `.editorconfig`
 - [Dev Deps] update `eslint`
 - [Dev Deps] add `aud` in `posttest`

v1.18.0

 - [New] extract `isCore` implementation to `is-core-module`
 - [New] add new core modules that will be in node v15
 - [readme] soft-deprecate `resolve.isCore`
 - [Dev Deps] update `eslint`, `@ljharb/eslint-config`, `tape`

v1.17.0

 - [New] `sync`/`async`: add `realpath`/`realpathSync` options (#218)
 - [Dev Deps] update `tape`

v1.16.1

 - [patch] when a non-node `fs` is broken and lacks `realpath`/`realpathSync`, do not crash (#220)

v1.16.0

 - [New] `core`: `fs/promises` is a core module again in node 14+ (f6473e2)
 - [patch] `sync`/`async`: use native `realpath` if available to unwrap symlinks (#217)

v1.15.1

 - [Fix] correct behavior when requiring `.` with same name (#212)
 - [Dev Deps] update `@ljharb/eslint-config`
 - [Tests] allow node 5 on windows to fail due to npm registry bug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Error messages are not precise enough loadNodeModules() should be more clear about what went wrong
2 participants