-
Notifications
You must be signed in to change notification settings - Fork 13.1k
fix: prioritize local imports over node_modules in auto-import suggestions(#62667) #62681
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
base: main
Are you sure you want to change the base?
Conversation
|
@virajsabhaya23 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes auto-import suggestions to prioritize local/relative imports over node_modules imports when the same symbol is available from both sources. The fix adds a compareLocalVsExternal() function that ensures local imports are ranked first in the quick fix menu, improving developer experience by surfacing the most contextually relevant import option.
Key Changes:
- Introduced
compareLocalVsExternal()helper function to distinguish local vs external module imports - Modified
compareModuleSpecifiers()to prioritize local imports before evaluating other comparison criteria - Added comprehensive fourslash test validating the new behavior with real-world scenarios (MUI and Zustand)
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/services/codefixes/importFixes.ts | Implements the comparison logic to prioritize local imports over node_modules |
| tests/cases/fourslash/importFixesPrioritizeLocal.ts | Adds test coverage for the local import prioritization feature |
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Local imports are not prioritized over node_modules imports when multiple import options exist for the same symbol.
The reason is that compareModuleSpecifiers was not considering module locality (local vs external) before comparing other factors like package.json filters and path complexity, so external packages would often appear first in the quick fix menu even when local project exports were more relevant.This fix adds compareLocalVsExternal() which runs as the first comparison in compareModuleSpecifiers(), ensuring imports with moduleSpecifierKind !== "node_modules" are prioritized over moduleSpecifierKind === "node_modules".
Before:
After