Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 2 deletions internal/module/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1669,7 +1669,7 @@ func (r *resolutionState) getPackageJsonInfo(packageDirectory string, onlyRecord
Fields: packageJsonContent,
},
}
if !r.resolver.packageJsonInfoCache.IsReadonly {
if !r.resolver.packageJsonInfoCache.IsReadonly() {
r.resolver.packageJsonInfoCache.Set(packageJsonPath, result)
}
r.affectingLocations = append(r.affectingLocations, packageJsonPath)
Expand All @@ -1678,7 +1678,7 @@ func (r *resolutionState) getPackageJsonInfo(packageDirectory string, onlyRecord
if directoryExists && r.tracer != nil {
r.tracer.write(diagnostics.File_0_does_not_exist.Format(packageJsonPath))
}
if !r.resolver.packageJsonInfoCache.IsReadonly {
if !r.resolver.packageJsonInfoCache.IsReadonly() {
r.resolver.packageJsonInfoCache.Set(packageJsonPath, &packagejson.InfoCacheEntry{
PackageDirectory: packageDirectory,
DirectoryExists: directoryExists,
Expand Down
33 changes: 18 additions & 15 deletions internal/packagejson/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package packagejson

import (
"sync"
"sync/atomic"

"github.com/microsoft/typescript-go/internal/collections"
"github.com/microsoft/typescript-go/internal/core"
Expand Down Expand Up @@ -121,9 +122,8 @@ func (p *InfoCacheEntry) GetDirectory() string {
}

type InfoCache struct {
mu sync.RWMutex
IsReadonly bool
cache map[tspath.Path]InfoCacheEntry
cache collections.SyncMap[tspath.Path, *InfoCacheEntry]
isReadonly atomic.Bool
currentDirectory string
useCaseSensitiveFileNames bool
}
Expand All @@ -136,22 +136,25 @@ func NewInfoCache(currentDirectory string, useCaseSensitiveFileNames bool) *Info
}

func (p *InfoCache) Get(packageJsonPath string) *InfoCacheEntry {
p.mu.RLock()
defer p.mu.RUnlock()
key := tspath.ToPath(packageJsonPath, p.currentDirectory, p.useCaseSensitiveFileNames)
entry, ok := p.cache[key]
if !ok {
return nil
if value, ok := p.cache.Load(key); ok {
return value
}
return &entry
return nil
}

func (p *InfoCache) Set(packageJsonPath string, info *InfoCacheEntry) {
p.mu.Lock()
defer p.mu.Unlock()
key := tspath.ToPath(packageJsonPath, p.currentDirectory, p.useCaseSensitiveFileNames)
if p.cache == nil {
p.cache = make(map[tspath.Path]InfoCacheEntry)
if p.isReadonly.Load() {
return
}
p.cache[key] = *info
key := tspath.ToPath(packageJsonPath, p.currentDirectory, p.useCaseSensitiveFileNames)
p.cache.Store(key, info)
}

func (p *InfoCache) SetReadonly(readonly bool) {
p.isReadonly.Store(readonly)
}

func (p *InfoCache) IsReadonly() bool {
return p.isReadonly.Load()
}
Loading