Skip to content

Commit 70ca6da

Browse files
committed
Fix the config lifetime when getting project for non open file
1 parent fee7f97 commit 70ca6da

File tree

34 files changed

+29
-317
lines changed

34 files changed

+29
-317
lines changed

internal/project/configfileregistrybuilder.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ func (c *configFileRegistryBuilder) Finalize() *ConfigFileRegistry {
7676
return newRegistry
7777
}
7878

79-
func (c *configFileRegistryBuilder) findOrAcquireConfigForOpenFile(
79+
func (c *configFileRegistryBuilder) findOrAcquireConfigForFile(
8080
configFileName string,
8181
configFilePath tspath.Path,
82-
openFilePath tspath.Path,
82+
filePath tspath.Path,
8383
loadKind projectLoadKind,
8484
logger *logging.LogTree,
8585
) *tsoptions.ParsedCommandLine {
@@ -90,7 +90,7 @@ func (c *configFileRegistryBuilder) findOrAcquireConfigForOpenFile(
9090
}
9191
return nil
9292
case projectLoadKindCreate:
93-
return c.acquireConfigForOpenFile(configFileName, configFilePath, openFilePath, logger)
93+
return c.acquireConfigForFile(configFileName, configFilePath, filePath, logger)
9494
default:
9595
panic(fmt.Sprintf("unknown project load kind: %d", loadKind))
9696
}
@@ -248,25 +248,27 @@ func (c *configFileRegistryBuilder) acquireConfigForProject(fileName string, pat
248248
return entry.Value().commandLine
249249
}
250250

251-
// acquireConfigForOpenFile loads a config file entry from the cache, or parses it if not already
251+
// acquireConfigForFile loads a config file entry from the cache, or parses it if not already
252252
// cached, then adds the open file to `retainingOpenFiles` to keep it alive in the cache.
253-
// Each `acquireConfigForOpenFile` call that passes an `openFilePath`
253+
// Each `acquireConfigForFile` call that passes an `openFilePath`
254254
// should be accompanied by an eventual `releaseConfigForOpenFile` call with the same open file.
255-
func (c *configFileRegistryBuilder) acquireConfigForOpenFile(configFileName string, configFilePath tspath.Path, openFilePath tspath.Path, logger *logging.LogTree) *tsoptions.ParsedCommandLine {
255+
func (c *configFileRegistryBuilder) acquireConfigForFile(configFileName string, configFilePath tspath.Path, filePath tspath.Path, logger *logging.LogTree) *tsoptions.ParsedCommandLine {
256256
entry, _ := c.configs.LoadOrStore(configFilePath, newConfigFileEntry(configFileName))
257257
var needsRetainOpenFile bool
258258
entry.ChangeIf(
259259
func(config *configFileEntry) bool {
260-
_, alreadyRetaining := config.retainingOpenFiles[openFilePath]
261-
needsRetainOpenFile = !alreadyRetaining
260+
if c.fs.isOpenFile(filePath) {
261+
_, alreadyRetaining := config.retainingOpenFiles[filePath]
262+
needsRetainOpenFile = !alreadyRetaining
263+
}
262264
return needsRetainOpenFile || config.pendingReload != PendingReloadNone
263265
},
264266
func(config *configFileEntry) {
265267
if needsRetainOpenFile {
266268
if config.retainingOpenFiles == nil {
267269
config.retainingOpenFiles = make(map[tspath.Path]struct{})
268270
}
269-
config.retainingOpenFiles[openFilePath] = struct{}{}
271+
config.retainingOpenFiles[filePath] = struct{}{}
270272
}
271273
c.reloadIfNeeded(config, configFileName, configFilePath, logger)
272274
},
@@ -524,8 +526,7 @@ func (c *configFileRegistryBuilder) getConfigFileNameForFile(fileName string, pa
524526
}
525527

526528
configName := c.computeConfigFileName(fileName, false, logger)
527-
528-
if _, ok := c.fs.overlays[path]; ok {
529+
if c.fs.isOpenFile(path) {
529530
c.configFileNames.Add(path, &configFileNames{
530531
nearestConfigFileName: configName,
531532
})
@@ -568,7 +569,7 @@ func (c *configFileRegistryBuilder) getAncestorConfigFileName(fileName string, p
568569
// Look for config in parent folders of config file
569570
result := c.computeConfigFileName(configFileName, true, logger)
570571

571-
if _, ok := c.fs.overlays[path]; ok {
572+
if c.fs.isOpenFile(path) {
572573
entry.Change(func(value *configFileNames) {
573574
if value.ancestors == nil {
574575
value.ancestors = make(map[string]string)

internal/project/dirty/set.go

Lines changed: 0 additions & 126 deletions
This file was deleted.

internal/project/projectcollection.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ type ProjectCollection struct {
2727
// apiOpenedProjects is the set of projects that should be kept open for
2828
// API clients.
2929
apiOpenedProjects map[tspath.Path]struct{}
30-
// Files that are open
31-
openedFiles map[tspath.Path]struct{}
3230
}
3331

3432
func (c *ProjectCollection) ConfigFileRegistry() *ConfigFileRegistry { return c.configFileRegistry }
@@ -231,7 +229,6 @@ func (c *ProjectCollection) clone() *ProjectCollection {
231229
configuredProjects: c.configuredProjects,
232230
inferredProject: c.inferredProject,
233231
fileDefaultProjects: c.fileDefaultProjects,
234-
openedFiles: c.openedFiles,
235232
}
236233
}
237234

internal/project/projectcollectionbuilder.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ type ProjectCollectionBuilder struct {
4242
fileDefaultProjects map[tspath.Path]tspath.Path
4343
configuredProjects *dirty.SyncMap[tspath.Path, *Project]
4444
inferredProject *dirty.Box[*Project]
45-
openedFiles *dirty.Set[tspath.Path]
4645

4746
apiOpenedProjects map[tspath.Path]struct{}
4847
}
@@ -71,7 +70,6 @@ func newProjectCollectionBuilder(
7170
newSnapshotID: newSnapshotID,
7271
configuredProjects: dirty.NewSyncMap(oldProjectCollection.configuredProjects, nil),
7372
inferredProject: dirty.NewBox(oldProjectCollection.inferredProject),
74-
openedFiles: dirty.NewSet(oldProjectCollection.openedFiles),
7573
apiOpenedProjects: maps.Clone(oldAPIOpenedProjects),
7674
}
7775
}
@@ -101,11 +99,6 @@ func (b *ProjectCollectionBuilder) Finalize(logger *logging.LogTree) (*ProjectCo
10199
newProjectCollection.inferredProject = newInferredProject
102100
}
103101

104-
if openedFiles, openedFilesChanged := b.openedFiles.Finalize(); openedFilesChanged {
105-
ensureCloned()
106-
newProjectCollection.openedFiles = openedFiles
107-
}
108-
109102
configFileRegistry := b.configFileRegistryBuilder.Finalize()
110103
newProjectCollection.configFileRegistry = configFileRegistry
111104
return newProjectCollection, configFileRegistry
@@ -182,7 +175,6 @@ func (b *ProjectCollectionBuilder) DidChangeFiles(summary FileChangeSummary, log
182175
if fh := b.fs.GetFileByPath(fileName, path); fh == nil || fh.Hash() != hash {
183176
changedFiles = append(changedFiles, path)
184177
}
185-
b.openedFiles.Delete(path)
186178
}
187179
for uri := range summary.Changed.Keys() {
188180
fileName := uri.FileName()
@@ -251,7 +243,6 @@ func (b *ProjectCollectionBuilder) DidChangeFiles(summary FileChangeSummary, log
251243
if summary.Opened != "" {
252244
fileName := summary.Opened.FileName()
253245
path := b.toPath(fileName)
254-
b.openedFiles.Add(path)
255246
var toRemoveProjects collections.Set[tspath.Path]
256247
openFileResult := b.ensureConfiguredProjectAndAncestorsForFile(fileName, path, logger)
257248
b.configuredProjects.Range(func(entry *dirty.SyncMapEntry[tspath.Path, *Project]) bool {
@@ -420,7 +411,7 @@ func (b *ProjectCollectionBuilder) DidRequestProject(projectId tspath.Path, logg
420411
func (b *ProjectCollectionBuilder) DidRequestEnsureDefaultProject(uri lsproto.DocumentUri, logger *logging.LogTree) {
421412
fileName := uri.FileName()
422413
path := b.toPath(fileName)
423-
if b.openedFiles.Has(path) {
414+
if b.fs.isOpenFile(path) {
424415
b.DidRequestFile(uri, logger)
425416
return
426417
}
@@ -640,7 +631,7 @@ func (b *ProjectCollectionBuilder) findDefaultConfiguredProject(fileName string,
640631

641632
func (b *ProjectCollectionBuilder) ensureConfiguredProjectAndAncestorsForFile(fileName string, path tspath.Path, logger *logging.LogTree) searchResult {
642633
result := b.findOrCreateDefaultConfiguredProjectForFile(fileName, path, projectLoadKindCreate, logger)
643-
if result.project != nil && b.openedFiles.Has(path) {
634+
if result.project != nil && b.fs.isOpenFile(path) {
644635
b.createAncestorTree(fileName, path, &result, logger)
645636
}
646637
return result
@@ -737,7 +728,7 @@ func (b *ProjectCollectionBuilder) findOrCreateDefaultConfiguredProjectWorker(
737728
},
738729
func(node searchNode) (isResult bool, stop bool) {
739730
configFilePath := b.toPath(node.configFileName)
740-
config := b.configFileRegistryBuilder.findOrAcquireConfigForOpenFile(node.configFileName, configFilePath, path, node.loadKind, node.logger.Fork("Acquiring config for open file"))
731+
config := b.configFileRegistryBuilder.findOrAcquireConfigForFile(node.configFileName, configFilePath, path, node.loadKind, node.logger.Fork("Acquiring config for open file"))
741732
if config == nil {
742733
node.logger.Log("Config file for project does not already exist")
743734
return false, false

internal/project/snapshotfs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ func (s *snapshotFSBuilder) Finalize() (*SnapshotFS, bool) {
9292
}, changed
9393
}
9494

95+
func (s *snapshotFSBuilder) isOpenFile(path tspath.Path) bool {
96+
_, ok := s.overlays[path]
97+
return ok
98+
}
99+
95100
func (s *snapshotFSBuilder) GetFile(fileName string) FileHandle {
96101
path := s.toPath(fileName)
97102
return s.GetFileByPath(fileName, path)

testdata/baselines/reference/lspservertests/declarationMaps/findAllReferences-definition-is-in-mapped-file.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ Config::
8787
RetainingProjects: *modified*
8888
/home/src/projects/project/a/tsconfig.json *new*
8989
/home/src/projects/project/b/tsconfig.json
90-
RetainingOpenFiles: *modified*
91-
/home/src/projects/project/a/a.ts *new*
9290
[/home/src/projects/project/b/tsconfig.json]
9391
RetainingProjects:
9492
/home/src/projects/project/b/tsconfig.json

testdata/baselines/reference/lspservertests/declarationMaps/findAllReferences.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ Config::
103103
[/home/src/projects/project/a/tsconfig.json] *new*
104104
RetainingProjects:
105105
/home/src/projects/project/a/tsconfig.json
106-
RetainingOpenFiles:
107-
/home/src/projects/project/a/a.ts
108106
// === /home/src/projects/project/a/a.ts ===
109107
// export function [|fnA|]() {}
110108
// export interface IfaceA {}
@@ -148,11 +146,7 @@ Open Files::
148146
[/home/src/projects/project/dummy/dummy.ts] *new*
149147
/home/src/projects/project/dummy/tsconfig.json (default)
150148
Config::
151-
[/home/src/projects/project/a/tsconfig.json] *modified*
152-
RetainingProjects: *modified*
153-
/home/src/projects/project/a/tsconfig.json *deleted*
154-
RetainingOpenFiles:
155-
/home/src/projects/project/a/a.ts
149+
[/home/src/projects/project/a/tsconfig.json] *deleted*
156150
[/home/src/projects/project/dummy/tsconfig.json] *new*
157151
RetainingProjects:
158152
/home/src/projects/project/dummy/tsconfig.json

testdata/baselines/reference/lspservertests/declarationMaps/opening-original-location-project-disableSourceOfProjectReferenceRedirect.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ Config::
8585
RetainingProjects: *modified*
8686
/user/username/projects/a/tsconfig.json *new*
8787
/user/username/projects/b/tsconfig.json
88-
RetainingOpenFiles: *modified*
89-
/user/username/projects/a/a.ts *new*
9088
[/user/username/projects/b/tsconfig.json]
9189
RetainingProjects:
9290
/user/username/projects/b/tsconfig.json

testdata/baselines/reference/lspservertests/declarationMaps/opening-original-location-project.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ Config::
8585
RetainingProjects: *modified*
8686
/user/username/projects/a/tsconfig.json *new*
8787
/user/username/projects/b/tsconfig.json
88-
RetainingOpenFiles: *modified*
89-
/user/username/projects/a/a.ts *new*
9088
[/user/username/projects/b/tsconfig.json]
9189
RetainingProjects:
9290
/user/username/projects/b/tsconfig.json

testdata/baselines/reference/lspservertests/declarationMaps/rename-before-project-is-built.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@ Config::
163163
RetainingProjects:
164164
/user/username/projects/myproject/main/tsconfig.json
165165
/user/username/projects/myproject/tsconfig.json
166-
RetainingOpenFiles:
167-
/user/username/projects/myproject/main/main.ts
168166
[/user/username/projects/myproject/tsconfig.json] *new*
169167
RetainingProjects:
170168
/user/username/projects/myproject/tsconfig.json
@@ -276,12 +274,7 @@ Open Files::
276274
/user/username/projects/random/tsconfig.json (default)
277275
Config::
278276
[/user/username/projects/myproject/dependency/tsconfig.json] *deleted*
279-
[/user/username/projects/myproject/main/tsconfig.json] *modified*
280-
RetainingProjects: *modified*
281-
/user/username/projects/myproject/main/tsconfig.json *deleted*
282-
/user/username/projects/myproject/tsconfig.json *deleted*
283-
RetainingOpenFiles:
284-
/user/username/projects/myproject/main/main.ts
277+
[/user/username/projects/myproject/main/tsconfig.json] *deleted*
285278
[/user/username/projects/myproject/tsconfig.json] *deleted*
286279
[/user/username/projects/random/tsconfig.json]
287280
RetainingProjects:

0 commit comments

Comments
 (0)