From: Jay Conrod Date: Tue, 29 Sep 2020 20:40:57 +0000 (-0400) Subject: cmd/go: error if -modfile used with 'go install pkg@version' X-Git-Tag: go1.16beta1~892 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=8b0d00b1645c47076f5b20dc692b2ca6d9bac19b;p=gostls13.git cmd/go: error if -modfile used with 'go install pkg@version' 'go install pkg@version' runs without a main module or a module root directory. The -modfile flag cannot be used to set the module root directory or to substitute a different go.mod file. This error won't be reported if -modfile is set in GOFLAGS. Unsupported flags there are generally ignored. For #40276 Change-Id: I0b39b1fa9184c15c6e863b647d43c328710920f4 Reviewed-on: https://go-review.googlesource.com/c/go/+/258297 Trust: Jay Conrod Run-TryBot: Jay Conrod Reviewed-by: Bryan C. Mills TryBot-Result: Go Bot --- diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index 9d05eadda5..3344242489 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -163,8 +163,9 @@ func Init() { // Running 'go mod init': go.mod will be created in current directory. modRoot = base.Cwd } else if RootMode == NoRoot { - // TODO(jayconrod): report an error if -mod -modfile is explicitly set on - // the command line. Ignore those flags if they come from GOFLAGS. + if cfg.ModFile != "" && !base.InGOFLAGS("-modfile") { + base.Fatalf("go: -modfile cannot be used with commands that ignore the current module") + } modRoot = "" } else { modRoot = findModuleRoot(base.Cwd) diff --git a/src/cmd/go/testdata/script/mod_install_pkg_version.txt b/src/cmd/go/testdata/script/mod_install_pkg_version.txt index 7e6d4e8e7c..dc4a329688 100644 --- a/src/cmd/go/testdata/script/mod_install_pkg_version.txt +++ b/src/cmd/go/testdata/script/mod_install_pkg_version.txt @@ -26,6 +26,17 @@ rm $GOPATH/bin/a cd .. +# 'go install -modfile=x.mod pkg@version' reports an error, but only if +# -modfile is specified explicitly on the command line. +cd m +env GOFLAGS=-modfile=go.mod +go install example.com/cmd/a@latest # same as above +env GOFLAGS= +! go install -modfile=go.mod example.com/cmd/a@latest +stderr '^go: -modfile cannot be used with commands that ignore the current module$' +cd .. + + # Every test case requires linking, so we only cover the most important cases # when -short is set. [short] stop