Skip to content

Commit 5df3a61

Browse files
Skip URI-like module names in typings-location resolution (#65340)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: dec07792-6b96-4d6d-b2c8-c8a49abd0ce6 Copilot-Session: b84e8c74-3143-4f8e-b830-fbea2c5ea6d1
1 parent 3434c52 commit 5df3a61

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

‎tsc/internal/module/resolver.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,11 @@ func (r *DefaultResolver) ResolvePackageDirectory(moduleName string, containingF
571571
func (r *DefaultResolver) tryResolveFromTypingsLocation(moduleName string, containingDirectory tspath.RootedDirectoryPath, originalResult *ResolvedModule, traceBuilder *tracer) *ResolvedModule {
572572
if r.typingsLocation == "" ||
573573
tspath.IsExternalModuleNameRelative(moduleName) ||
574+
// Like the primary node_modules lookup, skip names containing a colon, which look like
575+
// absolute URIs or scheme-prefixed specifiers (e.g. "https://esm.sh/pkg?dev", "node:fs",
576+
// "jsr:@std/path"). These never map to a package directory in the typings cache; "node:"
577+
// builtins are typed via ambient module declarations in @types/node instead.
578+
strings.Contains(moduleName, ":") ||
574579
(originalResult.ResolvedFileName != "" && tspath.ExtensionIsOneOf(originalResult.Extension, tspath.SupportedTSExtensionsWithJsonFlat)) {
575580
return originalResult
576581
}

‎tsc/internal/project/session_test.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,30 @@ func TestSession(t *testing.T) {
135135
assert.Assert(t, file != nil)
136136
assert.Equal(t, file.ScriptKind, core.ScriptKindTS)
137137
})
138+
139+
t.Run("inferred project with URL imports containing query or fragment", func(t *testing.T) {
140+
t.Parallel()
141+
content := `import "https://esm.sh/preact?dev";
142+
import "https://esm.sh/preact#main";`
143+
files := map[string]any{
144+
"/home/projects/TS/p1/index.ts": content,
145+
// The typings cache must exist for the extra typings-location resolution pass to run.
146+
projecttestutil.TestTypingsLocation + "/node_modules/@types/somelib/index.d.ts": `export const x: number;`,
147+
}
148+
session, _ := projecttestutil.Setup(files)
149+
150+
session.DidOpenFile(context.Background(), "file:///home/projects/TS/p1/index.ts", 1, content, lsproto.LanguageKindTypeScript)
151+
152+
ls, err := session.GetLanguageService(context.Background(), "file:///home/projects/TS/p1/index.ts")
153+
assert.NilError(t, err)
154+
program := ls.GetProgram()
155+
file := program.GetSourceFile("/home/projects/TS/p1/index.ts")
156+
assert.Assert(t, file != nil)
157+
assert.Equal(t, len(file.Imports()), 2)
158+
for _, imp := range file.Imports() {
159+
assert.Assert(t, !program.GetResolvedModuleFromModuleSpecifier(file, imp).IsResolved())
160+
}
161+
})
138162
})
139163

140164
t.Run("watchChange and didOpen in same batch rebuilds program", func(t *testing.T) {

0 commit comments

Comments
 (0)