Skip to content

Commit e927418

Browse files
committed
gci: fix cgo
1 parent 62d7ebf commit e927418

File tree

7 files changed

+95
-201
lines changed

7 files changed

+95
-201
lines changed

go.mod

Lines changed: 0 additions & 1 deletion
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

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/gci/gci.go

Lines changed: 91 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,118 @@
11
package gci
22

33
import (
4+
"bytes"
45
"fmt"
6+
"io"
7+
"os"
58
"strings"
69

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

916
"github.com/golangci/golangci-lint/pkg/config"
1017
"github.com/golangci/golangci-lint/pkg/goanalysis"
11-
"github.com/golangci/golangci-lint/pkg/golinters/gci/internal"
18+
"github.com/golangci/golangci-lint/pkg/golinters/internal"
19+
"github.com/golangci/golangci-lint/pkg/lint/linter"
1220
)
1321

1422
const linterName = "gci"
1523

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

1828
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
29+
log.InitLogger()
30+
_ = log.L().Sync()
31+
32+
diff := difflib.New()
33+
34+
a := &analysis.Analyzer{
35+
Name: linterName,
36+
Doc: goanalysis.TheOnlyanalyzerDoc,
37+
Run: goanalysis.DummyRun,
38+
}
39+
40+
return goanalysis.NewLinter(
41+
linterName,
42+
a.Doc,
43+
[]*analysis.Analyzer{a},
44+
nil,
45+
).WithContextSetter(func(lintCtx *linter.Context) {
46+
a.Run = func(pass *analysis.Pass) (any, error) {
47+
err := run(lintCtx, pass, settings, diff)
48+
if err != nil {
49+
return nil, err
2850
}
2951

30-
sections = append(sections, section)
52+
return nil, nil
53+
}
54+
}).WithLoadMode(goanalysis.LoadModeSyntax)
55+
}
56+
57+
func run(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GciSettings, diff differ) error {
58+
cfg := gcicfg.YamlConfig{
59+
Cfg: gcicfg.BoolConfig{
60+
NoInlineComments: settings.NoInlineComments,
61+
NoPrefixComments: settings.NoPrefixComments,
62+
SkipGenerated: settings.SkipGenerated,
63+
CustomOrder: settings.CustomOrder,
64+
NoLexOrder: settings.NoLexOrder,
65+
},
66+
SectionStrings: settings.Sections,
67+
ModPath: pass.Module.Path,
68+
}
69+
70+
if settings.LocalPrefixes != "" {
71+
cfg.SectionStrings = []string{
72+
"standard",
73+
"default",
74+
fmt.Sprintf("prefix(%s)", settings.LocalPrefixes),
75+
}
76+
}
77+
78+
parsedCfg, err := cfg.Parse()
79+
if err != nil {
80+
return err
81+
}
82+
83+
for _, file := range pass.Files {
84+
position := goanalysis.GetFilePosition(pass, file)
85+
86+
if !strings.HasSuffix(position.Filename, ".go") {
87+
continue
88+
}
89+
90+
input, err := os.ReadFile(position.Filename)
91+
if err != nil {
92+
return fmt.Errorf("unable to open file %s: %w", position.Filename, err)
3193
}
3294

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-
},
95+
_, output, err := gci.LoadFormat(input, position.Filename, *parsedCfg)
96+
if err != nil {
97+
return fmt.Errorf("error while running gci: %w", err)
4398
}
4499

45-
if settings.LocalPrefixes != "" {
46-
prefix := []string{
47-
"standard",
48-
"default",
49-
fmt.Sprintf("prefix(%s)", strings.Join(strings.Split(settings.LocalPrefixes, ","), prefixSeparator)),
100+
if !bytes.Equal(input, output) {
101+
out := bytes.NewBufferString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", position.Filename))
102+
103+
err := diff.Diff(out, bytes.NewReader(input), bytes.NewReader(output))
104+
if err != nil {
105+
return fmt.Errorf("error while running gci: %w", err)
106+
}
107+
108+
diff := out.String()
109+
110+
err = internal.ExtractDiagnosticFromPatch(pass, file, diff, lintCtx)
111+
if err != nil {
112+
return fmt.Errorf("can't extract issues from gci diff output %q: %w", diff, err)
50113
}
51-
cfg[a.Name][internal.SectionsFlag] = prefix
52114
}
53115
}
54116

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

pkg/golinters/gci/internal/analyzer.go

Lines changed: 0 additions & 143 deletions
This file was deleted.

pkg/golinters/gci/internal/errors.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

pkg/golinters/gci/testdata/gci.go

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 7 deletions
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)