parcel-bundler / parcel

The zero configuration build tool for the web. 📦🚀
https://parceljs.org
MIT License
43.43k stars 2.26k forks source link

Crash on `package.json#imports` with default settings #9083

Open Andarist opened 1 year ago

Andarist commented 1 year ago

🐛 bug report

Parcel crashes at runtime when resolving a simple #is-development specifier in a package with package.json#imports without enabling packageExports: true:

TypeError: Cannot read properties of undefined (reading 'message')

https://github.com/bvaughn/suspense/pull/34#issuecomment-1582969380

🎛 Configuration (.babelrc, package.json, cli command)

defaults

🤔 Expected Behavior

It should not crash

😯 Current Behavior

The package.json#imports support is not enabled by default, see this line: https://github.com/parcel-bundler/parcel/blob/76aa20fc2f752fae9c7347f071ea457b112a5dad/packages/utils/node-resolver-rs/src/lib.rs#L482

So UnknownError is returned here: https://github.com/parcel-bundler/parcel/blob/76aa20fc2f752fae9c7347f071ea457b112a5dad/packages/utils/node-resolver-rs/src/lib.rs#L514

The error isn't mapped to anything useful by this handleError call: https://github.com/parcel-bundler/parcel/blob/76aa20fc2f752fae9c7347f071ea457b112a5dad/packages/utils/node-resolver-core/src/Wrapper.js#L153

So an empty diagnostics are returned here: https://github.com/parcel-bundler/parcel/blob/76aa20fc2f752fae9c7347f071ea457b112a5dad/packages/utils/node-resolver-core/src/Wrapper.js#L159

That is being converted to ThrowableDiagnostics here: https://github.com/parcel-bundler/parcel/blob/76aa20fc2f752fae9c7347f071ea457b112a5dad/packages/core/core/src/requests/PathRequest.js#L369-L376

but that expects non-empty array: https://github.com/parcel-bundler/parcel/blob/76aa20fc2f752fae9c7347f071ea457b112a5dad/packages/core/diagnostic/src/diagnostic.js#L202-L207

💁 Possible Solution

I think that a helpful diagnostic should be thrown here to suggest enabling packageExports: true. I also don't quite see a strong reason to not support package.json#imports by default so i think that enabling that is also an option but I also understand why you put package.json#exports and package.json#imports in a single bucket.

💻 Code Sample

Can be reproduced on this commit with pnpm run docs

🌍 Your Environment

Software Version(s)
Parcel 2.9.1
Node irrelevant
npm/Yarn irrelevant
Operating System irrelevant
devongovett commented 1 year ago

Hmm yeah that should probably be an InvalidSpecifier error instead of UnknownError. I'd also like to have more details in the error about how to enable package exports support.

niieani commented 11 months ago

Stumbled upon this when trying to build a library including node_modules.

parcel/core: Failed to resolve '#ansi-styles' from './node_modules/zx/node_modules/chalk/source/index.js'

  /Volumes/Projects/Software/toolchain/node_modules/zx/node_modules/chalk/source/index.js:1:24
  > 1 | import ansiStyles from '#ansi-styles';
  >   |                        ^^^^^^^^^^^^^^
    2 | import supportsColor from '#supports-color';
    3 | import { // eslint-disable-line import/order

@parcel/resolver-default: Cannot read properties of undefined (reading 'message')

Didn't realize packageExports: true needs to be enabled manually!