Skip to content

Commit ffb15ca

Browse files
authored
gci: fix cgo (#5274)
1 parent afa0e27 commit ffb15ca

File tree

7 files changed

+94
-202
lines changed

7 files changed

+94
-202
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ require (
158158
github.com/go-toolsmith/typep v1.1.0 // indirect
159159
github.com/gobwas/glob v0.2.3 // indirect
160160
github.com/golang/protobuf v1.5.3 // indirect
161-
github.com/golangci/modinfo v0.3.3 // indirect
162161
github.com/google/go-cmp v0.6.0 // indirect
163162
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
164163
github.com/gostaticanalysis/comment v1.4.2 // indirect

go.sum

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/gci/gci.go

+90-35
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,116 @@
11
package gci
22

33
import (
4+
"bytes"
45
"fmt"
5-
"strings"
6+
"io"
7+
"os"
68

9+
gcicfg "github.com/daixiang0/gci/pkg/config"
10+
"github.com/daixiang0/gci/pkg/gci"
11+
"github.com/daixiang0/gci/pkg/log"
12+
"github.com/shazow/go-diff/difflib"
713
"golang.org/x/tools/go/analysis"
814

915
"github.com/golangci/golangci-lint/pkg/config"
1016
"github.com/golangci/golangci-lint/pkg/goanalysis"
11-
"github.com/golangci/golangci-lint/pkg/golinters/gci/internal"
17+
"github.com/golangci/golangci-lint/pkg/golinters/internal"
18+
"github.com/golangci/golangci-lint/pkg/lint/linter"
1219
)
1320

1421
const linterName = "gci"
1522

16-
const prefixSeparator = "¤"
23+
type differ interface {
24+
Diff(out io.Writer, a io.ReadSeeker, b io.ReadSeeker) error
25+
}
1726

1827
func New(settings *config.GciSettings) *goanalysis.Linter {
19-
a := internal.NewAnalyzer()
20-
21-
var cfg map[string]map[string]any
22-
if settings != nil {
23-
var sections []string
24-
for _, section := range settings.Sections {
25-
if strings.HasPrefix(section, "prefix(") {
26-
sections = append(sections, strings.ReplaceAll(section, ",", prefixSeparator))
27-
continue
28+
log.InitLogger()
29+
_ = log.L().Sync()
30+
31+
diff := difflib.New()
32+
33+
a := &analysis.Analyzer{
34+
Name: linterName,
35+
Doc: goanalysis.TheOnlyanalyzerDoc,
36+
Run: goanalysis.DummyRun,
37+
}
38+
39+
return goanalysis.NewLinter(
40+
linterName,
41+
a.Doc,
42+
[]*analysis.Analyzer{a},
43+
nil,
44+
).WithContextSetter(func(lintCtx *linter.Context) {
45+
a.Run = func(pass *analysis.Pass) (any, error) {
46+
err := run(lintCtx, pass, settings, diff)
47+
if err != nil {
48+
return nil, err
2849
}
2950

30-
sections = append(sections, section)
51+
return nil, nil
3152
}
53+
}).WithLoadMode(goanalysis.LoadModeSyntax)
54+
}
55+
56+
func run(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GciSettings, diff differ) error {
57+
cfg := gcicfg.YamlConfig{
58+
Cfg: gcicfg.BoolConfig{
59+
NoInlineComments: settings.NoInlineComments,
60+
NoPrefixComments: settings.NoPrefixComments,
61+
SkipGenerated: settings.SkipGenerated,
62+
CustomOrder: settings.CustomOrder,
63+
NoLexOrder: settings.NoLexOrder,
64+
},
65+
SectionStrings: settings.Sections,
66+
ModPath: pass.Module.Path,
67+
}
3268

33-
cfg = map[string]map[string]any{
34-
a.Name: {
35-
internal.NoInlineCommentsFlag: settings.NoInlineComments,
36-
internal.NoPrefixCommentsFlag: settings.NoPrefixComments,
37-
internal.SkipGeneratedFlag: settings.SkipGenerated,
38-
internal.SectionsFlag: sections, // bug because prefix contains comas.
39-
internal.CustomOrderFlag: settings.CustomOrder,
40-
internal.NoLexOrderFlag: settings.NoLexOrder,
41-
internal.PrefixDelimiterFlag: prefixSeparator,
42-
},
69+
if settings.LocalPrefixes != "" {
70+
cfg.SectionStrings = []string{
71+
"standard",
72+
"default",
73+
fmt.Sprintf("prefix(%s)", settings.LocalPrefixes),
4374
}
75+
}
76+
77+
parsedCfg, err := cfg.Parse()
78+
if err != nil {
79+
return err
80+
}
4481

45-
if settings.LocalPrefixes != "" {
46-
prefix := []string{
47-
"standard",
48-
"default",
49-
fmt.Sprintf("prefix(%s)", strings.Join(strings.Split(settings.LocalPrefixes, ","), prefixSeparator)),
82+
for _, file := range pass.Files {
83+
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
84+
if !isGoFile {
85+
continue
86+
}
87+
88+
input, err := os.ReadFile(position.Filename)
89+
if err != nil {
90+
return fmt.Errorf("unable to open file %s: %w", position.Filename, err)
91+
}
92+
93+
_, output, err := gci.LoadFormat(input, position.Filename, *parsedCfg)
94+
if err != nil {
95+
return fmt.Errorf("error while running gci: %w", err)
96+
}
97+
98+
if !bytes.Equal(input, output) {
99+
out := bytes.NewBufferString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", position.Filename))
100+
101+
err := diff.Diff(out, bytes.NewReader(input), bytes.NewReader(output))
102+
if err != nil {
103+
return fmt.Errorf("error while running gci: %w", err)
104+
}
105+
106+
diff := out.String()
107+
108+
err = internal.ExtractDiagnosticFromPatch(pass, file, diff, lintCtx)
109+
if err != nil {
110+
return fmt.Errorf("can't extract issues from gci diff output %q: %w", diff, err)
50111
}
51-
cfg[a.Name][internal.SectionsFlag] = prefix
52112
}
53113
}
54114

55-
return goanalysis.NewLinter(
56-
linterName,
57-
a.Doc,
58-
[]*analysis.Analyzer{a},
59-
cfg,
60-
).WithLoadMode(goanalysis.LoadModeSyntax)
115+
return nil
61116
}

pkg/golinters/gci/internal/analyzer.go

-143
This file was deleted.

pkg/golinters/gci/internal/errors.go

-11
This file was deleted.

pkg/golinters/gci/testdata/gci.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
//golangcitest:config_path testdata/gci.yml
33
package testdata
44

5-
// want +1 "File is not properly formatted"
6-
import (
5+
import ( // want "File is not properly formatted"
76
"golang.org/x/tools/go/analysis"
8-
"github.com/golangci/golangci-lint/pkg/config"
7+
"github.com/golangci/golangci-lint/pkg/config" // want "File is not properly formatted"
98
"fmt"
109
"errors"
1110
gcicfg "github.com/daixiang0/gci/pkg/config"

pkg/golinters/gci/testdata/gci_cgo.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//go:build ignore
2-
3-
// TODO(ldez) the linter doesn't support cgo.
4-
51
//golangcitest:args -Egci
62
//golangcitest:config_path testdata/gci.yml
73
package testdata
@@ -16,10 +12,9 @@ package testdata
1612
*/
1713
import "C"
1814

19-
// want +1 "File is not properly formatted"
20-
import (
15+
import ( // want "File is not properly formatted"
2116
"golang.org/x/tools/go/analysis"
22-
"github.com/golangci/golangci-lint/pkg/config"
17+
"github.com/golangci/golangci-lint/pkg/config" // want "File is not properly formatted"
2318
"unsafe"
2419
"fmt"
2520
"errors"

0 commit comments

Comments
 (0)