'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>
// 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)
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