Skip to content

Commit ee1fc86

Browse files
committed
internal/imports: use cache of mod cache pkgs in find packages
To check if a package is in a module that is in scope, the module resolver checks if there are Go files that would be included in a package in the directory matching the import path in scope. If this directory is in the module cache and we have saved it as a package, we know this directory contains Go files, and do not have to read the directory. Change-Id: I7c9365ce42c760ab95bc68b036212120895c89fb Reviewed-on: https://go-review.googlesource.com/c/tools/+/186922 Run-TryBot: Suzy Mueller <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 857b4dd commit ee1fc86

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

internal/imports/mod.go

+11
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ func (r *ModuleResolver) findPackage(importPath string) (*ModuleJSON, string) {
121121
continue
122122
}
123123

124+
if info, ok := r.moduleCacheInfo.Load(pkgDir); ok {
125+
if packageScanned, err := info.reachedStatus(directoryScanned); packageScanned {
126+
if err != nil {
127+
// There was some error with scanning this directory.
128+
// It does not contain a valid package.
129+
continue
130+
}
131+
return m, pkgDir
132+
}
133+
}
134+
124135
pkgFiles, err := ioutil.ReadDir(pkgDir)
125136
if err != nil {
126137
continue

internal/imports/mod_cache.go

+12
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ type directoryPackageInfo struct {
5151
needsReplace bool
5252
}
5353

54+
// reachedStatus returns true when info has a status at least target and any error associated with
55+
// an attempt to reach target.
56+
func (info *directoryPackageInfo) reachedStatus(target directoryPackageStatus) (bool, error) {
57+
if info.err == nil {
58+
return info.status >= target, nil
59+
}
60+
if info.status == target {
61+
return true, info.err
62+
}
63+
return true, nil
64+
}
65+
5466
// moduleCacheInfo is a concurrency safe map for storing information about
5567
// the directories in the module cache.
5668
//

internal/imports/mod_cache_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package imports
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestDirectoryPackageInfoReachedStatus(t *testing.T) {
9+
tests := []struct {
10+
info directoryPackageInfo
11+
target directoryPackageStatus
12+
wantStatus bool
13+
wantError bool
14+
}{
15+
{
16+
info: directoryPackageInfo{
17+
status: directoryScanned,
18+
err: nil,
19+
},
20+
target: directoryScanned,
21+
wantStatus: true,
22+
},
23+
{
24+
info: directoryPackageInfo{
25+
status: directoryScanned,
26+
err: fmt.Errorf("error getting to directory scanned"),
27+
},
28+
target: directoryScanned,
29+
wantStatus: true,
30+
wantError: true,
31+
},
32+
{
33+
info: directoryPackageInfo{},
34+
target: directoryScanned,
35+
wantStatus: false,
36+
},
37+
}
38+
39+
for _, tt := range tests {
40+
gotStatus, gotErr := tt.info.reachedStatus(tt.target)
41+
if gotErr != nil {
42+
if !tt.wantError {
43+
t.Errorf("unexpected error: %s", gotErr)
44+
}
45+
continue
46+
}
47+
48+
if tt.wantStatus != gotStatus {
49+
t.Errorf("reached status expected: %v, got: %v", tt.wantStatus, gotStatus)
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)