github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-internal/vet/vet.go (about) 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. 4 5 // Package vet implements the ``go vet'' command. 6 package vet 7 8 import ( 9 "github.com/gagliardetto/golang-go/cmd/go/not-internal/base" 10 "github.com/gagliardetto/golang-go/cmd/go/not-internal/load" 11 "github.com/gagliardetto/golang-go/cmd/go/not-internal/modload" 12 "github.com/gagliardetto/golang-go/cmd/go/not-internal/work" 13 "path/filepath" 14 ) 15 16 var CmdVet = &base.Command{ 17 Run: runVet, 18 CustomFlags: true, 19 UsageLine: "go vet [-n] [-x] [-vettool prog] [build flags] [vet flags] [packages]", 20 Short: "report likely mistakes in packages", 21 Long: ` 22 Vet runs the Go vet command on the packages named by the import paths. 23 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'. 28 29 The -n flag prints commands that would be executed. 30 The -x flag prints commands as they are executed. 31 32 The -vettool=prog flag selects a different analysis tool with alternative 33 or additional checks. 34 For example, the 'shadow' analyzer can be built and run using these commands: 35 36 go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow 37 go vet -vettool=$(which shadow) 38 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'. 42 43 See also: go fmt, go fix. 44 `, 45 } 46 47 func runVet(cmd *base.Command, args []string) { 48 modload.LoadTests = true 49 50 vetFlags, pkgArgs := vetFlags(vetUsage, args) 51 52 work.BuildInit() 53 work.VetFlags = vetFlags 54 if len(vetFlags) > 0 { 55 work.VetExplicit = true 56 } 57 if vetTool != "" { 58 var err error 59 work.VetTool, err = filepath.Abs(vetTool) 60 if err != nil { 61 base.Fatalf("%v", err) 62 } 63 } 64 65 pkgs := load.PackagesForBuild(pkgArgs) 66 if len(pkgs) == 0 { 67 base.Fatalf("no packages to vet") 68 } 69 70 var b work.Builder 71 b.Init() 72 73 root := &work.Action{Mode: "go vet"} 74 for _, p := range pkgs { 75 _, ptest, pxtest, err := load.TestPackagesFor(p, nil) 76 if err != nil { 77 base.Errorf("%v", err) 78 continue 79 } 80 if len(ptest.GoFiles) == 0 && len(ptest.CgoFiles) == 0 && pxtest == nil { 81 base.Errorf("go vet %s: no Go files in %s", p.ImportPath, p.Dir) 82 continue 83 } 84 if len(ptest.GoFiles) > 0 || len(ptest.CgoFiles) > 0 { 85 root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, ptest)) 86 } 87 if pxtest != nil { 88 root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, pxtest)) 89 } 90 } 91 b.Do(root) 92 }