Skip to content

Commit e267e66

Browse files
committed
fix #302: fix concurrent astcache access
1 parent dba3907 commit e267e66

File tree

3 files changed

+9
-28
lines changed

3 files changed

+9
-28
lines changed

pkg/lint/astcache/astcache.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"go/parser"
66
"go/token"
77
"path/filepath"
8-
"strings"
98
"time"
109

1110
"golang.org/x/tools/go/packages"
@@ -34,19 +33,9 @@ func NewCache(log logutils.Log) *Cache {
3433
}
3534

3635
func (c Cache) Get(filename string) *File {
37-
return c.m[filepath.Clean(filename)]
38-
}
39-
40-
func (c Cache) keys() []string {
41-
var keys []string
42-
for k := range c.m {
43-
keys = append(keys, k)
44-
}
45-
return keys
46-
}
47-
48-
func (c Cache) GetOrParse(filename string, fset *token.FileSet) *File {
49-
if !filepath.IsAbs(filename) {
36+
if filepath.IsAbs(filename) {
37+
filename = filepath.Clean(filename)
38+
} else {
5039
absFilename, err := filepath.Abs(filename)
5140
if err != nil {
5241
c.log.Warnf("Can't abs-ify filename %s: %s", filename, err)
@@ -55,14 +44,6 @@ func (c Cache) GetOrParse(filename string, fset *token.FileSet) *File {
5544
}
5645
}
5746

58-
f := c.m[filename]
59-
if f != nil {
60-
return f
61-
}
62-
63-
c.log.Infof("Parse AST for file %s on demand, existing files are %s",
64-
filename, strings.Join(c.keys(), ","))
65-
c.parseFile(filename, fset)
6647
return c.m[filename]
6748
}
6849

pkg/result/processors/autogenerated_exclude.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ func (p *AutogeneratedExclude) getOrCreateFileSummary(i *result.Issue) (*ageFile
9292
return nil, fmt.Errorf("no file path for issue")
9393
}
9494

95-
f := p.astCache.GetOrParse(i.FilePath(), nil)
96-
if f.Err != nil {
97-
return nil, fmt.Errorf("can't parse file %s: %s", i.FilePath(), f.Err)
95+
f := p.astCache.Get(i.FilePath())
96+
if f == nil || f.Err != nil {
97+
return nil, fmt.Errorf("can't parse file %s: %v", i.FilePath(), f)
9898
}
9999

100100
autogenDebugf("file %q: astcache file is %+v", i.FilePath(), *f)

pkg/result/processors/nolint.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ func (p *Nolint) getOrCreateFileData(i *result.Issue) (*fileData, error) {
8787
return nil, fmt.Errorf("no file path for issue")
8888
}
8989

90-
file := p.astCache.GetOrParse(i.FilePath(), nil)
91-
if file.Err != nil {
92-
return nil, fmt.Errorf("can't parse file %s: %s", i.FilePath(), file.Err)
90+
file := p.astCache.Get(i.FilePath())
91+
if file == nil || file.Err != nil {
92+
return nil, fmt.Errorf("can't parse file %s: %v", i.FilePath(), file)
9393
}
9494

9595
fd.ignoredRanges = p.buildIgnoredRangesForFile(file.F, file.Fset, i.FilePath())

0 commit comments

Comments
 (0)