Skip to content

Commit

Permalink
gci: fix cgo (#5274)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Jan 1, 2025
1 parent afa0e27 commit ffb15ca
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 202 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ require (
github.com/go-toolsmith/typep v1.1.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golangci/modinfo v0.3.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/gostaticanalysis/analysisutil v0.7.1 // indirect
github.com/gostaticanalysis/comment v1.4.2 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 90 additions & 35 deletions pkg/golinters/gci/gci.go
Original file line number Diff line number Diff line change
@@ -1,61 +1,116 @@
package gci

import (
"bytes"
"fmt"
"strings"
"io"
"os"

gcicfg "github.com/daixiang0/gci/pkg/config"
"github.com/daixiang0/gci/pkg/gci"
"github.com/daixiang0/gci/pkg/log"
"github.com/shazow/go-diff/difflib"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/goanalysis"
"github.com/golangci/golangci-lint/pkg/golinters/gci/internal"
"github.com/golangci/golangci-lint/pkg/golinters/internal"
"github.com/golangci/golangci-lint/pkg/lint/linter"
)

const linterName = "gci"

const prefixSeparator = "¤"
type differ interface {
Diff(out io.Writer, a io.ReadSeeker, b io.ReadSeeker) error
}

func New(settings *config.GciSettings) *goanalysis.Linter {
a := internal.NewAnalyzer()

var cfg map[string]map[string]any
if settings != nil {
var sections []string
for _, section := range settings.Sections {
if strings.HasPrefix(section, "prefix(") {
sections = append(sections, strings.ReplaceAll(section, ",", prefixSeparator))
continue
log.InitLogger()
_ = log.L().Sync()

diff := difflib.New()

a := &analysis.Analyzer{
Name: linterName,
Doc: goanalysis.TheOnlyanalyzerDoc,
Run: goanalysis.DummyRun,
}

return goanalysis.NewLinter(
linterName,
a.Doc,
[]*analysis.Analyzer{a},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
a.Run = func(pass *analysis.Pass) (any, error) {
err := run(lintCtx, pass, settings, diff)
if err != nil {
return nil, err
}

sections = append(sections, section)
return nil, nil
}
}).WithLoadMode(goanalysis.LoadModeSyntax)
}

func run(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GciSettings, diff differ) error {
cfg := gcicfg.YamlConfig{
Cfg: gcicfg.BoolConfig{
NoInlineComments: settings.NoInlineComments,
NoPrefixComments: settings.NoPrefixComments,
SkipGenerated: settings.SkipGenerated,
CustomOrder: settings.CustomOrder,
NoLexOrder: settings.NoLexOrder,
},
SectionStrings: settings.Sections,
ModPath: pass.Module.Path,
}

cfg = map[string]map[string]any{
a.Name: {
internal.NoInlineCommentsFlag: settings.NoInlineComments,
internal.NoPrefixCommentsFlag: settings.NoPrefixComments,
internal.SkipGeneratedFlag: settings.SkipGenerated,
internal.SectionsFlag: sections, // bug because prefix contains comas.
internal.CustomOrderFlag: settings.CustomOrder,
internal.NoLexOrderFlag: settings.NoLexOrder,
internal.PrefixDelimiterFlag: prefixSeparator,
},
if settings.LocalPrefixes != "" {
cfg.SectionStrings = []string{
"standard",
"default",
fmt.Sprintf("prefix(%s)", settings.LocalPrefixes),
}
}

parsedCfg, err := cfg.Parse()
if err != nil {
return err
}

if settings.LocalPrefixes != "" {
prefix := []string{
"standard",
"default",
fmt.Sprintf("prefix(%s)", strings.Join(strings.Split(settings.LocalPrefixes, ","), prefixSeparator)),
for _, file := range pass.Files {
position, isGoFile := goanalysis.GetGoFilePosition(pass, file)
if !isGoFile {
continue
}

input, err := os.ReadFile(position.Filename)
if err != nil {
return fmt.Errorf("unable to open file %s: %w", position.Filename, err)
}

_, output, err := gci.LoadFormat(input, position.Filename, *parsedCfg)
if err != nil {
return fmt.Errorf("error while running gci: %w", err)
}

if !bytes.Equal(input, output) {
out := bytes.NewBufferString(fmt.Sprintf("--- %[1]s\n+++ %[1]s\n", position.Filename))

err := diff.Diff(out, bytes.NewReader(input), bytes.NewReader(output))
if err != nil {
return fmt.Errorf("error while running gci: %w", err)
}

diff := out.String()

err = internal.ExtractDiagnosticFromPatch(pass, file, diff, lintCtx)
if err != nil {
return fmt.Errorf("can't extract issues from gci diff output %q: %w", diff, err)
}
cfg[a.Name][internal.SectionsFlag] = prefix
}
}

return goanalysis.NewLinter(
linterName,
a.Doc,
[]*analysis.Analyzer{a},
cfg,
).WithLoadMode(goanalysis.LoadModeSyntax)
return nil
}
143 changes: 0 additions & 143 deletions pkg/golinters/gci/internal/analyzer.go

This file was deleted.

11 changes: 0 additions & 11 deletions pkg/golinters/gci/internal/errors.go

This file was deleted.

5 changes: 2 additions & 3 deletions pkg/golinters/gci/testdata/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
//golangcitest:config_path testdata/gci.yml
package testdata

// want +1 "File is not properly formatted"
import (
import ( // want "File is not properly formatted"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/config" // want "File is not properly formatted"
"fmt"
"errors"
gcicfg "github.com/daixiang0/gci/pkg/config"
Expand Down
9 changes: 2 additions & 7 deletions pkg/golinters/gci/testdata/gci_cgo.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//go:build ignore

// TODO(ldez) the linter doesn't support cgo.

//golangcitest:args -Egci
//golangcitest:config_path testdata/gci.yml
package testdata
Expand All @@ -16,10 +12,9 @@ package testdata
*/
import "C"

// want +1 "File is not properly formatted"
import (
import ( // want "File is not properly formatted"
"golang.org/x/tools/go/analysis"
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/config" // want "File is not properly formatted"
"unsafe"
"fmt"
"errors"
Expand Down

0 comments on commit ffb15ca

Please sign in to comment.