Skip to content
Open
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
25 changes: 5 additions & 20 deletions tsc/internal/api/module_resolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/microsoft/TypeScript/tsc/internal/json"
"github.com/microsoft/TypeScript/tsc/internal/locale"
"github.com/microsoft/TypeScript/tsc/internal/module"
"github.com/microsoft/TypeScript/tsc/internal/packagejson"
"github.com/microsoft/TypeScript/tsc/internal/project"
"github.com/microsoft/TypeScript/tsc/internal/tspath"
)
Expand All @@ -20,7 +19,6 @@ type moduleResolverFactory struct {
registration *moduleResolverRegistration
session *Session
conn ipc.Conn
ctx context.Context
currentDirectory string
}

Expand All @@ -41,6 +39,7 @@ type callbackModuleResolver struct {
}

func (f *moduleResolverFactory) NewResolver(
ctx context.Context,
options module.ResolverOptions,
) (module.Resolver, func()) {
options.CompilerOptions = f.registration.compilerOptions
Expand All @@ -55,7 +54,7 @@ func (f *moduleResolverFactory) NewResolver(
var resolver module.Resolver = &callbackModuleResolver{
registration: f.registration,
conn: f.conn,
ctx: f.ctx,
ctx: ctx,
currentDirectory: f.currentDirectory,
programResolutionContextID: contextID,
fallbackResolver: fallback,
Expand Down Expand Up @@ -128,21 +127,8 @@ func (p *callbackModuleResolver) ResolveTypeReferenceDirective(
return p.fallbackResolver.ResolveTypeReferenceDirective(typeReferenceDirectiveName, containingFile, resolutionMode, redirectedReference)
}

func (p *callbackModuleResolver) GetPackageScopeForPath(directory string) *packagejson.InfoCacheEntry {
return p.fallbackResolver.GetPackageScopeForPath(directory)
}

func (p *callbackModuleResolver) PackageJsonCacheEntries(f func(key tspath.Path, value *packagejson.InfoCacheEntry) bool) {
p.fallbackResolver.PackageJsonCacheEntries(f)
}

func (p *callbackModuleResolver) ResolvePackageDirectory(
moduleName string,
containingFile string,
resolutionMode core.ResolutionMode,
redirectedReference module.ResolvedProjectReference,
) *module.ResolvedModule {
return p.fallbackResolver.ResolvePackageDirectory(moduleName, containingFile, resolutionMode, redirectedReference)
func (p *callbackModuleResolver) GetResolutionData() *module.ResolutionData {
return p.fallbackResolver.GetResolutionData()
}

func compileModuleResolutionSpec(spec *ModuleResolutionSpec, currentDirectory string, useCaseSensitive bool) (*module.StaticResolutions, error) {
Expand Down Expand Up @@ -226,7 +212,7 @@ func moduleResolutionTraceToStrings(trace []module.DiagAndArgs) []string {
})
}

func (s *Session) moduleResolverFactory(ctx context.Context, options *CreateProgramOptions) (project.ModuleResolverFactory, error) {
func (s *Session) moduleResolverFactory(options *CreateProgramOptions) (project.ModuleResolverFactory, error) {
if options.ModuleResolver == 0 {
return nil, nil
}
Expand All @@ -243,7 +229,6 @@ func (s *Session) moduleResolverFactory(ctx context.Context, options *CreateProg
registration: data,
session: s,
conn: s.conn,
ctx: ctx,
currentDirectory: s.GetCurrentDirectory(),
}, nil
}
Expand Down
4 changes: 2 additions & 2 deletions tsc/internal/api/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ func (s *Session) toAPISnapshotRequest(ctx context.Context, changes *SnapshotReq
if programParams.Options != nil {
request.ProjectReferences = programParams.Options.ProjectReferences
request.ConfigFileParsingDiagnostics = core.Map(programParams.Options.ConfigFileParsingDiagnostics, func(d *DiagnosticResponse) *ast.Diagnostic { return d.ToDiagnostic() })
factory, err := s.moduleResolverFactory(ctx, programParams.Options)
factory, err := s.moduleResolverFactory(programParams.Options)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1405,7 +1405,7 @@ func (s *Session) toAPISnapshotRequest(ctx context.Context, changes *SnapshotReq
if programParams.Options != nil {
request.ProjectReferences = programParams.Options.ProjectReferences
request.ConfigFileParsingDiagnostics = core.Map(programParams.Options.ConfigFileParsingDiagnostics, func(d *DiagnosticResponse) *ast.Diagnostic { return d.ToDiagnostic() })
factory, err := s.moduleResolverFactory(ctx, programParams.Options)
factory, err := s.moduleResolverFactory(programParams.Options)
if err != nil {
return nil, err
}
Expand Down
39 changes: 35 additions & 4 deletions tsc/internal/api/session_module_resolution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@ import (
)

type failingModuleResolutionConn struct {
calls int
calls int
contexts []context.Context
}

func (c *failingModuleResolutionConn) Run(context.Context) error {
return nil
}

func (c *failingModuleResolutionConn) Call(context.Context, string, any) (json.Value, error) {
func (c *failingModuleResolutionConn) Call(ctx context.Context, _ string, _ any) (json.Value, error) {
c.calls++
c.contexts = append(c.contexts, ctx)
return nil, errors.New("callback error")
}

Expand Down Expand Up @@ -238,10 +240,9 @@ func TestModuleResolutionCallbackErrorsAreReturned(t *testing.T) {
registration: registration,
session: session,
conn: conn,
ctx: context.Background(),
currentDirectory: "/",
}
provider, cleanup := factory.NewResolver(module.ResolverOptions{
provider, cleanup := factory.NewResolver(context.Background(), module.ResolverOptions{
Host: session,
CompilerOptions: core.EmptyCompilerOptions,
})
Expand All @@ -255,6 +256,35 @@ func TestModuleResolutionCallbackErrorsAreReturned(t *testing.T) {
assert.Equal(t, len(session.programResolutionContexts), 0)
}

func TestModuleResolutionFactoryUsesCurrentContext(t *testing.T) {
t.Parallel()
projectSession, _ := projecttestutil.Setup(map[string]any{})
defer projectSession.Close()
session := NewLSPSession(projectSession, nil)
defer session.Close()
conn := &failingModuleResolutionConn{}
factory := &moduleResolverFactory{
registration: &moduleResolverRegistration{id: 1, resolveModuleNameCallback: "resolveModuleName/1"},
session: session,
conn: conn,
currentDirectory: "/",
}
oldContext, cancel := context.WithCancel(context.Background())
defer cancel()
for _, ctx := range []context.Context{oldContext, t.Context()} {
resolver, cleanup := factory.NewResolver(ctx, module.ResolverOptions{
Host: session,
CompilerOptions: core.EmptyCompilerOptions,
})
_, _, err := resolver.ResolveModuleNameFromDirectory("pkg", "/src", core.ResolutionModeESM)
assert.ErrorContains(t, err, "callback error")
assert.Equal(t, conn.contexts[len(conn.contexts)-1], ctx)
cleanup()
cancel()
}
assert.Equal(t, len(session.programResolutionContexts), 0)
}

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

Expand Down Expand Up @@ -290,6 +320,7 @@ func TestModuleResolutionCallbackErrorRejectsLanguageServerUpdate(t *testing.T)
}},
})
assert.ErrorContains(t, err, "callback error")
assert.Equal(t, len(session.programResolutionContexts), 0)
assert.Assert(t, projectSession.Snapshot() == baseSnapshot)
assert.Equal(t, len(projectSession.Snapshot().ProjectCollection.SyntheticProjects()), 0)
}
Expand Down
5 changes: 0 additions & 5 deletions tsc/internal/compiler/emitHost.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,3 @@ func (host *emitHost) IsSourceFileFromExternalLibrary(file *ast.SourceFile) bool
func (host *emitHost) GetSymlinkCache() *symlinks.KnownSymlinks {
return host.program.GetSymlinkCache()
}

func (host *emitHost) ResolveModuleName(moduleName string, containingFile string, resolutionMode core.ResolutionMode) *module.ResolvedModule {
resolved, _, _ := host.program.resolver.ResolveModuleName(moduleName, containingFile, resolutionMode, nil)
return resolved
}
51 changes: 22 additions & 29 deletions tsc/internal/compiler/fileInclude.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package compiler

import (
"fmt"
"sync"

"github.com/microsoft/TypeScript/tsc/internal/ast"
"github.com/microsoft/TypeScript/tsc/internal/diagnostics"
Expand All @@ -28,16 +27,12 @@ const (
)

type FileIncludeReason struct {
kind fileIncludeKind
data any

// Uses relative file name
relativeFileNameDiag *ast.Diagnostic
relativeFileNameDiagOnce sync.Once

// Uses file name as is
diag *ast.Diagnostic
diagOnce sync.Once
kind fileIncludeKind
index int
isDefaultLib bool
referencedFile *referencedFileData
automaticTypeDirective *automaticTypeDirectiveFileData
canonicalSourceFile tspath.Path
}

type referencedFileData struct {
Expand Down Expand Up @@ -80,24 +75,23 @@ type automaticTypeDirectiveFileData struct {
}

func (r *FileIncludeReason) asIndex() int {
return r.data.(int)
return r.index
}

func (r *FileIncludeReason) asLibFileIndex() (int, bool) {
index, ok := r.data.(int)
return index, ok
return r.index, !r.isDefaultLib
}

func (r *FileIncludeReason) isReferencedFile() bool {
return r != nil && r.kind <= fileIncludeKindLibReferenceDirective
}

func (r *FileIncludeReason) asReferencedFileData() *referencedFileData {
return r.data.(*referencedFileData)
return r.referencedFile
}

func (r *FileIncludeReason) asAutomaticTypeDirectiveFileData() *automaticTypeDirectiveFileData {
return r.data.(*automaticTypeDirectiveFileData)
return r.automaticTypeDirective
}

func (r *FileIncludeReason) getReferencedLocation(program *Program) *referenceFileLocation {
Expand Down Expand Up @@ -152,19 +146,18 @@ func (r *FileIncludeReason) getReferencedLocation(program *Program) *referenceFi
}

func (r *FileIncludeReason) toDiagnostic(program *Program, relativeFileName bool) *ast.Diagnostic {
if relativeFileName {
r.relativeFileNameDiagOnce.Do(func() {
r.relativeFileNameDiag = r.computeDiagnostic(program, func(fileName string) string {
return tspath.GetRelativePathFromDirectory(program.GetCurrentDirectory(), fileName, program.comparePathsOptions)
})
})
return r.relativeFileNameDiag
} else {
r.diagOnce.Do(func() {
r.diag = r.computeDiagnostic(program, func(fileName string) string { return fileName })
})
return r.diag
key := includeReasonDiagnosticKey{reason: r, relativeFileName: relativeFileName}
if diagnostic, ok := program.includeProcessor.reasonDiagnostics.Load(key); ok {
return diagnostic
}
diagnostic := r.computeDiagnostic(program, func(fileName string) string {
if relativeFileName {
return tspath.GetRelativePathFromDirectory(program.GetCurrentDirectory(), fileName, program.comparePathsOptions)
}
return fileName
})
diagnostic, _ = program.includeProcessor.reasonDiagnostics.LoadOrStore(key, diagnostic)
return diagnostic
}

func (r *FileIncludeReason) computeDiagnostic(program *Program, toFileName func(string) string) *ast.Diagnostic {
Expand Down Expand Up @@ -214,7 +207,7 @@ func (r *FileIncludeReason) computeDiagnostic(program *Program, toFileName func(
return ast.NewCompilerDiagnostic(diagnostics.Default_library)
}
case fileIncludeKindContentMapperSupplemental:
canonical := program.GetSourceFileByPath(r.data.(tspath.Path))
canonical := program.GetSourceFileByPath(r.canonicalSourceFile)
return ast.NewCompilerDiagnostic(diagnostics.Supplemental_virtual_file_produced_by_the_content_mapper_for_file_0, toFileName(canonical.FileName()))
default:
panic(fmt.Sprintf("unknown reason: %v", r.kind))
Expand Down
Loading
Loading