Skip to content

Commit 76d6872

Browse files
authored
Port finding default project finder code (#1186)
1 parent 8708d82 commit 76d6872

11 files changed

+861
-188
lines changed

internal/compiler/fileloader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func (p *fileLoader) addProjectReferenceTasks() {
200200
opts: p.opts,
201201
host: p.opts.Host,
202202
}
203-
projectReferences := p.opts.Config.ProjectReferences()
203+
projectReferences := p.opts.Config.ResolvedProjectReferencePaths()
204204
if len(projectReferences) == 0 {
205205
return
206206
}

internal/compiler/program.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (p *Program) GetRedirectForResolution(file ast.HasFileName) *tsoptions.Pars
124124
}
125125

126126
func (p *Program) ForEachResolvedProjectReference(
127-
fn func(path tspath.Path, config *tsoptions.ParsedCommandLine) bool,
127+
fn func(path tspath.Path, config *tsoptions.ParsedCommandLine),
128128
) {
129129
p.projectReferenceFileMapper.forEachResolvedProjectReference(fn)
130130
}

internal/compiler/projectreferencefilemapper.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (mapper *projectReferenceFileMapper) getResolvedReferenceFor(path tspath.Pa
140140
}
141141

142142
func (mapper *projectReferenceFileMapper) forEachResolvedProjectReference(
143-
fn func(path tspath.Path, config *tsoptions.ParsedCommandLine) bool,
143+
fn func(path tspath.Path, config *tsoptions.ParsedCommandLine),
144144
) {
145145
if mapper.opts.Config.ConfigFile == nil {
146146
return
@@ -150,14 +150,13 @@ func (mapper *projectReferenceFileMapper) forEachResolvedProjectReference(
150150
}
151151

152152
func (mapper *projectReferenceFileMapper) forEachResolvedReferenceWorker(
153-
referenes []tspath.Path,
154-
fn func(path tspath.Path, config *tsoptions.ParsedCommandLine) bool,
153+
references []tspath.Path,
154+
fn func(path tspath.Path, config *tsoptions.ParsedCommandLine),
155155
) {
156-
for _, path := range referenes {
156+
for _, path := range references {
157157
config, _ := mapper.configToProjectReference[path]
158-
if !fn(path, config) {
159-
return
160-
}
158+
fn(path, config)
159+
mapper.forEachResolvedReferenceWorker(mapper.referencesInConfigFile[path], fn)
161160
}
162161
}
163162

internal/compiler/projectreferenceparsetask.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (t *projectReferenceParseTask) start(loader *fileLoader) {
2525
t.resolved.ParseInputOutputNames()
2626
})
2727
}
28-
subReferences := t.resolved.ProjectReferences()
28+
subReferences := t.resolved.ResolvedProjectReferencePaths()
2929
if len(subReferences) == 0 {
3030
return
3131
}
@@ -36,13 +36,10 @@ func getSubTasksOfProjectReferenceParseTask(t *projectReferenceParseTask) []*pro
3636
return t.subTasks
3737
}
3838

