summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2018-08-03 11:27:32 -0700
committerIan Lance Taylor <iant@golang.org>2018-08-03 19:51:38 +0000
commit07bcfe574593eea878e187c99e2d7aa17798eef5 (patch)
treedcf0a16f17ef9587c541627efe0055f5920b4580
parentb800f202dc4d4281edd56ec40ea58f09dac8b730 (diff)
downloadgo-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.go2
-rw-r--r--src/cmd/go/testdata/script/mod_gofmt_invalid.txt13
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