Skip to content

Commit b87262b

Browse files
committed
chore(runner_action.go): extract cache related elements
1 parent 8077a19 commit b87262b

File tree

3 files changed

+123
-114
lines changed

3 files changed

+123
-114
lines changed

pkg/goanalysis/runner_action.go

Lines changed: 0 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ import (
44
"errors"
55
"fmt"
66
"go/types"
7-
"io"
87
"reflect"
98
"runtime/debug"
109
"time"
1110

1211
"golang.org/x/tools/go/analysis"
1312
"golang.org/x/tools/go/packages"
14-
"golang.org/x/tools/go/types/objectpath"
1513

16-
"github.com/golangci/golangci-lint/internal/cache"
1714
"github.com/golangci/golangci-lint/internal/errorutil"
1815
"github.com/golangci/golangci-lint/pkg/goanalysis/pkgerrors"
1916
)
@@ -66,27 +63,6 @@ func (act *action) String() string {
6663
return fmt.Sprintf("%s@%s", act.a, act.pkg)
6764
}
6865

69-
func (act *action) loadCachedFacts() bool {
70-
if act.loadCachedFactsDone { // can't be set in parallel
71-
return act.loadCachedFactsOk
72-
}
73-
74-
res := func() bool {
75-
if act.isInitialPkg {
76-
return true // load cached facts only for non-initial packages
77-
}
78-
79-
if len(act.a.FactTypes) == 0 {
80-
return true // no need to load facts
81-
}
82-
83-
return act.loadPersistedFacts()
84-
}()
85-
act.loadCachedFactsDone = true
86-
act.loadCachedFactsOk = res
87-
return res
88-
}
89-
9066
func (act *action) waitUntilDependingAnalyzersWorked() {
9167
for _, dep := range act.deps {
9268
if dep.pkg == act.pkg {
@@ -287,91 +263,6 @@ func (act *action) factType(fact analysis.Fact) reflect.Type {
287263
return t
288264
}
289265

290-
func (act *action) persistFactsToCache() error {
291-
analyzer := act.a
292-
if len(analyzer.FactTypes) == 0 {
293-
return nil
294-
}
295-
296-
// Merge new facts into the package and persist them.
297-
var facts []Fact
298-
for key, fact := range act.packageFacts {
299-
if key.pkg != act.pkg.Types {
300-
// The fact is from inherited facts from another package
301-
continue
302-
}
303-
facts = append(facts, Fact{
304-
Path: "",
305-
Fact: fact,
306-
})
307-
}
308-
for key, fact := range act.objectFacts {
309-
obj := key.obj
310-
if obj.Pkg() != act.pkg.Types {
311-
// The fact is from inherited facts from another package
312-
continue
313-
}
314-
315-
path, err := objectpath.For(obj)
316-
if err != nil {
317-
// The object is not globally addressable
318-
continue
319-
}
320-
321-
facts = append(facts, Fact{
322-
Path: string(path),
323-
Fact: fact,
324-
})
325-
}
326-
327-
factsCacheDebugf("Caching %d facts for package %q and analyzer %s", len(facts), act.pkg.Name, act.a.Name)
328-
329-
key := fmt.Sprintf("%s/facts", analyzer.Name)
330-
return act.r.pkgCache.Put(act.pkg, cache.HashModeNeedAllDeps, key, facts)
331-
}
332-
333-
func (act *action) loadPersistedFacts() bool {
334-
var facts []Fact
335-
key := fmt.Sprintf("%s/facts", act.a.Name)
336-
if err := act.r.pkgCache.Get(act.pkg, cache.HashModeNeedAllDeps, key, &facts); err != nil {
337-
if !errors.Is(err, cache.ErrMissing) && !errors.Is(err, io.EOF) {
338-
act.r.log.Warnf("Failed to get persisted facts: %s", err)
339-
}
340-
341-
factsCacheDebugf("No cached facts for package %q and analyzer %s", act.pkg.Name, act.a.Name)
342-
return false
343-
}
344-
345-
factsCacheDebugf("Loaded %d cached facts for package %q and analyzer %s", len(facts), act.pkg.Name, act.a.Name)
346-
347-
for _, f := range facts {
348-
if f.Path == "" { // this is a package fact
349-
key := packageFactKey{act.pkg.Types, act.factType(f.Fact)}
350-
act.packageFacts[key] = f.Fact
351-
continue
352-
}
353-
obj, err := objectpath.Object(act.pkg.Types, objectpath.Path(f.Path))
354-
if err != nil {
355-
// Be lenient about these errors. For example, when
356-
// analyzing io/ioutil from source, we may get a fact
357-
// for methods on the devNull type, and objectpath
358-
// will happily create a path for them. However, when
359-
// we later load io/ioutil from export data, the path
360-
// no longer resolves.
361-
//
362-
// If an exported type embeds the unexported type,
363-
// then (part of) the unexported type will become part
364-
// of the type information and our path will resolve
365-
// again.
366-
continue
367-
}
368-
factKey := objectFactKey{obj, act.factType(f.Fact)}
369-
act.objectFacts[factKey] = f.Fact
370-
}
371-
372-
return true
373-
}
374-
375266
func (act *action) markDepsForAnalyzingSource() {
376267
// Horizontal deps (analyzer.Requires) must be loaded from source and analyzed before analyzing
377268
// this action.

pkg/goanalysis/runner_action_cache.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package goanalysis
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io"
7+
8+
"golang.org/x/tools/go/analysis"
9+
"golang.org/x/tools/go/types/objectpath"
10+
11+
"github.com/golangci/golangci-lint/internal/cache"
12+
)
13+
14+
type Fact struct {
15+
Path string // non-empty only for object facts
16+
Fact analysis.Fact
17+
}
18+
19+
func (act *action) loadCachedFacts() bool {
20+
if act.loadCachedFactsDone { // can't be set in parallel
21+
return act.loadCachedFactsOk
22+
}
23+
24+
res := func() bool {
25+
if act.isInitialPkg {
26+
return true // load cached facts only for non-initial packages
27+
}
28+
29+
if len(act.a.FactTypes) == 0 {
30+
return true // no need to load facts
31+
}
32+
33+
return act.loadPersistedFacts()
34+
}()
35+
act.loadCachedFactsDone = true
36+
act.loadCachedFactsOk = res
37+
return res
38+
}
39+
40+
func (act *action) persistFactsToCache() error {
41+
analyzer := act.a
42+
if len(analyzer.FactTypes) == 0 {
43+
return nil
44+
}
45+
46+
// Merge new facts into the package and persist them.
47+
var facts []Fact
48+
for key, fact := range act.packageFacts {
49+
if key.pkg != act.pkg.Types {
50+
// The fact is from inherited facts from another package
51+
continue
52+
}
53+
facts = append(facts, Fact{
54+
Path: "",
55+
Fact: fact,
56+
})
57+
}
58+
for key, fact := range act.objectFacts {
59+
obj := key.obj
60+
if obj.Pkg() != act.pkg.Types {
61+
// The fact is from inherited facts from another package
62+
continue
63+
}
64+
65+
path, err := objectpath.For(obj)
66+
if err != nil {
67+
// The object is not globally addressable
68+
continue
69+
}
70+
71+
facts = append(facts, Fact{
72+
Path: string(path),
73+
Fact: fact,
74+
})
75+
}
76+
77+
factsCacheDebugf("Caching %d facts for package %q and analyzer %s", len(facts), act.pkg.Name, act.a.Name)
78+
79+
key := fmt.Sprintf("%s/facts", analyzer.Name)
80+
return act.r.pkgCache.Put(act.pkg, cache.HashModeNeedAllDeps, key, facts)
81+
}
82+
83+
func (act *action) loadPersistedFacts() bool {
84+
var facts []Fact
85+
key := fmt.Sprintf("%s/facts", act.a.Name)
86+
if err := act.r.pkgCache.Get(act.pkg, cache.HashModeNeedAllDeps, key, &facts); err != nil {
87+
if !errors.Is(err, cache.ErrMissing) && !errors.Is(err, io.EOF) {
88+
act.r.log.Warnf("Failed to get persisted facts: %s", err)
89+
}
90+
91+
factsCacheDebugf("No cached facts for package %q and analyzer %s", act.pkg.Name, act.a.Name)
92+
return false
93+
}
94+
95+
factsCacheDebugf("Loaded %d cached facts for package %q and analyzer %s", len(facts), act.pkg.Name, act.a.Name)
96+
97+
for _, f := range facts {
98+
if f.Path == "" { // this is a package fact
99+
key := packageFactKey{act.pkg.Types, act.factType(f.Fact)}
100+
act.packageFacts[key] = f.Fact
101+
continue
102+
}
103+
obj, err := objectpath.Object(act.pkg.Types, objectpath.Path(f.Path))
104+
if err != nil {
105+
// Be lenient about these errors. For example, when
106+
// analyzing io/ioutil from source, we may get a fact
107+
// for methods on the devNull type, and objectpath
108+
// will happily create a path for them. However, when
109+
// we later load io/ioutil from export data, the path
110+
// no longer resolves.
111+
//
112+
// If an exported type embeds the unexported type,
113+
// then (part of) the unexported type will become part
114+
// of the type information and our path will resolve
115+
// again.
116+
continue
117+
}
118+
factKey := objectFactKey{obj, act.factType(f.Fact)}
119+
act.objectFacts[factKey] = f.Fact
120+
}
121+
122+
return true
123+
}

pkg/goanalysis/runner_facts.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ type packageFactKey struct {
2020
typ reflect.Type
2121
}
2222

23-
type Fact struct {
24-
Path string // non-empty only for object facts
25-
Fact analysis.Fact
26-
}
27-
2823
// inheritFacts populates act.facts with
2924
// those it obtains from its dependency, dep.
3025
func inheritFacts(act, dep *action) {

0 commit comments

Comments
 (0)