Skip to content

Implement resolveFromTypeRoot fallback in module resolution#3306

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/fix-vscode-extension-typeroots
Draft

Implement resolveFromTypeRoot fallback in module resolution#3306
Copilot wants to merge 3 commits intomainfrom
copilot/fix-vscode-extension-typeroots

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 31, 2026

When typeRoots points to a non-ancestor directory (e.g. ../assets/node_modules/@types), imports of packages whose types live exclusively in those custom roots failed with TS2307: Cannot find module — even though tsc resolves them correctly.

Root cause

TypeScript's node-like module resolver falls back to resolveFromTypeRoot after exhausting node_modules traversal. In the Go port, this call was stubbed out:

if r.extensions&extensionsDeclaration != 0 {
    // !!!
    // if resolved := r.resolveFromTypeRoot(); !resolved.shouldContinueSearching() {
    //     return r.createResolvedModuleHandlingSymlink(resolved)
    // }
}

Changes

  • internal/module/resolver.go — Implements resolveFromTypeRoot on resolutionState, mirroring the TypeScript original: iterates compilerOptions.TypeRoots, attempts loadModuleFromFile then loadNodeModuleFromDirectory with extensionsDeclaration, and attaches packageId from the nearest package.json. Only activates when TypeRoots is explicitly configured (not the default computed roots). Uncomments the call site in resolveNodeLikeWorker.

Baseline impact

Three compiler test baselines now match the TypeScript reference exactly:

Test Before After
moduleResolutionAsTypeReferenceDirective TS2307 error, a2: any no error, a2: number
moduleResolutionAsTypeReferenceDirectiveScoped 6 errors 3 errors (matches TS)
moduleResolutionAsTypeReferenceDirectiveAmbient trace mismatch trace matches TS

Copilot AI and others added 2 commits April 1, 2026 00:03
…oots

When a module cannot be found via regular node_modules resolution, fall back
to looking in the configured typeRoots (from tsconfig.json). This implements
the resolveFromTypeRoot function that was previously commented out with '// !!!'.

This fixes the issue where 'import gql from "graphql-tag"' fails to resolve
when the type declarations are only available in a custom typeRoots directory
(e.g. '../assets/node_modules/@types').

The fix also updates test baselines for three tests:
- moduleResolutionAsTypeReferenceDirective.ts: No longer errors, a2 is now typed as number
- moduleResolutionAsTypeReferenceDirectiveAmbient.ts: Trace updated
- moduleResolutionAsTypeReferenceDirectiveScoped.ts: Fewer errors, matching TS reference

Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/63d20d26-9900-496c-8ccd-72f81619aa4e

Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix VSCode extension issue with typeRoots Implement resolveFromTypeRoot fallback in module resolution Apr 1, 2026
Copilot AI requested a review from RyanCavanaugh April 1, 2026 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VSCode extension does not appear to work with typeRoots

2 participants