Skip to content

Commit a9a08bb

Browse files
committed
gofumpt: Add lang-version option + update linter
Without the Go language version input, `gofumpt` cannot apply some rules which are version dependent. Defaulting `lang-version` to the Go version of the running environment if not specified. Signed-off-by: Kailun Qin <[email protected]>
1 parent ba45043 commit a9a08bb

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

.golangci.example.yml

+4
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ linters-settings:
259259
simplify: true
260260

261261
gofumpt:
262+
# Select the Go language version to target.
263+
# Default is the Go version installed in the running environment.
264+
lang-version: "1.15"
265+
262266
# Choose whether or not to use the extra rules that are disabled
263267
# by default
264268
extra-rules: false

pkg/config/linters_settings.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ var defaultLintersSettings = LintersSettings{
5959
DefaultSignifiesExhaustive: false,
6060
},
6161
Gofumpt: GofumptSettings{
62-
ExtraRules: false,
62+
LangVersion: "",
63+
ExtraRules: false,
6364
},
6465
ErrorLint: ErrorLintSettings{
6566
Errorf: true,
@@ -232,7 +233,8 @@ type GoFmtSettings struct {
232233
}
233234

234235
type GofumptSettings struct {
235-
ExtraRules bool `mapstructure:"extra-rules"`
236+
LangVersion string `mapstructure:"lang-version"`
237+
ExtraRules bool `mapstructure:"extra-rules"`
236238
}
237239

238240
type GoHeaderSettings struct {

pkg/golinters/gofumpt.go

+22-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"io/ioutil"
7+
"os/exec"
78
"sync"
89

910
"github.com/pkg/errors"
@@ -33,6 +34,24 @@ func NewGofumpt() *goanalysis.Linter {
3334
nil,
3435
).WithContextSetter(func(lintCtx *linter.Context) {
3536
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
37+
settings := lintCtx.Settings().Gofumpt
38+
39+
// Get the target Go language version to be used in gofumpt.
40+
// Reuse logic from `https://github.com/mvdan/gofumpt/blob/ba6406b58f3630c5246474b20f6ee5c83d1cc6b9/gofmt.go#L127-L133`.
41+
langVersion := settings.LangVersion
42+
if langVersion == "" {
43+
outGoVersion, errGoVersion := exec.Command("go", "list", "-m", "-f", "{{.GoVersion}}").Output()
44+
outGoVersion = bytes.TrimSpace(outGoVersion)
45+
if errGoVersion == nil && len(outGoVersion) > 0 {
46+
langVersion = string(outGoVersion)
47+
}
48+
}
49+
50+
options := format.Options{
51+
LangVersion: langVersion,
52+
ExtraRules: settings.ExtraRules,
53+
}
54+
3655
var fileNames []string
3756
for _, f := range pass.Files {
3857
pos := pass.Fset.PositionFor(f.Pos(), false)
@@ -46,12 +65,12 @@ func NewGofumpt() *goanalysis.Linter {
4665
if err != nil {
4766
return nil, fmt.Errorf("unable to open file %s: %w", f, err)
4867
}
49-
output, err := format.Source(input, format.Options{
50-
ExtraRules: lintCtx.Settings().Gofumpt.ExtraRules,
51-
})
68+
69+
output, err := format.Source(input, options)
5270
if err != nil {
5371
return nil, fmt.Errorf("error while running gofumpt: %w", err)
5472
}
73+
5574
if !bytes.Equal(input, output) {
5675
out := bytes.Buffer{}
5776
_, err = out.WriteString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", f))

0 commit comments

Comments
 (0)