Home | History | Annotate | Download | only in vet
      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 	"cmd/go/internal/base"
     10 	"cmd/go/internal/load"
     11 	"cmd/go/internal/work"
     12 	"path/filepath"
     13 )
     14 
     15 var CmdVet = &base.Command{
     16 	Run:         runVet,
     17 	CustomFlags: true,
     18 	UsageLine:   "vet [-n] [-x] [build flags] [vet flags] [packages]",
     19 	Short:       "report likely mistakes in packages",
     20 	Long: `
     21 Vet runs the Go vet command on the packages named by the import paths.
     22 
     23 For more about vet and its flags, see 'go doc cmd/vet'.
     24 For more about specifying packages, see 'go help packages'.
     25 
     26 The -n flag prints commands that would be executed.
     27 The -x flag prints commands as they are executed.
     28 
     29 The build flags supported by go vet are those that control package resolution
     30 and execution, such as -n, -x, -v, -tags, and -toolexec.
     31 For more about these flags, see 'go help build'.
     32 
     33 See also: go fmt, go fix.
     34 	`,
     35 }
     36 
     37 func runVet(cmd *base.Command, args []string) {
     38 	vetFlags, pkgArgs := vetFlags(args)
     39 
     40 	work.BuildInit()
     41 	work.VetFlags = vetFlags
     42 	if vetTool != "" {
     43 		var err error
     44 		work.VetTool, err = filepath.Abs(vetTool)
     45 		if err != nil {
     46 			base.Fatalf("%v", err)
     47 		}
     48 	}
     49 
     50 	pkgs := load.PackagesForBuild(pkgArgs)
     51 	if len(pkgs) == 0 {
     52 		base.Fatalf("no packages to vet")
     53 	}
     54 
     55 	var b work.Builder
     56 	b.Init()
     57 
     58 	root := &work.Action{Mode: "go vet"}
     59 	for _, p := range pkgs {
     60 		ptest, pxtest, err := load.TestPackagesFor(p, false)
     61 		if err != nil {
     62 			base.Errorf("%v", err)
     63 			continue
     64 		}
     65 		if len(ptest.GoFiles) == 0 && pxtest == nil {
     66 			base.Errorf("go vet %s: no Go files in %s", p.ImportPath, p.Dir)
     67 			continue
     68 		}
     69 		if len(ptest.GoFiles) > 0 {
     70 			root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, ptest))
     71 		}
     72 		if pxtest != nil {
     73 			root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, pxtest))
     74 		}
     75 	}
     76 	b.Do(root)
     77 }
     78