Skip to content

fix: prioritize local imports over node_modules in auto-import suggestions(#62667)#62681

Closed
virajsabhaya23 wants to merge 5 commits intomicrosoft:mainfrom
virajsabhaya23:main
Closed

fix: prioritize local imports over node_modules in auto-import suggestions(#62667)#62681
virajsabhaya23 wants to merge 5 commits intomicrosoft:mainfrom
virajsabhaya23:main

Conversation

@virajsabhaya23
Copy link
Copy Markdown

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:

1. import { useTheme } from "@mui/material";
2. import { useTheme } from "../utils/store";

After

1. import { useTheme } from "../utils/store";
2. import { useTheme } from "@mui/material";

Copilot AI review requested due to automatic review settings October 28, 2025 00:17
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Oct 28, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a 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

virajsabhaya23 and others added 4 commits October 27, 2025 19:19
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@github-project-automation github-project-automation bot moved this from Not started to Done in PR Backlog Mar 24, 2026
@typescript-bot
Copy link
Copy Markdown
Collaborator

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:

  • For crash bugfixes or language service improvements, PRs are currently accepted at the typescript-go repo
  • Changes to type system behavior should wait until after 7.0, at which point mainline TypeScript development will resume in this repository with the Go codebase
  • Library file updates (lib.d.ts etc) continue to live in this repo or the DOM Generator repo as appropriate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants