Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14822,7 +14822,18 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
if ast.FindAncestor(location, ast.IsEmittableImport) != nil {
tsExtension := tspath.TryExtractTSExtension(moduleReference)
if tsExtension == "" {
panic("should be able to extract TS extension from string that passes IsDeclarationFileName")
// Fallback: do a best-effort extraction using strings.Contains.
// This handles cases where a wildcard pattern matches a TS extension that's
// not at the end of the module specifier, e.g., "#/foo.ts.omg" through "#/*.omg": "./src/*"
for _, ext := range tspath.SupportedTSExtensionsFlat {
if strings.Contains(moduleReference, ext) {
tsExtension = ext
break
Comment on lines +14825 to +14831
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

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

The fallback TS extension extraction uses strings.Contains(moduleReference, ext) while iterating tspath.SupportedTSExtensionsFlat (which lists .ts before .tsx and .d.ts). This can pick the wrong extension (e.g. "foo.tsx.omg" matches .ts first, "foo.d.ts.omg" matches .ts instead of .d.ts), producing incorrect TS5097 diagnostics. Prefer a boundary-aware match (e.g. look for an extension that is followed by /, ., ?, #, or end-of-string) and/or prefer the longest matching extension.

Copilot uses AI. Check for mistakes.
}
}
}
if tsExtension == "" {
panic("should be able to extract TS extension from string when resolvedUsingTsExtension is true")
}
c.error(
errorNode,
Expand Down
9 changes: 8 additions & 1 deletion internal/module/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1601,10 +1601,17 @@ func (r *resolutionState) loadFileNameFromPackageJSONField(extensions extensions
if extensions&extensionsTypeScript != 0 && tspath.HasImplementationTSFileExtension(candidate) || extensions&extensionsDeclaration != 0 && tspath.IsDeclarationFileName(candidate) {
if path, ok := r.tryFile(candidate, onlyRecordFailures); ok {
extension := tspath.TryExtractTSExtension(path)
// resolvedUsingTsExtension should be true when the pattern ends with * and the
// candidate file ends in a TS extension. This means the * matched a TS extension
// from the module specifier. For example:
// - import "pkg/foo.ts" with pattern "./*" -> true
// - import "pkg/foo.ts.omg" with pattern "./*.omg" -> true (star matched .ts)
// - import "pkg/foo" with pattern "./*.ts" -> false (extension in pattern, not specifier)
resolvedUsingTsExtension := strings.HasSuffix(packageJSONValue, "*") && extension != ""
return &resolved{
path: path,
extension: extension,
resolvedUsingTsExtension: packageJSONValue != "" && !strings.HasSuffix(packageJSONValue, extension),
resolvedUsingTsExtension: resolvedUsingTsExtension,
}
}
return continueSearching()
Expand Down
32 changes: 32 additions & 0 deletions testdata/tests/cases/compiler/packageJsonImportsWildcardNoCrash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// @noEmit: true
// @noTypesAndSymbols: true
// @module: nodenext
// @moduleResolution: nodenext
// @filename: src/a.ts
Comment on lines +1 to +5
Copy link

Copilot AI Feb 9, 2026

Choose a reason for hiding this comment

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

A new compiler test was added (packageJsonImportsWildcardNoCrash.ts), but there is no corresponding reference baseline under testdata/baselines/reference/compiler/ for it. The compiler test runner typically expects an .errors.txt baseline even when there are 0 errors, so this will likely fail CI. Please add the generated baseline for this test (at minimum packageJsonImportsWildcardNoCrash.errors.txt).

Copilot uses AI. Check for mistakes.
import * as b from "#/b.";

b.foo();

// @filename: src/b.ts
export function foo() {}

// @filename: package.json
{
"imports": {
"#/*": {
"types": "./src/*ts",
"default": "./dist/*js"
}
}
}

// @filename: tsconfig.json
{
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext",
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// @noEmit: true
// @noTypesAndSymbols: true
// @module: nodenext
// @moduleResolution: nodenext
// Test verifies that when a module specifier contains ".ts" that gets matched by a
// wildcard pattern, resolvedUsingTsExtension is correctly set to true.
// Example: import "#/foo.ts.omg" with pattern "#/*.omg": "./src/*"
// The * matches "foo.ts", and when expanded becomes "./src/foo.ts"
// Since the wildcard matched ".ts" from the specifier, an error should be reported.

// @filename: src/foo.ts
export function hello() {
return "world";
}

// @filename: src/index.ts
import { hello } from "#/foo.ts.omg";

hello();

// @filename: package.json
{
"type": "module",
"imports": {
"#/*.omg": "./src/*"
}
}

// @filename: tsconfig.json
{
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext",
"rootDir": "src",
"outDir": "dist"
},
"include": ["src"]
}