Skip to content

Watch type refs and instead of watching directly use go routine #1217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
36 changes: 29 additions & 7 deletions internal/compiler/fileloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,16 @@ func processAllProgramFiles(
var unsupportedExtensions []string

loader.parseTasks.collect(&loader, loader.rootTasks, func(task *parseTask, _ []tspath.Path) {
file := task.file
if task.isRedirected {
return
}

if task.isForAutomaticTypeDirective {
typeResolutionsInFile[task.path] = task.typeResolutionsInFile
return
}
file := task.file
path := task.path
if file == nil {
missingFiles = append(missingFiles, task.normalizedFilePath)
return
Expand All @@ -126,7 +132,6 @@ func processAllProgramFiles(
} else {
files = append(files, file)
}
path := file.Path()

filesByPath[path] = file
resolvedModules[path] = task.resolutionsInFile
Expand Down Expand Up @@ -189,14 +194,31 @@ func (p *fileLoader) addAutomaticTypeDirectiveTasks() {
containingDirectory = p.opts.Host.GetCurrentDirectory()
}
containingFileName := tspath.CombinePaths(containingDirectory, module.InferredTypesContainingFile)
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: containingFileName, isLib: false, isForAutomaticTypeDirective: true})
}

automaticTypeDirectiveNames := module.GetAutomaticTypeDirectiveNames(compilerOptions, p.opts.Host)
for _, name := range automaticTypeDirectiveNames {
resolved := p.resolver.ResolveTypeReferenceDirective(name, containingFileName, core.ModuleKindNodeNext, nil)
if resolved.IsResolved() {
p.rootTasks = append(p.rootTasks, &parseTask{normalizedFilePath: resolved.ResolvedFileName, isLib: false})
func (p *fileLoader) resolveAutomaticTypeDirectives(containingFileName string) (
toParse []resolvedRef,
typeResolutionsInFile module.ModeAwareCache[*module.ResolvedTypeReferenceDirective],
) {
automaticTypeDirectiveNames := module.GetAutomaticTypeDirectiveNames(p.opts.Config.CompilerOptions(), p.opts.Host)
if len(automaticTypeDirectiveNames) != 0 {
toParse = make([]resolvedRef, 0, len(automaticTypeDirectiveNames))
typeResolutionsInFile = make(module.ModeAwareCache[*module.ResolvedTypeReferenceDirective], len(automaticTypeDirectiveNames))
for _, name := range automaticTypeDirectiveNames {
resolutionMode := core.ModuleKindNodeNext
resolved := p.resolver.ResolveTypeReferenceDirective(name, containingFileName, resolutionMode, nil)
typeResolutionsInFile[module.ModeAwareCacheKey{Name: name, Mode: resolutionMode}] = resolved
if resolved.IsResolved() {
toParse = append(toParse, resolvedRef{
fileName: resolved.ResolvedFileName,
increaseDepth: resolved.IsExternalLibraryImport,
elideOnDepth: false,
})
}
}
}
return toParse, typeResolutionsInFile
}

func (p *fileLoader) addProjectReferenceTasks() {
Expand Down
28 changes: 20 additions & 8 deletions internal/compiler/parsetask.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
)

type parseTask struct {
normalizedFilePath string
path tspath.Path
file *ast.SourceFile
isLib bool
isRedirected bool
subTasks []*parseTask
loaded bool
normalizedFilePath string
path tspath.Path
file *ast.SourceFile
isLib bool
isRedirected bool
subTasks []*parseTask
loaded bool
isForAutomaticTypeDirective bool

metadata ast.SourceFileMetaData
resolutionsInFile module.ModeAwareCache[*module.ResolvedModule]
Expand All @@ -36,8 +37,11 @@ func (t *parseTask) Path() tspath.Path {

func (t *parseTask) load(loader *fileLoader) {
t.loaded = true

t.path = loader.toPath(t.normalizedFilePath)
if t.isForAutomaticTypeDirective {
t.loadAutomaticTypeDirectives(loader)
return
}
redirect := loader.projectReferenceFileMapper.getParseFileRedirect(t)
if redirect != "" {
t.redirect(loader, redirect)
Expand Down Expand Up @@ -97,6 +101,14 @@ func (t *parseTask) redirect(loader *fileLoader, fileName string) {
t.subTasks = []*parseTask{{normalizedFilePath: tspath.NormalizePath(fileName), isLib: t.isLib}}
}

func (t *parseTask) loadAutomaticTypeDirectives(loader *fileLoader) {
toParseTypeRefs, typeResolutionsInFile := loader.resolveAutomaticTypeDirectives(t.normalizedFilePath)
t.typeResolutionsInFile = typeResolutionsInFile
for _, typeResolution := range toParseTypeRefs {
t.addSubTask(typeResolution, false)
}
}

type resolvedRef struct {
fileName string
increaseDepth bool
Expand Down
4 changes: 4 additions & 0 deletions internal/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,10 @@ func (p *Program) GetResolvedTypeReferenceDirectiveFromTypeReferenceDirective(ty
return nil
}

func (p *Program) GetResolvedTypeReferenceDirectives() map[tspath.Path]module.ModeAwareCache[*module.ResolvedTypeReferenceDirective] {
return p.typeResolutionsInFile
}

func (p *Program) getModeForTypeReferenceDirectiveInFile(ref *ast.FileReference, sourceFile *ast.SourceFile) core.ResolutionMode {
if ref.ResolutionMode != core.ResolutionModeNone {
return ref.ResolutionMode
Expand Down
8 changes: 8 additions & 0 deletions internal/module/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ func (r *ResolvedModule) IsResolved() bool {
return r != nil && r.ResolvedFileName != ""
}

func (r *ResolvedModule) GetLookupLocations() *LookupLocations {
return &r.LookupLocations
}

type ResolvedTypeReferenceDirective struct {
LookupLocations
Primary bool
Expand All @@ -94,6 +98,10 @@ func (r *ResolvedTypeReferenceDirective) IsResolved() bool {
return r.ResolvedFileName != ""
}

func (r *ResolvedTypeReferenceDirective) GetLookupLocations() *LookupLocations {
return &r.LookupLocations
}

type extensions int32

const (
Expand Down
2 changes: 1 addition & 1 deletion internal/project/ata.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (ti *TypingsInstaller) discoverAndInstallTypings(p *Project, typingsInfo *T
)

// start watching files
p.WatchTypingLocations(filesToWatch)
go p.WatchTypingLocations(filesToWatch)

requestId := ti.installRunCount.Add(1)
// install typings
Expand Down
69 changes: 42 additions & 27 deletions internal/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ type Project struct {
typingFiles []string

// Watchers
watchMu sync.RWMutex
failedLookupsWatch *watchedFiles[map[tspath.Path]string]
affectingLocationsWatch *watchedFiles[map[tspath.Path]string]
typingsFilesWatch *watchedFiles[map[tspath.Path]string]
Expand Down Expand Up @@ -347,37 +348,48 @@ func (p *Project) GetLanguageServiceForRequest(ctx context.Context) (*ls.Languag
return languageService, cleanup
}

func (p *Project) getModuleResolutionWatchGlobs() (failedLookups map[tspath.Path]string, affectingLocaions map[tspath.Path]string) {
failedLookups = make(map[tspath.Path]string)
affectingLocaions = make(map[tspath.Path]string)
for _, resolvedModulesInFile := range p.program.GetResolvedModules() {
for _, resolvedModule := range resolvedModulesInFile {
for _, failedLookupLocation := range resolvedModule.FailedLookupLocations {
path := p.toPath(failedLookupLocation)
func (p *Project) updatedResolutionWatchers(ctx context.Context, program *compiler.Program) {
client := p.Client()
if !p.host.IsWatchEnabled() || client == nil {
return
}

failedLookups := make(map[tspath.Path]string)
affectingLocations := make(map[tspath.Path]string)
extractLookups(program, failedLookups, affectingLocations, program.GetResolvedModules())
extractLookups(program, failedLookups, affectingLocations, program.GetResolvedTypeReferenceDirectives())
p.watchMu.Lock()
defer p.watchMu.Unlock()
p.failedLookupsWatch.update(ctx, failedLookups)
p.affectingLocationsWatch.update(ctx, affectingLocations)
}

type ResolutionWithLookupLocations interface {
GetLookupLocations() *module.LookupLocations
}

func extractLookups[T ResolutionWithLookupLocations](
program *compiler.Program,
failedLookups map[tspath.Path]string,
affectingLocations map[tspath.Path]string,
allResolutions map[tspath.Path]module.ModeAwareCache[T],
) {
for _, resolutionsInFile := range allResolutions {
for _, resolution := range resolutionsInFile {
for _, failedLookupLocation := range resolution.GetLookupLocations().FailedLookupLocations {
path := tspath.ToPath(failedLookupLocation, program.GetCurrentDirectory(), program.UseCaseSensitiveFileNames())
if _, ok := failedLookups[path]; !ok {
failedLookups[path] = failedLookupLocation
}
}
for _, affectingLocation := range resolvedModule.AffectingLocations {
path := p.toPath(affectingLocation)
if _, ok := affectingLocaions[path]; !ok {
affectingLocaions[path] = affectingLocation
for _, affectingLocation := range resolution.GetLookupLocations().AffectingLocations {
path := tspath.ToPath(affectingLocation, program.GetCurrentDirectory(), program.UseCaseSensitiveFileNames())
if _, ok := affectingLocations[path]; !ok {
affectingLocations[path] = affectingLocation
}
}
}
}
return failedLookups, affectingLocaions
}

func (p *Project) updateWatchers(ctx context.Context) {
client := p.Client()
if !p.host.IsWatchEnabled() || client == nil {
return
}

failedLookupGlobs, affectingLocationGlobs := p.getModuleResolutionWatchGlobs()
p.failedLookupsWatch.update(ctx, failedLookupGlobs)
p.affectingLocationsWatch.update(ctx, affectingLocationGlobs)
}

// onWatchEventForNilScriptInfo is fired for watch events that are not the
Expand All @@ -389,6 +401,8 @@ func (p *Project) updateWatchers(ctx context.Context) {
// part of the project, e.g., a .js file in a project without --allowJs.
func (p *Project) onWatchEventForNilScriptInfo(fileName string) {
path := p.toPath(fileName)
p.watchMu.RLock()
defer p.watchMu.RUnlock()
if _, ok := p.failedLookupsWatch.data[path]; ok {
p.markAsDirty()
} else if _, ok := p.affectingLocationsWatch.data[path]; ok {
Expand Down Expand Up @@ -528,9 +542,7 @@ func (p *Project) updateGraph() (*compiler.Program, bool) {
})
}
p.enqueueInstallTypingsForProject(oldProgram, hasAddedOrRemovedFiles)
// TODO: this is currently always synchronously called by some kind of updating request,
// but in Strada we throttle, so at least sometimes this should be considered top-level?
p.updateWatchers(context.TODO())
go p.updatedResolutionWatchers(context.TODO(), p.program)
}
p.Logf("Finishing updateGraph: Project: %s version: %d in %s", p.name, p.version, time.Since(start))
return p.program, true
Expand Down Expand Up @@ -768,10 +780,11 @@ func (p *Project) UpdateTypingFiles(typingsInfo *TypingsInfo, typingFiles []stri

func (p *Project) WatchTypingLocations(files []string) {
p.mu.Lock()
defer p.mu.Unlock()
if p.isClosed() {
p.mu.Unlock()
return
}
p.mu.Unlock()

client := p.Client()
if !p.host.IsWatchEnabled() || client == nil {
Expand Down Expand Up @@ -817,6 +830,8 @@ func (p *Project) WatchTypingLocations(files []string) {
}
}
ctx := context.Background()
p.watchMu.Lock()
defer p.watchMu.Unlock()
p.typingsFilesWatch.update(ctx, typingsInstallerFileGlobs)
p.typingsDirectoryWatch.update(ctx, typingsInstallerDirectoryGlobs)
}
Expand Down