-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.go
280 lines (247 loc) · 7.98 KB
/
import.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"errors"
"fmt"
"go/parser"
"go/token"
"io/fs"
"path/filepath"
"slices"
"strings"
"sync"
"golang.org/x/tools/go/packages"
)
const stdlib = "std"
// once is used to ensure that the stdLibPkgs map is populated only once
var (
once = &sync.Once{} //nolint:gochecknoglobals
stdLibPkgs = map[string]struct{}{ //nolint:gochecknoglobals
"C": {}, // cGo. see: https://blog.golang.org/cgo
}
)
func isStdLibPkg(pkg, std string) (bool, error) {
var err error
once.Do(func() {
pkgs, errL := packages.Load(nil, std)
if errL != nil {
// set stdLibPkgs to empty when error occurs.
stdLibPkgs = map[string]struct{}{}
err = errL
}
if len(pkgs) < 10 {
// it means an error occured since
// we will always have more than 10 pkgs in the Go standard lib
for _, p := range pkgs {
if len(p.Errors) > 0 {
err = errors.New(p.Errors[0].Msg)
}
}
}
for _, p := range pkgs {
stdLibPkgs[p.PkgPath] = struct{}{}
}
})
_, ok := stdLibPkgs[pkg]
return ok, err
}
// fetchImports returns all the imports found in one .go file
func fetchImports(file string) ([]string, error) {
fset := token.NewFileSet()
var src interface{} = nil
mode := parser.ImportsOnly
f, err := parser.ParseFile(fset, file, src, mode)
if err != nil {
return nil, err
}
impPaths := make([]string, 0)
for _, impPath := range f.Imports {
if impPath != nil {
if impPath.Path != nil {
p := impPath.Path.Value
p = strings.Trim(p, "\"")
isStdlib, _ := isStdLibPkg(p, stdlib) // ignore error, since when error happens; the other value will be false
if !isStdlib {
impPaths = append(impPaths, p)
}
}
}
}
return impPaths, nil
}
// Usage:
//
// fetchModule("testdata/modfiles/mod1/", "github.com/hashicorp/nomad/drivers/shared/executor")
func fetchModule(root, importPath string) (string, error) {
cfg := &packages.Config{
Mode: packages.NeedModule,
Tests: false,
Dir: root,
}
pkgs, err := packages.Load(
cfg,
fmt.Sprintf("pattern=%s", importPath),
// to show the Go commands that this method call will invoke;
// run ote while the env var `export GOPACKAGESDEBUG=true` is set on the commandline
)
if err != nil {
return "", err
}
if len(pkgs) > 1 {
return "", fmt.Errorf("import %s produced greater than 1 packages", importPath)
}
if len(pkgs) <= 0 {
return "", fmt.Errorf("import %s does not belong to any package", importPath)
}
pkg := pkgs[0]
if pkg.Module == nil {
if len(pkg.Errors) > 0 {
if pkg.Errors[0].Kind == packages.ListError && strings.Contains(pkg.Errors[0].Msg, "build constraints exclude all Go files") {
// If a package depends on an import that requires an explicit build tag,
// then, `packages.Load()` is going to fail. The only way to fix it would be to pass in
// the explicit build tag required by that dependency to `packages.Config.BuildFlags`
// However, we do not know in advance what the required build tags are. That's why the call
// to `packages.Load()/packages.Config` do not have any build tag specified.
//
// As an example, `/testdata/modfiles/mod1/` depends on `golang.org/x/sys/windows`. That dependency
// has the build tag; `+build windows`: https://github.com/golang/sys/blob/0f9fa26af87c481a6877a4ca1330699ba9a30673/windows/aliases.go#L5-L8
// and thus `packages.Load()` fails with error;
// `build constraints exclude all Go files in /pkg/mod/golang.org/x/[email protected]/windows`
//
// We can always add more errors here as/when we discover them
return "", nil
} else {
return "", fmt.Errorf("unable to find module for import %s : %s", importPath, pkg.Errors[0].Msg)
}
} else {
// this can be raised if an import path is inside a file that has some build tag
// that ote didn't take into account.
// see: https://github.com/komuw/ote/issues/3
return "", fmt.Errorf("import %s does not belong to any module", importPath)
}
}
return pkg.Module.Path, nil
}
// getAllTestModules finds all the Go modules used only in test files.
func getAllTestModules(testImportPaths, nonTestImportPaths []string, root string) (testModules []string, err error) {
// There could be some import paths that exist in both test files & non-test files.
// In hashicorp/nomad we found that to be about 50% of imports.
// In juju/juju it is about 80%
// see: https://github.com/komuw/ote/issues/22
//
// Given that, it then only makes sense to filter out this import paths that are common
// before calling fetchModule(which is one of the most expensive calls in ote)
testOnlyMods := []string{}
for _, v := range testImportPaths {
m, errF := fetchModule(root, v)
if errF != nil {
return testModules, errF
}
testOnlyMods = append(testOnlyMods, m)
}
nonTestMods := []string{}
for _, v := range nonTestImportPaths {
m, errF := fetchModule(root, v)
if errF != nil {
return testModules, errF
}
nonTestMods = append(nonTestMods, m)
}
existsInBoth := []string{}
for _, a := range nonTestMods {
if slices.Contains(testOnlyMods, a) {
existsInBoth = append(existsInBoth, a)
}
}
testModules = difference(testOnlyMods, existsInBoth)
return dedupe(testModules), nil
}
func getTestModules(root string) ([]string, error) {
allGoFiles := []string{}
nonMainModFileDirs := []string{}
err := filepath.WalkDir(
// note: WalkDir reads an entire directory into memory before proceeding to walk that directory.
// see documentation of filepath.WalkDir
root,
func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
// return on directories since we don't want to parse them
return nil
}
if !d.Type().IsRegular() {
// non regular files. nothing to parse
return nil
}
fName := d.Name()
if filepath.Ext(fName) == ".mod" {
if path != filepath.Join(root, "go.mod") {
nonMainModFileDirs = append(nonMainModFileDirs, filepath.Dir(path))
}
}
if filepath.Ext(fName) != ".go" {
return nil
}
if strings.Contains(path, "vendor/") {
// ignore files inside vendor/ directory
return nil
}
allGoFiles = append(allGoFiles, path)
return nil
},
)
if err != nil {
return []string{}, err
}
filesTobeAnalyzed := fetchToAnalyze(allGoFiles, nonMainModFileDirs)
testImportPaths, nonTestImportPaths, err := getAllImports(filesTobeAnalyzed)
if err != nil {
return []string{}, err
}
trueTestModules, err := getAllTestModules(testImportPaths, nonTestImportPaths, root)
if err != nil {
return []string{}, err
}
return trueTestModules, nil
}
// fetchToAnalyze returns only the list of Go files that need to be analyzed.
// it excludes files that are in directories which are nested go modules.
func fetchToAnalyze(allGoFiles, nonMainModFileDirs []string) []string {
notToBetAnalzyed := []string{}
for _, goFile := range allGoFiles {
for _, mod := range nonMainModFileDirs {
if strings.Contains(goFile, mod) {
// this file should not be analyzed because it belongs
// to a nested module
notToBetAnalzyed = append(notToBetAnalzyed, goFile)
}
}
}
tobeAnalyzed := difference(allGoFiles, notToBetAnalzyed)
return tobeAnalyzed // no need to dedupe. files are unlikely to be duplicates.
}
// getAllImports aggregates all imports from a list of .go files
func getAllImports(files []string) ([]string, []string, error) {
// TODO: turn into
// type importPaths string
// testImportPaths = []importPaths{}
testImportPaths := []string{}
nonTestImportPaths := []string{}
for _, filePath := range files {
impPaths, errF := fetchImports(filePath)
if errF != nil {
return []string{}, []string{}, errF
}
if strings.Contains(filePath, "_test.go") {
// this takes care of both;
// (i) test files
// (ii) example files(https://blog.golang.org/examples)
testImportPaths = append(testImportPaths, impPaths...)
} else {
nonTestImportPaths = append(nonTestImportPaths, impPaths...)
}
}
// dedupe, since one importPath is likely to have been used in multiple Go files.
return dedupe(testImportPaths), dedupe(nonTestImportPaths), nil
}