Skip to content

Commit 3b99647

Browse files
authored
assume go1.16 when a go.mod file is missing a go directive
Otherwise we would panic: panic: invalid Go version: "go" Fixes #317.
1 parent 8222158 commit 3b99647

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

gofmt.go

+5
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ func processFile(filename string, info fs.FileInfo, in io.Reader, r *reporter, e
297297
mod, ok := moduleCacheByDir.Load(dir)
298298
if ok && mod != nil {
299299
mod := mod.(*module)
300+
if mod.Go == "" {
301+
// If the go directive is missing, go 1.16 is assumed.
302+
// https://go.dev/ref/mod#go-mod-file-go
303+
mod.Go = "1.16"
304+
}
300305
if lang == "" {
301306
lang = "go" + mod.Go
302307
}

testdata/script/gomod.txtar

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ stdout '//gofumpt:diagnose.* -lang=go1.21'
66
exec gofumpt toolchain-unstable/a.go
77
stdout '//gofumpt:diagnose.* -lang=go1.21'
88

9+
exec gofumpt missing-go-directive/a.go
10+
stdout '//gofumpt:diagnose.* -lang=go1.16'
11+
912
-- toolchain-stable/go.mod --
1013
module a
1114

@@ -23,3 +26,11 @@ go 1.21rc3
2326
package a
2427

2528
//gofumpt:diagnose
29+
30+
-- missing-go-directive/go.mod --
31+
module a
32+
33+
-- missing-go-directive/a.go --
34+
package a
35+
36+
//gofumpt:diagnose

0 commit comments

Comments
 (0)