diff options
author | Ian Lance Taylor <iant@golang.org> | 2018-08-03 11:27:32 -0700 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2018-08-03 19:51:38 +0000 |
commit | 07bcfe574593eea878e187c99e2d7aa17798eef5 (patch) | |
tree | dcf0a16f17ef9587c541627efe0055f5920b4580 | |
parent | b800f202dc4d4281edd56ec40ea58f09dac8b730 (diff) | |
download | go-git-07bcfe574593eea878e187c99e2d7aa17798eef5.tar.gz |
cmd/go: don't crash in go fmt on invalid input when using modules
Fixes #26792
Change-Id: I9a878180af28e3939b654fd88bed150010dffde0
Reviewed-on: https://go-review.googlesource.com/127856
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
-rw-r--r-- | src/cmd/go/internal/fmtcmd/fmt.go | 2 | ||||
-rw-r--r-- | src/cmd/go/testdata/script/mod_gofmt_invalid.txt | 13 |
2 files changed, 14 insertions, 1 deletions
diff --git a/src/cmd/go/internal/fmtcmd/fmt.go b/src/cmd/go/internal/fmtcmd/fmt.go index 0e5509edfa..8e4ef37281 100644 --- a/src/cmd/go/internal/fmtcmd/fmt.go +++ b/src/cmd/go/internal/fmtcmd/fmt.go @@ -60,7 +60,7 @@ func runFmt(cmd *base.Command, args []string) { }() } for _, pkg := range load.PackagesAndErrors(args) { - if modload.Enabled() && !pkg.Module.Main { + if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main { if !printed { fmt.Fprintf(os.Stderr, "go: not formatting packages in dependency modules\n") printed = true diff --git a/src/cmd/go/testdata/script/mod_gofmt_invalid.txt b/src/cmd/go/testdata/script/mod_gofmt_invalid.txt new file mode 100644 index 0000000000..21edc7dc2f --- /dev/null +++ b/src/cmd/go/testdata/script/mod_gofmt_invalid.txt @@ -0,0 +1,13 @@ +# Test for a crash in go fmt on invalid input when using modules. +# Issue 26792. + +env GO111MODULE=on +! go fmt x.go +! stderr panic + +-- go.mod -- +module x + +-- x.go -- +// Missing package declaration. +var V int |