Skip to content
Draft
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
4 changes: 4 additions & 0 deletions tsc/internal/module/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,10 @@ func (r *resolutionState) loadModuleFromExportsOrImports(
scope *packagejson.InfoCacheEntry,
isImports bool,
) *resolved {
if lookupTable == nil {
return continueSearching()
}

if !strings.HasSuffix(moduleName, "/") && !strings.Contains(moduleName, "*") {
if target, ok := lookupTable.Get(moduleName); ok {
return r.loadModuleFromTargetExportOrImport(extensions, moduleName, scope, isImports, target, "", false /*isPattern*/, moduleName)
Expand Down
44 changes: 44 additions & 0 deletions tsc/internal/module/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import (
"sync/atomic"
"testing"

"github.com/microsoft/TypeScript/tsc/internal/collections"
"github.com/microsoft/TypeScript/tsc/internal/core"
"github.com/microsoft/TypeScript/tsc/internal/module"
"github.com/microsoft/TypeScript/tsc/internal/packagejson"
"github.com/microsoft/TypeScript/tsc/internal/vfs"
"github.com/microsoft/TypeScript/tsc/internal/vfs/vfstest"
)
Expand Down Expand Up @@ -49,6 +51,48 @@ func TestResolveModuleNameTrailingSlash(t *testing.T) {
}
}

func TestResolvePackageImportWithNilLookupTable(t *testing.T) {
t.Parallel()

fs := vfstest.FromMap(map[string]string{
"/repo/package.json": `{"imports": {}}`,
"/repo/src/file.ts": "",
}, true)
fields, err := packagejson.Parse([]byte(`{"imports": {}}`))
if err != nil {
t.Fatal(err)
}
var imports *collections.OrderedMap[string, packagejson.ExportsOrImports]
fields.Imports.Value = imports
cache := packagejson.NewInfoCache("/repo", true)
cache.Set("/repo/package.json", &packagejson.InfoCacheEntry{
PackageDirectory: "/repo",
DirectoryExists: true,
Contents: &packagejson.PackageJson{
Fields: fields,
Parseable: true,
},
})
opts := &core.CompilerOptions{
ModuleResolution: core.ModuleResolutionKindNodeNext,
Module: core.ModuleKindNodeNext,
Target: core.ScriptTargetESNext,
}
resolver := module.NewResolver(module.ResolverOptions{
Host: &resolutionHostStub{fs: fs, cwd: "/repo"},
CompilerOptions: opts,
PackageJsonCache: cache,
})

resolved, _, err := resolver.ResolveModuleName("#missing", "/repo/src/file.ts", core.ModuleKindESNext, nil)
if err != nil {
t.Fatal(err)
}
if resolved.IsResolved() {
t.Fatalf("expected #missing to be unresolved, got %q", resolved.ResolvedFileName)
}
}

// blockingFS wraps a vfs.FS and forces FileExists calls for `targetPath` to
// block on `gate` until released. Each caller sends on `arrived` when it
// reaches the gate. This is used to deterministically reproduce the
Expand Down