1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package vet implements the ``go vet'' command.
10 "cmd/go/internal/load"
11 "cmd/go/internal/modload"
12 "cmd/go/internal/work"
16 var CmdVet = &base.Command{
19 UsageLine: "go vet [-n] [-x] [-vettool prog] [build flags] [vet flags] [packages]",
20 Short: "report likely mistakes in packages",
22 Vet runs the Go vet command on the packages named by the import paths.
24 For more about vet and its flags, see 'go doc cmd/vet'.
25 For more about specifying packages, see 'go help packages'.
26 For a list of checkers and their flags, see 'go tool vet help'.
27 For details of a specific checker such as 'printf', see 'go tool vet help printf'.
29 The -n flag prints commands that would be executed.
30 The -x flag prints commands as they are executed.
32 The -vettool=prog flag selects a different analysis tool with alternative
34 For example, the 'shadow' analyzer can be built and run using these commands:
36 go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
37 go vet -vettool=$(which shadow)
39 The build flags supported by go vet are those that control package resolution
40 and execution, such as -n, -x, -v, -tags, and -toolexec.
41 For more about these flags, see 'go help build'.
43 See also: go fmt, go fix.
47 func runVet(cmd *base.Command, args []string) {
48 modload.LoadTests = true
50 vetFlags, pkgArgs := vetFlags(vetUsage, args)
53 work.VetFlags = vetFlags
54 work.VetExplicit = true
57 work.VetTool, err = filepath.Abs(vetTool)
59 base.Fatalf("%v", err)
63 pkgs := load.PackagesForBuild(pkgArgs)
65 base.Fatalf("no packages to vet")
71 root := &work.Action{Mode: "go vet"}
72 for _, p := range pkgs {
73 _, ptest, pxtest, err := load.TestPackagesFor(p, nil)
75 base.Errorf("%v", err)
78 if len(ptest.GoFiles) == 0 && len(ptest.CgoFiles) == 0 && pxtest == nil {
79 base.Errorf("go vet %s: no Go files in %s", p.ImportPath, p.Dir)
82 if len(ptest.GoFiles) > 0 || len(ptest.CgoFiles) > 0 {
83 root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, ptest))
86 root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, pxtest))