Look for package.json main fields as part of paths module specifier resolution#41864
Closed
andrewbranch wants to merge 2 commits into
Closed
Look for package.json main fields as part of paths module specifier resolution#41864andrewbranch wants to merge 2 commits into
paths module specifier resolution#41864andrewbranch wants to merge 2 commits into
Conversation
| else if (host?.readFile && host.fileExists(combinePaths(pathsBasePath!, pattern, "package.json"))) { | ||
| // If the path points to a directory with a package.json, see if we can resolve to its 'main' field. | ||
| try { | ||
| const { main } = JSON.parse(host.readFile(combinePaths(pathsBasePath!, pattern, "package.json"))!); |
Member
There was a problem hiding this comment.
why just main.. I think during resoution we also look for types and typings field and that is preferred over main ?
Member
Author
|
After sitting on this for a little while, I don’t think I want to merge it. I really don’t want to add file IO to module specifier resolution when it could instead be done during config file parsing, or we could suggest/enforce that a non-wildcard path mapping resolve to a file, not a directory. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #41152
Previously, when suggesting module specifiers for auto-imports, we would only look at package.json
mainfields if the path to the module was actually insidenode_modules. This meant that if you have a monorepo that uses a node_modules symlink strategy, like yarn workspaces or lerna, everything was great, but if you just rely on tsconfigpathsaliases, you could not point an alias at a package root and expect it to work:While this change seems like an improvement, I’m not convinced it’s super valuable, since the same effect could be achieved if the user had just specified the full path to the
mainfield in theirpathsconfig, and it would be less work for the compiler and less code to maintain. I don’t know that it’s our goal for module specifier resolution to be a perfect inverse of module resolution, so there’s some room for debate over how far we should take the heuristics, especially when they involve file system reads like this one.