Skip to content

Move version from source file to document registry #1075

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

Merged
merged 3 commits into from
Jun 6, 2025
Merged
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: 0 additions & 4 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -9994,10 +9994,6 @@ type SourceFile struct {
lineMapMu sync.RWMutex
lineMap []core.TextPos

// Fields set by document registry

Version int

// Fields set by language service

tokenCacheMu sync.Mutex
Expand Down
15 changes: 12 additions & 3 deletions internal/project/documentregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func newRegistryKey(options *core.CompilerOptions, path tspath.Path, scriptKind

type registryEntry struct {
sourceFile *ast.SourceFile
version int
refCount int
mu sync.Mutex
}
Expand Down Expand Up @@ -94,22 +95,22 @@ func (r *DocumentRegistry) getDocumentWorker(
if entry, ok := r.documents.Load(key); ok {
// We have an entry for this file. However, it may be for a different version of
// the script snapshot. If so, update it appropriately.
if entry.sourceFile.Version != scriptInfoVersion {
if entry.version != scriptInfoVersion {
sourceFile := parser.ParseSourceFile(scriptInfo.fileName, scriptInfo.path, scriptInfoText, scriptTarget, scanner.JSDocParsingModeParseAll)
sourceFile.Version = scriptInfoVersion
entry.mu.Lock()
defer entry.mu.Unlock()
entry.sourceFile = sourceFile
entry.version = scriptInfoVersion
}
entry.refCount++
return entry.sourceFile
} else {
// Have never seen this file with these settings. Create a new source file for it.
sourceFile := parser.ParseSourceFile(scriptInfo.fileName, scriptInfo.path, scriptInfoText, scriptTarget, scanner.JSDocParsingModeParseAll)
sourceFile.Version = scriptInfoVersion
entry, _ := r.documents.LoadOrStore(key, &registryEntry{
sourceFile: sourceFile,
refCount: 0,
version: scriptInfoVersion,
})
entry.mu.Lock()
defer entry.mu.Unlock()
Expand All @@ -118,6 +119,14 @@ func (r *DocumentRegistry) getDocumentWorker(
}
}

func (r *DocumentRegistry) getFileVersion(file *ast.SourceFile, options *core.CompilerOptions) int {
key := newRegistryKey(options, file.Path(), file.ScriptKind)
if entry, ok := r.documents.Load(key); ok && entry.sourceFile == file {
return entry.version
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not enough i think. The source file may have been updated for the compilerOptions by other program and hence the source file may be newer = script info version. You actually want to check if file == entry.sourceFile as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks!

}
return -1
}

// size should only be used for testing.
func (r *DocumentRegistry) size() int {
return r.documents.Size()
Expand Down
9 changes: 7 additions & 2 deletions internal/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type snapshot struct {
func (s *snapshot) GetLineMap(fileName string) *ls.LineMap {
file := s.program.GetSourceFile(fileName)
scriptInfo := s.project.host.GetScriptInfoByPath(file.Path())
if file.Version == scriptInfo.Version() {
if s.project.getFileVersion(file, s.program.Options()) == scriptInfo.Version() {
return scriptInfo.LineMap()
}
return ls.ComputeLineStarts(file.Text())
Expand Down Expand Up @@ -1015,12 +1015,13 @@ func (p *Project) print(writeFileNames bool, writeFileExplanation bool, writeFil
builder.WriteString("\n\tFiles (0) NoProgram\n")
} else {
sourceFiles := p.program.GetSourceFiles()
options := p.program.Options()
builder.WriteString(fmt.Sprintf("\n\tFiles (%d)\n", len(sourceFiles)))
if writeFileNames {
for _, sourceFile := range sourceFiles {
builder.WriteString("\n\t\t" + sourceFile.FileName())
if writeFileVersionAndText {
builder.WriteString(fmt.Sprintf(" %d %s", sourceFile.Version, sourceFile.Text()))
builder.WriteString(fmt.Sprintf(" %d %s", p.getFileVersion(sourceFile, options), sourceFile.Text()))
}
}
// !!!
Expand All @@ -1031,6 +1032,10 @@ func (p *Project) print(writeFileNames bool, writeFileExplanation bool, writeFil
return builder.String()
}

func (p *Project) getFileVersion(file *ast.SourceFile, options *core.CompilerOptions) int {
return p.host.DocumentRegistry().getFileVersion(file, options)
}

func (p *Project) Log(s string) {
p.host.Log(s)
}
Expand Down