Source file src/cmd/go/internal/vet/vet.go

     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  	"context"
    10  	"fmt"
    11  	"path/filepath"
    12  
    13  	"cmd/go/internal/base"
    14  	"cmd/go/internal/cfg"
    15  	"cmd/go/internal/load"
    16  	"cmd/go/internal/modload"
    17  	"cmd/go/internal/trace"
    18  	"cmd/go/internal/work"
    19  )
    20  
    21  // Break init loop.
    22  func init() {
    23  	CmdVet.Run = runVet
    24  }
    25  
    26  var CmdVet = &base.Command{
    27  	CustomFlags: true,
    28  	UsageLine:   "go vet [-n] [-x] [-vettool prog] [build flags] [vet flags] [packages]",
    29  	Short:       "report likely mistakes in packages",
    30  	Long: `
    31  Vet runs the Go vet command on the packages named by the import paths.
    32  
    33  For more about vet and its flags, see 'go doc cmd/vet'.
    34  For more about specifying packages, see 'go help packages'.
    35  For a list of checkers and their flags, see 'go tool vet help'.
    36  For details of a specific checker such as 'printf', see 'go tool vet help printf'.
    37  
    38  The -n flag prints commands that would be executed.
    39  The -x flag prints commands as they are executed.
    40  
    41  The -vettool=prog flag selects a different analysis tool with alternative
    42  or additional checks.
    43  For example, the 'shadow' analyzer can be built and run using these commands:
    44  
    45    go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
    46    go vet -vettool=$(which shadow)
    47  
    48  The build flags supported by go vet are those that control package resolution
    49  and execution, such as -n, -x, -v, -tags, and -toolexec.
    50  For more about these flags, see 'go help build'.
    51  
    52  See also: go fmt, go fix.
    53  	`,
    54  }
    55  
    56  func runVet(ctx context.Context, cmd *base.Command, args []string) {
    57  	vetFlags, pkgArgs := vetFlags(args)
    58  	modload.InitWorkfile() // The vet command does custom flag processing; initialize workspaces after that.
    59  
    60  	if cfg.DebugTrace != "" {
    61  		var close func() error
    62  		var err error
    63  		ctx, close, err = trace.Start(ctx, cfg.DebugTrace)
    64  		if err != nil {
    65  			base.Fatalf("failed to start trace: %v", err)
    66  		}
    67  		defer func() {
    68  			if err := close(); err != nil {
    69  				base.Fatalf("failed to stop trace: %v", err)
    70  			}
    71  		}()
    72  	}
    73  
    74  	ctx, span := trace.StartSpan(ctx, fmt.Sprint("Running ", cmd.Name(), " command"))
    75  	defer span.Done()
    76  
    77  	work.BuildInit()
    78  	work.VetFlags = vetFlags
    79  	if len(vetFlags) > 0 {
    80  		work.VetExplicit = true
    81  	}
    82  	if vetTool != "" {
    83  		var err error
    84  		work.VetTool, err = filepath.Abs(vetTool)
    85  		if err != nil {
    86  			base.Fatalf("%v", err)
    87  		}
    88  	}
    89  
    90  	pkgOpts := load.PackageOpts{ModResolveTests: true}
    91  	pkgs := load.PackagesAndErrors(ctx, pkgOpts, pkgArgs)
    92  	load.CheckPackageErrors(pkgs)
    93  	if len(pkgs) == 0 {
    94  		base.Fatalf("no packages to vet")
    95  	}
    96  
    97  	var b work.Builder
    98  	b.Init()
    99  
   100  	root := &work.Action{Mode: "go vet"}
   101  	for _, p := range pkgs {
   102  		_, ptest, pxtest, err := load.TestPackagesFor(ctx, pkgOpts, p, nil)
   103  		if err != nil {
   104  			base.Errorf("%v", err)
   105  			continue
   106  		}
   107  		if len(ptest.GoFiles) == 0 && len(ptest.CgoFiles) == 0 && pxtest == nil {
   108  			base.Errorf("go: can't vet %s: no Go files in %s", p.ImportPath, p.Dir)
   109  			continue
   110  		}
   111  		if len(ptest.GoFiles) > 0 || len(ptest.CgoFiles) > 0 {
   112  			root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, ptest))
   113  		}
   114  		if pxtest != nil {
   115  			root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, pxtest))
   116  		}
   117  	}
   118  	b.Do(ctx, root)
   119  }
   120  

View as plain text