Skip to content

Conversation

@Ronitsabhaya75
Copy link

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

  3. import { useTheme } from "../utils/store";

  4. import { useTheme } from "@mui/material";

- 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.
Copilot AI review requested due to automatic review settings October 29, 2025 02:13
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Oct 29, 2025
Copy link

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 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>
@github-project-automation github-project-automation bot moved this from Not started to Done in PR Backlog Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant