Fix/prioritize local imports#62688
Conversation
- Removed duplicate local vs external comparison that conflicted with compareLocalVsExternal - Fixed test expectations in importNameCodeFixOptionalImport1.ts to expect local imports first - Fixed formatting issues (trailing newline) This resolves the test worker crashes and makes all CI checks pass.
There was a problem hiding this comment.
Pull Request Overview
This PR modifies the TypeScript import fix logic to prioritize local/relative imports over external node_modules imports when auto-importing symbols. The change ensures that when the same symbol is available from both a local file and a node_modules package, the local import is suggested first.
- Added a new comparison function to prioritize local imports over external imports
- Updated the import comparison logic to check local vs external first before other heuristics
- Updated existing test and added new comprehensive test to verify the prioritization behavior
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/services/codefixes/importFixes.ts | Added compareLocalVsExternal function and integrated it into compareModuleSpecifiers to prioritize local imports |
| tests/cases/fourslash/importNameCodeFixOptionalImport1.ts | Swapped the order of expected import fixes to reflect local imports being prioritized first |
| tests/cases/fourslash/importFixesPrioritizeLocal.ts | Added comprehensive test covering multiple scenarios where local imports should be prioritized over node_modules |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
This PR doesn't have any linked issues. Please open an issue that references this PR. From there we can discuss and prioritise. |
|
With 6.0 out as the final release vehicle for this codebase, we're closing all PRs that don't fit the merge criteria for post-6.0 patches. If you think this was a mistake and this PR fits the post-6.0 patch criteria, please post to the 6.0 iteration issue with details (specifically, which PR and which patch criteria it satisfies). Next steps for PRs:
|
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".
solves #62667
Before:
import { useTheme } from "@mui/material";
import { useTheme } from "../utils/store";
After
import { useTheme } from "../utils/store";
import { useTheme } from "@mui/material";