From e8c45e0433280218fb679d7bbad0f80fc9677353 Mon Sep 17 00:00:00 2001 From: "Bryan C. Mills" Date: Wed, 20 Feb 2019 09:37:26 -0500 Subject: [PATCH] go/analysis: allow overriding V flag without code patches In CL 149609, a file was added to src/cmd/vendor/golang.org/x/tools/go/analysis/internal/analysisflags/patch.go to override the behavior of the V flag for cmd/vet. That modification causes the behavior of cmd/vet to change when a pristine copy of x/tools is vendored in, and module-mode vendoring will only support pristine copies (see golang/go#30240). Instead, allow cmd/vet to override the V flag by defining its own V flag before it invokes unitchecker.Main. Tested manually (by patching into cmd/vendor). Updates golang/go#30240 Updates golang/go#30241 Updates golang/go#26924 Updates golang/go#30228 Change-Id: I10e4523e1f4ede94fbfc745012dadeefef48e927 Reviewed-on: https://go-review.googlesource.com/c/162989 Run-TryBot: Bryan C. Mills Reviewed-by: Alan Donovan TryBot-Result: Gobot Gobot --- go/analysis/internal/analysisflags/flags.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/go/analysis/internal/analysisflags/flags.go b/go/analysis/internal/analysisflags/flags.go index 729ac3b4..a03a185f 100644 --- a/go/analysis/internal/analysisflags/flags.go +++ b/go/analysis/internal/analysisflags/flags.go @@ -152,12 +152,13 @@ func printFlags() { // addVersionFlag registers a -V flag that, if set, // prints the executable version and exits 0. // -// It is a variable not a function to permit easy -// overriding in the copy vendored in $GOROOT/src/cmd/vet: -// -// func init() { addVersionFlag = objabi.AddVersionFlag } -var addVersionFlag = func() { - flag.Var(versionFlag{}, "V", "print version and exit") +// If the -V flag already exists — for example, because it was already +// registered by a call to cmd/internal/objabi.AddVersionFlag — then +// addVersionFlag does nothing. +func addVersionFlag() { + if flag.Lookup("V") == nil { + flag.Var(versionFlag{}, "V", "print version and exit") + } } // versionFlag minimally complies with the -V protocol required by "go vet".