]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: vet: revert $GOVETTOOL env var, restore -vettool flag
authorAlan Donovan <adonovan@google.com>
Thu, 8 Nov 2018 15:43:40 +0000 (10:43 -0500)
committerAlan Donovan <adonovan@google.com>
Thu, 8 Nov 2018 17:41:25 +0000 (17:41 +0000)
The environment variable is no longer necessary as we now plan to
transition to the new vet by replacing it in a single step,
and we really don't want to add more environment variables.

Fixes #28636

Change-Id: Ib85e5c0d61213b7b9f6a53d9376fec29525df971
Reviewed-on: https://go-review.googlesource.com/c/148497
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/vet/vetflag.go
src/cmd/go/internal/work/buildid.go
src/cmd/vet/vet_test.go

index 22bce16cf3429f2f5e1a59264b5a3ca8a9038db4..9b5184a4d4ef89a0bbfac15bc4285dedc541ea3b 100644 (file)
@@ -23,20 +23,39 @@ import (
 
 // go vet flag processing
 //
-// We query the flags of the tool specified by GOVETTOOL (default:
-// cmd/vet) and accept any of those flags plus any flag valid for 'go
-// build'. The tool must support -flags, which prints a description of
-// its flags in JSON to stdout.
+// We query the flags of the tool specified by -vettool and accept any
+// of those flags plus any flag valid for 'go build'. The tool must
+// support -flags, which prints a description of its flags in JSON to
+// stdout.
 
-// GOVETTOOL specifies the vet command to run.
-// This must be an environment variable because
-// we need it before flag processing, as we execute
-// $GOVETTOOL to discover the set of flags it supports.
+// vetTool specifies the vet command to run.
+// Any tool that supports the (still unpublished) vet
+// command-line protocol may be supplied; see
+// golang.org/x/tools/go/analysis/unitchecker for one
+// implementation. It is also used by tests.
 //
-// Using an environment variable also makes it easy for users to opt in
-// to (and later, opt out of) the new cmd/vet analysis driver during the
-// transition. It is also used by tests.
-var vetTool = os.Getenv("GOVETTOOL")
+// The default behavior (vetTool=="") runs 'go tool vet'.
+//
+var vetTool string // -vettool
+
+func init() {
+       // Extract -vettool by ad hoc flag processing:
+       // its value is needed even before we can declare
+       // the flags available during main flag processing.
+       for i, arg := range os.Args {
+               if arg == "-vettool" || arg == "--vettool" {
+                       if i+1 >= len(os.Args) {
+                               log.Fatalf("%s requires a filename", arg)
+                       }
+                       vetTool = os.Args[i+1]
+                       break
+               } else if strings.HasPrefix(arg, "-vettool=") ||
+                       strings.HasPrefix(arg, "--vettool=") {
+                       vetTool = arg[strings.IndexByte(arg, '=')+1:]
+                       break
+               }
+       }
+}
 
 // vetFlags processes the command line, splitting it at the first non-flag
 // into the list of flags and list of packages.
@@ -94,6 +113,9 @@ func vetFlags(usage func(), args []string) (passToVet, packageNames []string) {
        // Add build flags to vetFlagDefn.
        var cmd base.Command
        work.AddBuildFlags(&cmd)
+       // This flag declaration is a placeholder:
+       // -vettool is actually parsed by the init function above.
+       cmd.Flag.StringVar(new(string), "vettool", "", "path to vet tool binary")
        cmd.Flag.VisitAll(func(f *flag.Flag) {
                vetFlagDefn = append(vetFlagDefn, &cmdflag.Defn{
                        Name:  f.Name,
index a6cfb50558ba53ee15b5f5e632b02bbe971eccb4..c5aa1db50b8d6077c7d31158f4c8551681f291f1 100644 (file)
@@ -178,7 +178,8 @@ func (b *Builder) toolID(name string) string {
        path := base.Tool(name)
        desc := "go tool " + name
 
-       // Special case: undocumented $GOVETTOOL overrides usual vet, for testing vet.
+       // Special case: undocumented -vettool overrides usual vet,
+       // for testing vet or supplying an alternative analysis tool.
        if name == "vet" && VetTool != "" {
                path = VetTool
                desc = VetTool
index da5a6ed87c7034440a0c81359941f0578c228271..6b2125924d7a8ae34074f0379fa9d88bf4e03adf 100644 (file)
@@ -118,12 +118,11 @@ func TestVetPrint(t *testing.T) {
        Build(t)
        file := filepath.Join("testdata", "print.go")
        cmd := exec.Command(
-               "go", "vet",
+               "go", "vet", "-vettool="+binary,
                "-printf",
                "-printfuncs=Warn:1,Warnf:1",
                file,
        )
-       cmd.Env = append(os.Environ(), "GOVETTOOL="+binary)
        errchk(cmd, []string{file}, t)
 }