Skip to content

Commit cb08124

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

File tree

5 files changed

+92
-198
lines changed

5 files changed

+92
-198
lines changed

pkg/golinters/gci/gci.go

Lines changed: 88 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,115 @@
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
3153
}
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+
}
3269

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-
},
70+
if settings.LocalPrefixes != "" {
71+
cfg.SectionStrings = []string{
72+
"standard",
73+
"default",
74+
fmt.Sprintf("prefix(%s)", settings.LocalPrefixes),
4375
}
76+
}
77+
78+
parsedCfg, err := cfg.Parse()
79+
if err != nil {
80+
return err
81+
}
4482

45-
if settings.LocalPrefixes != "" {
46-
prefix := []string{
47-
"standard",
48-
"default",
49-
fmt.Sprintf("prefix(%s)", strings.Join(strings.Split(settings.LocalPrefixes, ","), prefixSeparator)),
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)
93+
}
94+
95+
_, output, err := gci.LoadFormat(input, position.Filename, *parsedCfg)
96+
97+
if !bytes.Equal(input, output) {
98+
out := bytes.NewBufferString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", position.Filename))
99+
100+
err := diff.Diff(out, bytes.NewReader(input), bytes.NewReader(output))
101+
if err != nil {
102+
return fmt.Errorf("error while running gci: %w", err)
103+
}
104+
105+
diff := out.String()
106+
107+
err = internal.ExtractDiagnosticFromPatch(pass, file, diff, lintCtx)
108+
if err != nil {
109+
return fmt.Errorf("can't extract issues from gci diff output %q: %w", diff, err)
50110
}
51-
cfg[a.Name][internal.SectionsFlag] = prefix
52111
}
53112
}
54113

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

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)