@@ -41,12 +41,15 @@ import (
41
41
)
42
42
43
43
var (
44
- verbose = flag .Bool ("verbose" , false , "print more information" )
45
- cross = flag .Bool ("cross" , true , "build for all platforms" )
46
- platforms = flag .String ("platform" , "" , "comma-separated list of platforms to typecheck" )
47
- timings = flag .Bool ("time" , false , "output times taken for each phase" )
48
- defuses = flag .Bool ("defuse" , false , "output defs/uses" )
49
- serial = flag .Bool ("serial" , false , "don't type check platforms in parallel" )
44
+ verbose = flag .Bool ("verbose" , false , "print more information" )
45
+ cross = flag .Bool ("cross" , true , "build for all platforms" )
46
+ platforms = flag .String ("platform" , "" , "comma-separated list of platforms to typecheck" )
47
+ timings = flag .Bool ("time" , false , "output times taken for each phase" )
48
+ defuses = flag .Bool ("defuse" , false , "output defs/uses" )
49
+ serial = flag .Bool ("serial" , false , "don't type check platforms in parallel" )
50
+ skipTest = flag .Bool ("skip-test" , false , "don't type check test code" )
51
+ tags = flag .String ("tags" , "" , "comma-separated list of build tags to apply in addition to go's defaults" )
52
+ ignoreDirs = flag .String ("ignore-dirs" , "" , "comma-separated list of directories to ignore in addition to the default hardcoded list including staging, vendor, and hidden dirs" )
50
53
51
54
isTerminal = terminal .IsTerminal (int (os .Stdout .Fd ()))
52
55
logPrefix = ""
62
65
}
63
66
darwinPlatString = "darwin/386,darwin/amd64"
64
67
windowsPlatString = "windows/386,windows/amd64"
68
+
69
+ // directories we always ignore
70
+ standardIgnoreDirs = []string {
71
+ // Staging code is symlinked from vendor/k8s.io, and uses import
72
+ // paths as if it were inside of vendor/. It fails typechecking
73
+ // inside of staging/, but works when typechecked as part of vendor/.
74
+ "staging" ,
75
+ // OS-specific vendor code tends to be imported by OS-specific
76
+ // packages. We recursively typecheck imported vendored packages for
77
+ // each OS, but don't typecheck everything for every OS.
78
+ "vendor" ,
79
+ "_output" ,
80
+ // This is a weird one. /testdata/ is *mostly* ignored by Go,
81
+ // and this translates to kubernetes/vendor not working.
82
+ // edit/record.go doesn't compile without gopkg.in/yaml.v2
83
+ // in $GOSRC/$GOROOT (both typecheck and the shell script).
84
+ "pkg/kubectl/cmd/testdata/edit" ,
85
+ }
65
86
)
66
87
67
88
type analyzer struct {
@@ -79,6 +100,10 @@ func newAnalyzer(platform string) *analyzer {
79
100
platSplit := strings .Split (platform , "/" )
80
101
ctx .GOOS , ctx .GOARCH = platSplit [0 ], platSplit [1 ]
81
102
ctx .CgoEnabled = true
103
+ if * tags != "" {
104
+ tagsSplit := strings .Split (* tags , "," )
105
+ ctx .BuildTags = append (ctx .BuildTags , tagsSplit ... )
106
+ }
82
107
83
108
a := & analyzer {
84
109
platform : platform ,
@@ -158,6 +183,9 @@ func (a *analyzer) filterFiles(fs map[string]*ast.File) []*ast.File {
158
183
files := []* ast.File {}
159
184
for _ , f := range fs {
160
185
fpath := a .fset .File (f .Pos ()).Name ()
186
+ if * skipTest && strings .HasSuffix (fpath , "_test.go" ) {
187
+ continue
188
+ }
161
189
dir , name := filepath .Split (fpath )
162
190
matches , err := a .ctx .MatchFile (dir , name )
163
191
if err != nil {
@@ -219,7 +247,8 @@ func (a *analyzer) typeCheck(dir string, files []*ast.File) error {
219
247
}
220
248
221
249
type collector struct {
222
- dirs []string
250
+ dirs []string
251
+ ignoreDirs []string
223
252
}
224
253
225
254
// handlePath walks the filesystem recursively, collecting directories,
@@ -231,23 +260,14 @@ func (c *collector) handlePath(path string, info os.FileInfo, err error) error {
231
260
}
232
261
if info .IsDir () {
233
262
// Ignore hidden directories (.git, .cache, etc)
234
- if len (path ) > 1 && path [0 ] == '.' ||
235
- // Staging code is symlinked from vendor/k8s.io, and uses import
236
- // paths as if it were inside of vendor/. It fails typechecking
237
- // inside of staging/, but works when typechecked as part of vendor/.
238
- path == "staging" ||
239
- // OS-specific vendor code tends to be imported by OS-specific
240
- // packages. We recursively typecheck imported vendored packages for
241
- // each OS, but don't typecheck everything for every OS.
242
- path == "vendor" ||
243
- path == "_output" ||
244
- // This is a weird one. /testdata/ is *mostly* ignored by Go,
245
- // and this translates to kubernetes/vendor not working.
246
- // edit/record.go doesn't compile without gopkg.in/yaml.v2
247
- // in $GOSRC/$GOROOT (both typecheck and the shell script).
248
- path == "pkg/kubectl/cmd/testdata/edit" {
263
+ if len (path ) > 1 && path [0 ] == '.' {
249
264
return filepath .SkipDir
250
265
}
266
+ for _ , dir := range c .ignoreDirs {
267
+ if path == dir {
268
+ return filepath .SkipDir
269
+ }
270
+ }
251
271
c .dirs = append (c .dirs , path )
252
272
}
253
273
return nil
@@ -310,7 +330,13 @@ func main() {
310
330
args = append (args , "." )
311
331
}
312
332
313
- c := collector {}
333
+ c := collector {
334
+ ignoreDirs : append ([]string (nil ), standardIgnoreDirs ... ),
335
+ }
336
+ if * ignoreDirs != "" {
337
+ c .ignoreDirs = append (c .ignoreDirs , strings .Split (* ignoreDirs , "," )... )
338
+ }
339
+
314
340
for _ , arg := range args {
315
341
err := filepath .Walk (arg , c .handlePath )
316
342
if err != nil {
0 commit comments