]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: vet support for upcoming cmd/vet fixes
authorRuss Cox <rsc@golang.org>
Wed, 13 Dec 2017 19:39:40 +0000 (14:39 -0500)
committerRuss Cox <rsc@golang.org>
Thu, 14 Dec 2017 19:55:18 +0000 (19:55 +0000)
Two minor changes to allow fixes in cmd/vet's printf checking.

1. Pass package import path in vet config, so that vet knows
whether it is, for example, vetting "fmt".

2. Add new, but undocumented and for now unsupported
flag -vettool to control which vet binary is invoked during go vet.
This lets the cmd/vet tests build and test a throwaway vet.exe
using cmd/go to ensure type checking information, all without
installing a potentially buggy cmd/vet.

For #22936.

Change-Id: I18df7c796ebc711361c847c63eb3ee17fb041ff7
Reviewed-on: https://go-review.googlesource.com/83837
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/go/internal/vet/vet.go
src/cmd/go/internal/vet/vetflag.go
src/cmd/go/internal/work/exec.go

index db734c9d846d7f726874214da10104bb7c01ae6b..8b4f9264ac3ad062f16a0052c2b16585c44db5eb 100644 (file)
@@ -9,6 +9,7 @@ import (
        "cmd/go/internal/base"
        "cmd/go/internal/load"
        "cmd/go/internal/work"
+       "path/filepath"
 )
 
 var CmdVet = &base.Command{
@@ -38,6 +39,13 @@ func runVet(cmd *base.Command, args []string) {
 
        work.BuildInit()
        work.VetFlags = vetFlags
+       if vetTool != "" {
+               var err error
+               work.VetTool, err = filepath.Abs(vetTool)
+               if err != nil {
+                       base.Fatalf("%v", err)
+               }
+       }
 
        pkgs := load.PackagesForBuild(pkgArgs)
        if len(pkgs) == 0 {
index 36ee04ede77cf2add6e42905959d1f3cdaea6cc9..d4664cc7e9d0a6263002063f692625050feb2931 100644 (file)
@@ -55,10 +55,13 @@ var vetFlagDefn = []*cmdflag.Defn{
        {Name: "unusedstringmethods"},
 }
 
+var vetTool string
+
 // add build flags to vetFlagDefn.
 func init() {
        var cmd base.Command
        work.AddBuildFlags(&cmd)
+       cmd.Flag.StringVar(&vetTool, "vettool", "", "path to vet tool binary") // for cmd/vet tests; undocumented for now
        cmd.Flag.VisitAll(func(f *flag.Flag) {
                vetFlagDefn = append(vetFlagDefn, &cmdflag.Defn{
                        Name:  f.Name,
@@ -87,8 +90,13 @@ func vetFlags(args []string) (passToVet, packageNames []string) {
                        }
                        switch f.Name {
                        // Flags known to the build but not to vet, so must be dropped.
-                       case "x", "n":
-                               args = append(args[:i], args[i+1:]...)
+                       case "x", "n", "vettool":
+                               if extraWord {
+                                       args = append(args[:i], args[i+2:]...)
+                                       extraWord = false
+                               } else {
+                                       args = append(args[:i], args[i+1:]...)
+                               }
                                i--
                        }
                }
index fc4a36ddf4f6afef0ebaa6efd913454342078f4e..60e2a3aa48ae3b07a0f0a03feb18019a4b945206 100644 (file)
@@ -495,6 +495,7 @@ func (b *Builder) build(a *Action) (err error) {
                        Compiler:    cfg.BuildToolchainName,
                        Dir:         a.Package.Dir,
                        GoFiles:     mkAbsFiles(a.Package.Dir, gofiles),
+                       ImportPath:  a.Package.ImportPath,
                        ImportMap:   make(map[string]string),
                        PackageFile: make(map[string]string),
                }
@@ -643,10 +644,15 @@ type vetConfig struct {
        GoFiles     []string
        ImportMap   map[string]string
        PackageFile map[string]string
+       ImportPath  string
 
        SucceedOnTypecheckFailure bool
 }
 
+// VetTool is the path to an alternate vet tool binary.
+// The caller is expected to set it (if needed) before executing any vet actions.
+var VetTool string
+
 // VetFlags are the flags to pass to vet.
 // The caller is expected to set them before executing any vet actions.
 var VetFlags []string
@@ -687,7 +693,11 @@ func (b *Builder) vet(a *Action) error {
        }
 
        p := a.Package
-       return b.run(a, p.Dir, p.ImportPath, nil, cfg.BuildToolexec, base.Tool("vet"), VetFlags, a.Objdir+"vet.cfg")
+       tool := VetTool
+       if tool == "" {
+               tool = base.Tool("vet")
+       }
+       return b.run(a, p.Dir, p.ImportPath, nil, cfg.BuildToolexec, tool, VetFlags, a.Objdir+"vet.cfg")
 }
 
 // linkActionID computes the action ID for a link action.