| 1 |
// Copyright 2017 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 |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"encoding/json" |
| 10 |
"errors" |
| 11 |
"flag" |
| 12 |
"fmt" |
| 13 |
"log" |
| 14 |
"os" |
| 15 |
"os/exec" |
| 16 |
"path/filepath" |
| 17 |
"strings" |
| 18 |
|
| 19 |
"cmd/go/internal/base" |
| 20 |
"cmd/go/internal/cmdflag" |
| 21 |
"cmd/go/internal/work" |
| 22 |
) |
| 23 |
|
| 24 |
// go vet flag processing |
| 25 |
// |
| 26 |
// We query the flags of the tool specified by -vettool and accept any |
| 27 |
// of those flags plus any flag valid for 'go build'. The tool must |
| 28 |
// support -flags, which prints a description of its flags in JSON to |
| 29 |
// stdout. |
| 30 |
|
| 31 |
// vetTool specifies the vet command to run. |
| 32 |
// Any tool that supports the (still unpublished) vet |
| 33 |
// command-line protocol may be supplied; see |
| 34 |
// golang.org/x/tools/go/analysis/unitchecker for one |
| 35 |
// implementation. It is also used by tests. |
| 36 |
// |
| 37 |
// The default behavior (vetTool=="") runs 'go tool vet'. |
| 38 |
var vetTool string // -vettool |
| 39 |
|
| 40 |
func init() { |
| 41 |
// For now, we omit the -json flag for vet because we could plausibly |
| 42 |
// support -json specific to the vet command in the future (perhaps using |
| 43 |
// the same format as build -json). |
| 44 |
work.AddBuildFlags(CmdVet, work.OmitJSONFlag) |
| 45 |
CmdVet.Flag.StringVar(&vetTool, "vettool", "", "") |
| 46 |
} |
| 47 |
|
| 48 |
func parseVettoolFlag(args []string) { |
| 49 |
// Extract -vettool by ad hoc flag processing: |
| 50 |
// its value is needed even before we can declare |
| 51 |
// the flags available during main flag processing. |
| 52 |
for i, arg := range args { |
| 53 |
if arg == "-vettool" || arg == "--vettool" { |
| 54 |
if i+1 >= len(args) { |
| 55 |
log.Fatalf("%s requires a filename", arg) |
| 56 |
} |
| 57 |
vetTool = args[i+1] |
| 58 |
return |
| 59 |
} else if strings.HasPrefix(arg, "-vettool=") || |
| 60 |
strings.HasPrefix(arg, "--vettool=") { |
| 61 |
vetTool = arg[strings.IndexByte(arg, '=')+1:] |
| 62 |
return |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
// vetFlags processes the command line, splitting it at the first non-flag |
| 68 |
// into the list of flags and list of packages. |
| 69 |
func vetFlags(args []string) (passToVet, packageNames []string) { |
| 70 |
parseVettoolFlag(args) |
| 71 |
|
| 72 |
// Query the vet command for its flags. |
| 73 |
var tool string |
| 74 |
if vetTool == "" { |
| 75 |
tool = base.Tool("vet") |
| 76 |
} else { |
| 77 |
var err error |
| 78 |
tool, err = filepath.Abs(vetTool) |
| 79 |
if err != nil { |
| 80 |
log.Fatal(err) |
| 81 |
} |
| 82 |
} |
| 83 |
out := new(bytes.Buffer) |
| 84 |
vetcmd := exec.Command(tool, "-flags") |
| 85 |
vetcmd.Stdout = out |
| 86 |
if err := vetcmd.Run(); err != nil { |
| 87 |
fmt.Fprintf(os.Stderr, "go: can't execute %s -flags: %v\n", tool, err) |
| 88 |
base.SetExitStatus(2) |
| 89 |
base.Exit() |
| 90 |
} |
| 91 |
var analysisFlags []struct { |
| 92 |
Name string |
| 93 |
Bool bool |
| 94 |
Usage string |
| 95 |
} |
| 96 |
if err := json.Unmarshal(out.Bytes(), &analysisFlags); err != nil { |
| 97 |
fmt.Fprintf(os.Stderr, "go: can't unmarshal JSON from %s -flags: %v", tool, err) |
| 98 |
base.SetExitStatus(2) |
| 99 |
base.Exit() |
| 100 |
} |
| 101 |
|
| 102 |
// Add vet's flags to CmdVet.Flag. |
| 103 |
// |
| 104 |
// Some flags, in particular -tags and -v, are known to vet but |
| 105 |
// also defined as build flags. This works fine, so we omit duplicates here. |
| 106 |
// However some, like -x, are known to the build but not to vet. |
| 107 |
isVetFlag := make(map[string]bool, len(analysisFlags)) |
| 108 |
cf := CmdVet.Flag |
| 109 |
for _, f := range analysisFlags { |
| 110 |
isVetFlag[f.Name] = true |
| 111 |
if cf.Lookup(f.Name) == nil { |
| 112 |
if f.Bool { |
| 113 |
cf.Bool(f.Name, false, "") |
| 114 |
} else { |
| 115 |
cf.String(f.Name, "", "") |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
// Record the set of vet tool flags set by GOFLAGS. We want to pass them to |
| 121 |
// the vet tool, but only if they aren't overridden by an explicit argument. |
| 122 |
base.SetFromGOFLAGS(&CmdVet.Flag) |
| 123 |
addFromGOFLAGS := map[string]bool{} |
| 124 |
CmdVet.Flag.Visit(func(f *flag.Flag) { |
| 125 |
if isVetFlag[f.Name] { |
| 126 |
addFromGOFLAGS[f.Name] = true |
| 127 |
} |
| 128 |
}) |
| 129 |
|
| 130 |
explicitFlags := make([]string, 0, len(args)) |
| 131 |
for len(args) > 0 { |
| 132 |
f, remainingArgs, err := cmdflag.ParseOne(&CmdVet.Flag, args) |
| 133 |
|
| 134 |
if errors.Is(err, flag.ErrHelp) { |
| 135 |
exitWithUsage() |
| 136 |
} |
| 137 |
|
| 138 |
if errors.Is(err, cmdflag.ErrFlagTerminator) { |
| 139 |
// All remaining args must be package names, but the flag terminator is |
| 140 |
// not included. |
| 141 |
packageNames = remainingArgs |
| 142 |
break |
| 143 |
} |
| 144 |
|
| 145 |
if nf := (cmdflag.NonFlagError{}); errors.As(err, &nf) { |
| 146 |
// Everything from here on out — including the argument we just consumed — |
| 147 |
// must be a package name. |
| 148 |
packageNames = args |
| 149 |
break |
| 150 |
} |
| 151 |
|
| 152 |
if err != nil { |
| 153 |
fmt.Fprintln(os.Stderr, err) |
| 154 |
exitWithUsage() |
| 155 |
} |
| 156 |
|
| 157 |
if isVetFlag[f.Name] { |
| 158 |
// Forward the raw arguments rather than cleaned equivalents, just in |
| 159 |
// case the vet tool parses them idiosyncratically. |
| 160 |
explicitFlags = append(explicitFlags, args[:len(args)-len(remainingArgs)]...) |
| 161 |
|
| 162 |
// This flag has been overridden explicitly, so don't forward its implicit |
| 163 |
// value from GOFLAGS. |
| 164 |
delete(addFromGOFLAGS, f.Name) |
| 165 |
} |
| 166 |
|
| 167 |
args = remainingArgs |
| 168 |
} |
| 169 |
|
| 170 |
// Prepend arguments from GOFLAGS before other arguments. |
| 171 |
CmdVet.Flag.Visit(func(f *flag.Flag) { |
| 172 |
if addFromGOFLAGS[f.Name] { |
| 173 |
passToVet = append(passToVet, fmt.Sprintf("-%s=%s", f.Name, f.Value)) |
| 174 |
} |
| 175 |
}) |
| 176 |
passToVet = append(passToVet, explicitFlags...) |
| 177 |
return passToVet, packageNames |
| 178 |
} |
| 179 |
|
| 180 |
func exitWithUsage() { |
| 181 |
fmt.Fprintf(os.Stderr, "usage: %s\n", CmdVet.UsageLine) |
| 182 |
fmt.Fprintf(os.Stderr, "Run 'go help %s' for details.\n", CmdVet.LongName()) |
| 183 |
|
| 184 |
// This part is additional to what (*Command).Usage does: |
| 185 |
cmd := "go tool vet" |
| 186 |
if vetTool != "" { |
| 187 |
cmd = vetTool |
| 188 |
} |
| 189 |
fmt.Fprintf(os.Stderr, "Run '%s help' for a full list of flags and analyzers.\n", cmd) |
| 190 |
fmt.Fprintf(os.Stderr, "Run '%s -help' for an overview.\n", cmd) |
| 191 |
|
| 192 |
base.SetExitStatus(2) |
| 193 |
base.Exit() |
| 194 |
} |