39-
func createProjectReferenceParseTasks(projectReferences []*core.ProjectReference) []*projectReferenceParseTask {
40-
tasks := make([]*projectReferenceParseTask, 0, len(projectReferences))
41-
for _, reference := range projectReferences {
42-
configName := core.ResolveProjectReferencePath(reference)
43-
tasks = append(tasks, &projectReferenceParseTask{
39+
func createProjectReferenceParseTasks(projectReferences []string) []*projectReferenceParseTask {
40+
return core.Map(projectReferences, func(configName string) *projectReferenceParseTask {
41+
return &projectReferenceParseTask{
4442
configName: configName,
45-
})
46-
}
47-
return tasks
43+
}
44+
})
4845
}

internal/project/configfileregistry.go

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type ConfigFileEntry struct {
1717
mu sync.Mutex
1818
commandLine *tsoptions.ParsedCommandLine
1919
projects collections.Set[*Project]
20+
infos collections.Set[*ScriptInfo]
2021
pendingReload PendingReload
2122
rootFilesWatch *watchedFiles[[]string]
2223
}
@@ -28,6 +29,7 @@ type ExtendedConfigFileEntry struct {
2829

2930
type ConfigFileRegistry struct {
3031
Host ProjectHost
32+
defaultProjectFinder *defaultProjectFinder
3133
ConfigFiles collections.SyncMap[tspath.Path, *ConfigFileEntry]
3234
ExtendedConfigCache collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry]
3335
ExtendedConfigsUsedBy collections.SyncMap[tspath.Path, *ExtendedConfigFileEntry]
@@ -60,26 +62,17 @@ func (c *configFileWatchHost) Log(message string) {
6062
c.host.Log(message)
6163
}
6264

63-
func (c *ConfigFileRegistry) ReleaseConfig(path tspath.Path, project *Project) {
65+
func (c *ConfigFileRegistry) releaseConfig(path tspath.Path, project *Project) {
6466
entry, ok := c.ConfigFiles.Load(path)
6567
if !ok {
6668
return
6769
}
6870
entry.mu.Lock()
6971
defer entry.mu.Unlock()
7072
entry.projects.Delete(project)
71-
if entry.projects.Len() == 0 {
72-
c.ConfigFiles.Delete(path)
73-
commandLine := entry.commandLine
74-
entry.commandLine = nil
75-
c.updateExtendedConfigsUsedBy(path, entry, commandLine)
76-
if entry.rootFilesWatch != nil {
77-
entry.rootFilesWatch.update(context.Background(), nil)
78-
}
79-
}
8073
}
8174

82-
func (c *ConfigFileRegistry) AcquireConfig(fileName string, path tspath.Path, project *Project) *tsoptions.ParsedCommandLine {
75+
func (c *ConfigFileRegistry) acquireConfig(fileName string, path tspath.Path, project *Project, info *ScriptInfo) *tsoptions.ParsedCommandLine {
8376
entry, ok := c.ConfigFiles.Load(path)
8477
if !ok {
8578
// Create parsed command line
@@ -97,7 +90,11 @@ func (c *ConfigFileRegistry) AcquireConfig(fileName string, path tspath.Path, pr
9790
}
9891
entry.mu.Lock()
9992
defer entry.mu.Unlock()
100-
entry.projects.Add(project)
93+
if project != nil {
94+
entry.projects.Add(project)
95+
} else if info != nil {
96+
entry.infos.Add(info)
97+
}
10198
if entry.pendingReload == PendingReloadNone {
10299
return entry.commandLine
103100
}
@@ -114,6 +111,23 @@ func (c *ConfigFileRegistry) AcquireConfig(fileName string, path tspath.Path, pr
114111
return entry.commandLine
115112
}
116113

114+
func (c *ConfigFileRegistry) getConfig(path tspath.Path) *tsoptions.ParsedCommandLine {
115+
entry, ok := c.ConfigFiles.Load(path)
116+
if ok {
117+
return entry.commandLine
118+
}
119+
return nil
120+
}
121+
122+
func (c *ConfigFileRegistry) releaseConfigsForInfo(info *ScriptInfo) {
123+
c.ConfigFiles.Range(func(path tspath.Path, entry *ConfigFileEntry) bool {
124+
entry.mu.Lock()
125+
entry.infos.Delete(info)
126+
entry.mu.Unlock()
127+
return true
128+
})
129+
}
130+
117131
func (c *ConfigFileRegistry) updateRootFilesWatch(fileName string, entry *ConfigFileEntry) {
118132
if entry.rootFilesWatch == nil {
119133
return
@@ -187,6 +201,10 @@ func (c *ConfigFileRegistry) onConfigChange(path tspath.Path, changeKind lsproto
187201
entry.mu.Lock()
188202
defer entry.mu.Unlock()
189203
if entry.SetPendingReload(PendingReloadFull) {
204+
for info := range entry.infos.Keys() {
205+
delete(c.defaultProjectFinder.configFileForOpenFiles, info.Path())
206+
delete(c.defaultProjectFinder.configFilesAncestorForOpenFiles, info.Path())
207+
}
190208
for project := range entry.projects.Keys() {
191209
if project.configFilePath == path {
192210
switch changeKind {
@@ -202,6 +220,7 @@ func (c *ConfigFileRegistry) onConfigChange(path tspath.Path, changeKind lsproto
202220
project.markAsDirty()
203221
}
204222
}
223+
return true
205224
}
206225
return false
207226
}
@@ -224,3 +243,19 @@ func (c *ConfigFileRegistry) tryInvokeWildCardDirectories(fileName string, path
224243
entry.mu.Unlock()
225244
}
226245
}
246+
247+
func (c *ConfigFileRegistry) cleanup(toRemoveConfigs map[tspath.Path]*ConfigFileEntry) {
248+
for path, entry := range toRemoveConfigs {
249+
entry.mu.Lock()
250+
if entry.projects.Len() == 0 && entry.infos.Len() == 0 {
251+
c.ConfigFiles.Delete(path)
252+
commandLine := entry.commandLine
253+
entry.commandLine = nil
254+
c.updateExtendedConfigsUsedBy(path, entry, commandLine)
255+
if entry.rootFilesWatch != nil {
256+
entry.rootFilesWatch.update(context.Background(), nil)
257+
}
258+
}
259+
entry.mu.Unlock()
260+
}
261+
}

0 commit comments

Comments
 (0)