Skip to content

fix(auto-import): don't suggest # imports that only resolve via condition fallback - #64177

Open
Marwan (marwan562) wants to merge 2 commits into
microsoft:mainfrom
marwan562:fix/64171-autoimport-invalid-specifier
Open

fix(auto-import): don't suggest # imports that only resolve via condition fallback#64177
Marwan (marwan562) wants to merge 2 commits into
microsoft:mainfrom
marwan562:fix/64171-autoimport-invalid-specifier

Conversation

@marwan562

Copy link
Copy Markdown

With imports": { "#*": { "node": "./dist/*/index.js", "default": "./dist/*.js" } } and nodenext, auto-import offered both #utils/summarize and #utils/summarize/summarize. The second only resolves in TS by falling back from node to default after the node target misses. Node picks the first matching condition and throws ERR_MODULE_NOT_FOUND instead, so picking that suggestion crashes at runtime.

Reverse mapping in tryGetModuleNameFromExportsOrImports did the same fallback as the resolver. It now mirrors Node first-match: when a runtime-active condition misses, later conditions aren't tried. types/types@ don't block since Node ignores them, nested objects with no active runtime key still fall through, and array fallback still works.

Added cases alongside the existing ones in tsc/internal/modulespecifiers/specifiers_test.go.

Fixes #64171
Related #62439, #50762

Disclosure: I used AI assistance to draft this patch and reviewed and tested it myself.

…tion fallback

Reverse mapping in tryGetModuleNameFromExportsOrImports mirrored TS resolver fallback across conditions, suggesting specifiers like #utils/summarize/summarize that resolve via default only after node misses. Node picks first matching condition and throws on miss, so such suggestions crash at runtime with ERR_MODULE_NOT_FOUND.

Mirror Node first-match semantics: when a runtime-active condition misses, block later conditions. Types-only conditions are ignored at runtime and don't block. Handles nested conditionals without active runtime keys and preserves array fallback.

Closes microsoft#64171
Copilot AI balanced review requested due to automatic review settings September 5, 2026 04:41
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Sep 5, 2026
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Sep 5, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Two moderate resolution-semantics issues could still produce incorrect auto-import behavior.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Updates auto-import reverse mapping to better match Node.js conditional import resolution.

Changes:

  • Stops fallback after matching runtime conditions.
  • Adds conditional, nested, types-only, and array mapping tests.
File summaries
File Review
tsc/internal/modulespecifiers/specifiers.go Requires fixes for deeper conditional objects and Node array-target semantics.
tsc/internal/modulespecifiers/specifiers_test.go Adds regression coverage, but incorrectly expects file-existence fallback for arrays.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Balanced

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread tsc/internal/modulespecifiers/specifiers.go
Comment thread tsc/internal/modulespecifiers/specifiers.go Outdated
@marwan562

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

…rays

Address Copilot review: arrays select first valid string target at runtime (no file-existence fallback), and deeper nested conditionals with inactive keys return undefined and should fallback. Switch reverse mapping to tri-state (matched/blocked/skipped) so terminal misses block later fallback while undefined continues.
@marwan562

Copy link
Copy Markdown
Author

Addressed both Copilot comments:

  • Arrays now treat first valid string as terminal (no file-existence fallback). Second-element match test corrected to expect blocked, first-element still valid. Undefined array entries (inactive conditional) still fall through.
  • Nested conditionals now track terminal vs undefined via tri-state return, so { node: { import: { browser: ... } }, default } with inactive browser correctly falls back to default.

Added deeper-nesting + array-undefined-first cases. All modulespecifiers tests pass.

Copilot review

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

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

[Auto-import] Quick Fix suggests invalid module specifiers that fail to resolve at runtime

2 participants