Skip to content

Commit

Permalink
cmd/go: error if -modfile used with 'go install pkg@version'
Browse files Browse the repository at this point in the history
'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 <[email protected]>
Run-TryBot: Jay Conrod <[email protected]>
Reviewed-by: Bryan C. Mills <[email protected]>
TryBot-Result: Go Bot <[email protected]>
  • Loading branch information
Jay Conrod committed Sep 30, 2020
1 parent 3caaadd commit 8b0d00b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/cmd/go/internal/modload/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions src/cmd/go/testdata/script/mod_install_pkg_version.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8b0d00b

Please sign in to comment.