]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: error if -modfile used with 'go install pkg@version'
authorJay Conrod <jayconrod@google.com>
Tue, 29 Sep 2020 20:40:57 +0000 (16:40 -0400)
committerJay Conrod <jayconrod@google.com>
Wed, 30 Sep 2020 13:18:19 +0000 (13:18 +0000)
'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 <jayconrod@google.com>
Run-TryBot: Jay Conrod <jayconrod@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Go Bot <gobot@golang.org>

src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/mod_install_pkg_version.txt

index 9d05eadda5560d2f154802b58e9d0503ec4f61b7..33442424891dfa0146ff2d3a5ee94fb8a9e73111 100644 (file)
@@ -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)
index 7e6d4e8e7c30b536ec3a2aac52aa6646e9ffebad..dc4a329688c126d8d7dc9644c7b412b7603ff704 100644 (file)
@@ -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