@@ -23,12 +23,47 @@ type Config struct {
23
23
ExcludePaths []string
24
24
}
25
25
26
+ func findFileCreator () func (file , prefix string ) (string , string , error ) {
27
+ cache := make (map [string ]* build.Package )
28
+
29
+ return func (file , prefix string ) (string , string , error ) {
30
+ profileFile := file
31
+
32
+ noPrefixName := stripPrefix (file , prefix )
33
+ if _ , err := os .Stat (noPrefixName ); err == nil { // coverage-ignore
34
+ return noPrefixName , noPrefixName , nil
35
+ }
36
+
37
+ dir , file := filepath .Split (file )
38
+ pkg , exists := cache [dir ]
39
+
40
+ if ! exists {
41
+ var err error
42
+
43
+ pkg , err = build .Import (dir , "." , build .FindOnly )
44
+ if err != nil {
45
+ return "" , "" , fmt .Errorf ("can't find file %q: %w" , profileFile , err )
46
+ }
47
+
48
+ cache [dir ] = pkg
49
+ }
50
+
51
+ file = filepath .Join (pkg .Dir , file )
52
+ if _ , err := os .Stat (file ); err == nil {
53
+ return file , stripPrefix (path .NormalizeForTool (file ), path .NormalizeForTool (pkg .Root )), nil
54
+ }
55
+
56
+ return "" , "" , fmt .Errorf ("can't find file %q" , profileFile )
57
+ }
58
+ }
59
+
26
60
func GenerateCoverageStats (cfg Config ) ([]Stats , error ) {
27
61
profiles , err := parseProfiles (cfg .Profiles )
28
62
if err != nil {
29
63
return nil , fmt .Errorf ("parsing profiles: %w" , err )
30
64
}
31
65
66
+ findFile := findFileCreator ()
32
67
fileStats := make ([]Stats , 0 , len (profiles ))
33
68
excludeRules := compileExcludePathRules (cfg .ExcludePaths )
34
69
@@ -72,30 +107,6 @@ func GenerateCoverageStats(cfg Config) ([]Stats, error) {
72
107
return fileStats , nil
73
108
}
74
109
75
- // findFile finds the location of the named file in GOROOT, GOPATH etc.
76
- func findFile (file , prefix string ) (string , string , error ) {
77
- profileFile := file
78
-
79
- noPrefixName := stripPrefix (file , prefix )
80
- if _ , err := os .Stat (noPrefixName ); err == nil { // coverage-ignore
81
- return noPrefixName , noPrefixName , nil
82
- }
83
-
84
- dir , file := filepath .Split (file )
85
-
86
- pkg , err := build .Import (dir , "." , build .FindOnly )
87
- if err != nil {
88
- return "" , "" , fmt .Errorf ("can't find file %q: %w" , profileFile , err )
89
- }
90
-
91
- file = filepath .Join (pkg .Dir , file )
92
- if _ , err := os .Stat (file ); err == nil {
93
- return file , stripPrefix (path .NormalizeForTool (file ), path .NormalizeForTool (pkg .Root )), nil
94
- }
95
-
96
- return "" , "" , fmt .Errorf ("can't find file %q" , profileFile )
97
- }
98
-
99
110
func findAnnotations (source []byte ) ([]extent , error ) {
100
111
fset := token .NewFileSet ()
101
112
0 commit comments