Skip to content

Commit

Permalink
assume go1.16 when a go.mod file is missing a go directive
Browse files Browse the repository at this point in the history
Otherwise we would panic:

    panic: invalid Go version: "go"

Fixes #317.
  • Loading branch information
alexandear authored Jan 2, 2025
1 parent 8222158 commit 3b99647
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ func processFile(filename string, info fs.FileInfo, in io.Reader, r *reporter, e
mod, ok := moduleCacheByDir.Load(dir)
if ok && mod != nil {
mod := mod.(*module)
if mod.Go == "" {
// If the go directive is missing, go 1.16 is assumed.
// https://go.dev/ref/mod#go-mod-file-go
mod.Go = "1.16"
}
if lang == "" {
lang = "go" + mod.Go
}
Expand Down
11 changes: 11 additions & 0 deletions testdata/script/gomod.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ stdout '//gofumpt:diagnose.* -lang=go1.21'
exec gofumpt toolchain-unstable/a.go
stdout '//gofumpt:diagnose.* -lang=go1.21'

exec gofumpt missing-go-directive/a.go
stdout '//gofumpt:diagnose.* -lang=go1.16'

-- toolchain-stable/go.mod --
module a

Expand All @@ -23,3 +26,11 @@ go 1.21rc3
package a

//gofumpt:diagnose

-- missing-go-directive/go.mod --
module a

-- missing-go-directive/a.go --
package a

//gofumpt:diagnose

0 comments on commit 3b99647

Please sign in to comment.