Skip to content

Commit 07bcfe5

Browse files
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 <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent b800f20 commit 07bcfe5

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/cmd/go/internal/fmtcmd/fmt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func runFmt(cmd *base.Command, args []string) {
6060
}()
6161
}
6262
for _, pkg := range load.PackagesAndErrors(args) {
63-
if modload.Enabled() && !pkg.Module.Main {
63+
if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
6464
if !printed {
6565
fmt.Fprintf(os.Stderr, "go: not formatting packages in dependency modules\n")
6666
printed = true
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Test for a crash in go fmt on invalid input when using modules.
2+
# Issue 26792.
3+
4+
env GO111MODULE=on
5+
! go fmt x.go
6+
! stderr panic
7+
8+
-- go.mod --
9+
module x
10+
11+
-- x.go --
12+
// Missing package declaration.
13+
var V int

0 commit comments

Comments
 (0)