Commit 89df0354 authored by Jay Conrod's avatar Jay Conrod

cmd/go: fix errors for commands run outside of modules

Since CL 148517, several commands (including list and get) work when
GO111MODULE=on even when no go.mod file is present. This broke an
assumption made by "fix" and "generate" which caused panics when run
with a list of .go files (whether or not the command was run inside a
module).

This change fixes those assumptions and adds test cases for other
commands run outside modules.

Fixes #29097

Change-Id: I7927559769c5d4617d73eb63f3b17e2f26d8c219
Reviewed-on: https://go-review.googlesource.com/c/153158Reviewed-by: default avatarBryan C. Mills <bcmills@google.com>
parent 6182d082
...@@ -34,7 +34,7 @@ See also: go fmt, go vet. ...@@ -34,7 +34,7 @@ See also: go fmt, go vet.
func runFix(cmd *base.Command, args []string) { func runFix(cmd *base.Command, args []string) {
printed := false printed := false
for _, pkg := range load.Packages(args) { for _, pkg := range load.Packages(args) {
if modload.Enabled() && !pkg.Module.Main { if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
if !printed { if !printed {
fmt.Fprintf(os.Stderr, "go: not fixing packages in dependency modules\n") fmt.Fprintf(os.Stderr, "go: not fixing packages in dependency modules\n")
printed = true printed = true
......
...@@ -161,7 +161,7 @@ func runGenerate(cmd *base.Command, args []string) { ...@@ -161,7 +161,7 @@ func runGenerate(cmd *base.Command, args []string) {
// Even if the arguments are .go files, this loop suffices. // Even if the arguments are .go files, this loop suffices.
printed := false printed := false
for _, pkg := range load.Packages(args) { for _, pkg := range load.Packages(args) {
if modload.Enabled() && !pkg.Module.Main { if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
if !printed { if !printed {
fmt.Fprintf(os.Stderr, "go: not generating in packages in dependency modules\n") fmt.Fprintf(os.Stderr, "go: not generating in packages in dependency modules\n")
printed = true printed = true
......
...@@ -36,6 +36,9 @@ stdout '^cmd/go$' ...@@ -36,6 +36,9 @@ stdout '^cmd/go$'
go list $GOROOT/src/fmt go list $GOROOT/src/fmt
stdout '^fmt$' stdout '^fmt$'
# 'go list' should work with file arguments.
go list ./foo/foo.go
stdout 'command-line-arguments'
# 'go list -m' with an explicit version should resolve that version. # 'go list -m' with an explicit version should resolve that version.
go list -m example.com/version@latest go list -m example.com/version@latest
...@@ -186,11 +189,27 @@ stdout 'path is \.$' ...@@ -186,11 +189,27 @@ stdout 'path is \.$'
stdout 'main is main \(devel\)' stdout 'main is main \(devel\)'
stdout 'using example.com/version v1.1.0' stdout 'using example.com/version v1.1.0'
# 'go generate' should work with file arguments.
[exec:touch] go generate ./foo/foo.go
[exec:touch] exists ./foo/gen.txt
# 'go install' should work with file arguments.
go install ./foo/foo.go
# 'go test' should work with file arguments.
go test -v ./foo/foo_test.go
stdout 'foo was tested'
# 'go vet' should work with file arguments.
go vet ./foo/foo.go
-- README.txt -- -- README.txt --
There is no go.mod file in the working directory. There is no go.mod file in the working directory.
-- foo/foo.go -- -- foo/foo.go --
//go:generate touch gen.txt
package main package main
import ( import (
...@@ -212,3 +231,15 @@ func main() { ...@@ -212,3 +231,15 @@ func main() {
fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version) fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
} }
} }
-- foo/foo_test.go --
package main
import (
"fmt"
"testing"
)
func TestFoo(t *testing.T) {
fmt.Println("foo was tested")
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment