Skip to content

Commit 9006d1f

Browse files
committed
cmd/go: vet support for upcoming cmd/vet fixes
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent c0cda71 commit 9006d1f

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

src/cmd/go/internal/vet/vet.go

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"cmd/go/internal/base"
1010
"cmd/go/internal/load"
1111
"cmd/go/internal/work"
12+
"path/filepath"
1213
)
1314

1415
var CmdVet = &base.Command{
@@ -38,6 +39,13 @@ func runVet(cmd *base.Command, args []string) {
3839

3940
work.BuildInit()
4041
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+
}
4149

4250
pkgs := load.PackagesForBuild(pkgArgs)
4351
if len(pkgs) == 0 {

src/cmd/go/internal/vet/vetflag.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ var vetFlagDefn = []*cmdflag.Defn{
5555
{Name: "unusedstringmethods"},
5656
}
5757

58+
var vetTool string
59+
5860
// add build flags to vetFlagDefn.
5961
func init() {
6062
var cmd base.Command
6163
work.AddBuildFlags(&cmd)
64+
cmd.Flag.StringVar(&vetTool, "vettool", "", "path to vet tool binary") // for cmd/vet tests; undocumented for now
6265
cmd.Flag.VisitAll(func(f *flag.Flag) {
6366
vetFlagDefn = append(vetFlagDefn, &cmdflag.Defn{
6467
Name: f.Name,
@@ -87,8 +90,13 @@ func vetFlags(args []string) (passToVet, packageNames []string) {
8790
}
8891
switch f.Name {
8992
// Flags known to the build but not to vet, so must be dropped.
90-
case "x", "n":
91-
args = append(args[:i], args[i+1:]...)
93+
case "x", "n", "vettool":
94+
if extraWord {
95+
args = append(args[:i], args[i+2:]...)
96+
extraWord = false
97+
} else {
98+
args = append(args[:i], args[i+1:]...)
99+
}
92100
i--
93101
}
94102
}

src/cmd/go/internal/work/exec.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ func (b *Builder) build(a *Action) (err error) {
495495
Compiler: cfg.BuildToolchainName,
496496
Dir: a.Package.Dir,
497497
GoFiles: mkAbsFiles(a.Package.Dir, gofiles),
498+
ImportPath: a.Package.ImportPath,
498499
ImportMap: make(map[string]string),
499500
PackageFile: make(map[string]string),
500501
}
@@ -643,10 +644,15 @@ type vetConfig struct {
643644
GoFiles []string
644645
ImportMap map[string]string
645646
PackageFile map[string]string
647+
ImportPath string
646648

647649
SucceedOnTypecheckFailure bool
648650
}
649651

652+
// VetTool is the path to an alternate vet tool binary.
653+
// The caller is expected to set it (if needed) before executing any vet actions.
654+
var VetTool string
655+
650656
// VetFlags are the flags to pass to vet.
651657
// The caller is expected to set them before executing any vet actions.
652658
var VetFlags []string
@@ -687,7 +693,11 @@ func (b *Builder) vet(a *Action) error {
687693
}
688694

689695
p := a.Package
690-
return b.run(a, p.Dir, p.ImportPath, nil, cfg.BuildToolexec, base.Tool("vet"), VetFlags, a.Objdir+"vet.cfg")
696+
tool := VetTool
697+
if tool == "" {
698+
tool = base.Tool("vet")
699+
}
700+
return b.run(a, p.Dir, p.ImportPath, nil, cfg.BuildToolexec, tool, VetFlags, a.Objdir+"vet.cfg")
691701
}
692702

693703
// linkActionID computes the action ID for a link action.

0 commit comments

Comments
 (0)