Skip to content

Look for package.json main fields as part of paths module specifier resolution#41864

Closed
andrewbranch wants to merge 2 commits into
microsoft:masterfrom
andrewbranch:bug/41152
Closed

Look for package.json main fields as part of paths module specifier resolution#41864
andrewbranch wants to merge 2 commits into
microsoft:masterfrom
andrewbranch:bug/41152

Conversation

@andrewbranch
Copy link
Copy Markdown
Member

Fixes #41152

Previously, when suggesting module specifiers for auto-imports, we would only look at package.json main fields if the path to the module was actually inside node_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 tsconfig paths aliases, you could not point an alias at a package root and expect it to work:

tsconfig.json - { "paths": { "@foo": ["./foo"] } }
foo/
  package.json - { "main": "nonStandardName.js" }
  nonStandardName.ts - `export class Foo {}`
bar/
  bar.ts - `import { Foo } from "@foo"` resolves, but only relative imports are suggested

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 main field in their paths config, 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.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Dec 7, 2020
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"))!);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why just main.. I think during resoution we also look for types and typings field and that is preferred over main ?

@andrewbranch
Copy link
Copy Markdown
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.

@andrewbranch andrewbranch deleted the bug/41152 branch January 25, 2021 17:43
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

TypeScript custom paths to packages are not used by quickfix to suggest imports

4 participants