diff --git a/internal/module/resolver.go b/internal/module/resolver.go index 70b1b9afac..815c3ad62b 100644 --- a/internal/module/resolver.go +++ b/internal/module/resolver.go @@ -1669,21 +1669,17 @@ func (r *resolutionState) getPackageJsonInfo(packageDirectory string, onlyRecord Fields: packageJsonContent, }, } - if !r.resolver.packageJsonInfoCache.IsReadonly { - r.resolver.packageJsonInfoCache.Set(packageJsonPath, result) - } + result = r.resolver.packageJsonInfoCache.Set(packageJsonPath, result) r.affectingLocations = append(r.affectingLocations, packageJsonPath) return result } else { if directoryExists && r.tracer != nil { r.tracer.write(diagnostics.File_0_does_not_exist.Format(packageJsonPath)) } - if !r.resolver.packageJsonInfoCache.IsReadonly { - r.resolver.packageJsonInfoCache.Set(packageJsonPath, &packagejson.InfoCacheEntry{ - PackageDirectory: packageDirectory, - DirectoryExists: directoryExists, - }) - } + _ = r.resolver.packageJsonInfoCache.Set(packageJsonPath, &packagejson.InfoCacheEntry{ + PackageDirectory: packageDirectory, + DirectoryExists: directoryExists, + }) r.failedLookupLocations = append(r.failedLookupLocations, packageJsonPath) } return nil diff --git a/internal/packagejson/cache.go b/internal/packagejson/cache.go index 0736a260a4..7b72a4e3e9 100644 --- a/internal/packagejson/cache.go +++ b/internal/packagejson/cache.go @@ -121,9 +121,7 @@ func (p *InfoCacheEntry) GetDirectory() string { } type InfoCache struct { - mu sync.RWMutex - IsReadonly bool - cache map[tspath.Path]InfoCacheEntry + cache collections.SyncMap[tspath.Path, *InfoCacheEntry] currentDirectory string useCaseSensitiveFileNames bool } @@ -136,22 +134,15 @@ 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() +func (p *InfoCache) Set(packageJsonPath string, info *InfoCacheEntry) *InfoCacheEntry { key := tspath.ToPath(packageJsonPath, p.currentDirectory, p.useCaseSensitiveFileNames) - if p.cache == nil { - p.cache = make(map[tspath.Path]InfoCacheEntry) - } - p.cache[key] = *info + actual, _ := p.cache.LoadOrStore(key, info) + return actual }