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>
This commit is contained in:
Ian Lance Taylor 2018-08-03 11:27:32 -07:00
parent b800f202dc
commit 07bcfe5745
2 changed files with 14 additions and 1 deletions

View file

@ -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

View file

@ -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