Merge pull request #197 from kenshaw/fix-pkgpath-concurrency-issue

Fixes a concurrency issue with package path cache
This commit is contained in:
Vasily Romanov
2019-02-21 10:43:41 +03:00
committed by GitHub
+13 -5
View File
@@ -10,6 +10,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
)
func getPkgPath(fname string, isDir bool) (string, error) {
@@ -34,9 +35,12 @@ func getPkgPath(fname string, isDir bool) (string, error) {
return getPkgPathFromGOPATH(fname, isDir)
}
var (
goModPathCache = make(map[string]string)
)
var goModPathCache = struct {
paths map[string]string
sync.RWMutex
}{
paths: make(map[string]string),
}
// empty if no go.mod, GO111MODULE=off or go without go modules support
func goModPath(fname string, isDir bool) (string, error) {
@@ -45,13 +49,17 @@ func goModPath(fname string, isDir bool) (string, error) {
root = filepath.Dir(fname)
}
goModPath, ok := goModPathCache[root]
goModPathCache.RLock()
goModPath, ok := goModPathCache.paths[root]
goModPathCache.RUnlock()
if ok {
return goModPath, nil
}
defer func() {
goModPathCache[root] = goModPath
goModPathCache.Lock()
goModPathCache.paths[root] = goModPath
goModPathCache.Unlock()
}()
cmd := exec.Command("go", "env", "GOMOD")