JedWatson / react-select

The Select Component for React.js
https://react-select.com/
MIT License
27.49k stars 4.12k forks source link

Module augmentation not working with moduleResolution: bundler #5743

Open FabianFrank opened 11 months ago

FabianFrank commented 11 months ago

There seems to be an issue with module augmentation and the exports field in package.json added in https://github.com/JedWatson/react-select/pull/5559 and improved in https://github.com/JedWatson/react-select/pull/5626. Module augmentation as described at https://react-select.com/typescript#custom-select-props appears to no longer work since react-select/dist/declarations/src/Select appears to no longer be the path of the Select module when the exports field in package.json is used during module resolution. I was unfortunately not able to figure out what the new path is. Deleting the exports field from package.json fixes the issue and module augmentation works as expected again.

The relevant section of building with tsc's traceResolution option enabled:

Failing with exports field being present

======== Resolving module 'react-select/dist/declarations/src/Select' from '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/dist/types/module-augmentation.d.ts'. ========
Explicitly specified module resolution kind: 'Bundler'.
Resolving in CJS mode with conditions 'import', 'types'.
File '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/dist/types/package.json' does not exist according to earlier cached lookups.
File '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/dist/package.json' does not exist according to earlier cached lookups.
File '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/package.json' exists according to earlier cached lookups.
Loading module 'react-select/dist/declarations/src/Select' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.
Directory '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/dist/types/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/dist/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/node_modules' does not exist, skipping all lookups in it.
File '/Users/fabian/projects/brmlabs/brm/node_modules/react-select/package.json' exists according to earlier cached lookups.
Export specifier './dist/declarations/src/Select' does not exist in package.json scope at path '/Users/fabian/projects/brmlabs/brm/node_modules/react-select'.
Directory '/Users/fabian/projects/brmlabs/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/fabian/projects/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/fabian/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/node_modules' does not exist, skipping all lookups in it.
Directory '/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/dist/types/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/dist/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/node_modules' does not exist, skipping all lookups in it.
File '/Users/fabian/projects/brmlabs/brm/node_modules/react-select/package.json' exists according to earlier cached lookups.
Export specifier './dist/declarations/src/Select' does not exist in package.json scope at path '/Users/fabian/projects/brmlabs/brm/node_modules/react-select'.
Directory '/Users/fabian/projects/brmlabs/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/fabian/projects/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/fabian/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/node_modules' does not exist, skipping all lookups in it.
Directory '/node_modules' does not exist, skipping all lookups in it.
======== Module name 'react-select/dist/declarations/src/Select' was not resolved. ========

Succeeding with exports field being deleted from react-select's package.json:

Resolving in CJS mode with conditions 'import', 'types'.
File '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/dist/types/package.json' does not exist according to earlier cached lookups.
File '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/dist/package.json' does not exist according to earlier cached lookups.
File '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/package.json' exists according to earlier cached lookups.
Loading module 'react-select/dist/declarations/src/Select' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.
Directory '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/dist/types/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/dist/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/fabian/projects/brmlabs/brm/node_modules/chakra-react-select/node_modules' does not exist, skipping all lookups in it.
File '/Users/fabian/projects/brmlabs/brm/node_modules/react-select/package.json' exists according to earlier cached lookups.
File '/Users/fabian/projects/brmlabs/brm/node_modules/react-select/dist/declarations/src/Select.ts' does not exist.
File '/Users/fabian/projects/brmlabs/brm/node_modules/react-select/dist/declarations/src/Select.tsx' does not exist.
File '/Users/fabian/projects/brmlabs/brm/node_modules/react-select/dist/declarations/src/Select.d.ts' exists - use it as a name resolution result.
Resolving real path for '/Users/fabian/projects/brmlabs/brm/node_modules/react-select/dist/declarations/src/Select.d.ts', result '/Users/fabian/projects/brmlabs/brm/node_modules/react-select/dist/declarations/src/Select.d.ts'.
======== Module name 'react-select/dist/declarations/src/Select' was successfully resolved to '/Users/fabian/projects/brmlabs/brm/node_modules/react-select/dist/declarations/src/Select.d.ts' with Package ID 'react-select/dist/declarations/src/Select.d.ts@5.7.4'. ========

Seems like the root cause is ./dist/declarations/src/Select' missing from the exports map:

Export specifier './dist/declarations/src/Select' does not exist in package.json scope at path '/Users/fabian/projects/brmlabs/brm/node_modules/react-select'.
csandman commented 10 months ago

I am running into this issue as well on my wrapper for this package chakra-react-select. I'm not sure this has anything to do with the change in #5626 though, as I'm having the same issue after reverting to v5.7.2. I'm really not too familiar with the inner workings of module augmentation, is there something that changed with it when using moduleResolution: "bundler"?

I'm also having the issue with node16/nodenext.

image image