From ed205573c0402e87cd9c2241086ac433997911ac Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 28 Mar 2024 20:50:47 +0100 Subject: [PATCH] build(deps): bump github.com/daixiang0/gci from 0.12.3 to 0.13.3 (#4522) Co-authored-by: Fernandez Ludovic --- .golangci.next.reference.yml | 3 +- go.mod | 3 +- go.sum | 6 +- jsonschema/golangci.next.jsonschema.json | 16 ++- pkg/golinters/gci.go | 124 +++++++++++++++++++---- 5 files changed, 130 insertions(+), 22 deletions(-) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index c4e23bb0cbd6..85f9737b8591 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -436,7 +436,7 @@ linters-settings: # Section configuration to compare against. # Section names are case-insensitive and may contain parameters in (). - # The default order of sections is `standard > default > custom > blank > dot > alias`, + # The default order of sections is `standard > default > custom > blank > dot > alias > localmodule`, # If `custom-order` is `true`, it follows the order of `sections` option. # Default: ["standard", "default"] sections: @@ -446,6 +446,7 @@ linters-settings: - blank # Blank section: contains all blank imports. This section is not present unless explicitly enabled. - dot # Dot section: contains all dot imports. This section is not present unless explicitly enabled. - alias # Alias section: contains all alias imports. This section is not present unless explicitly enabled. + - localmodule # Local module section: contains all local packages. This section is not present unless explicitly enabled. # Skip generated files. # Default: true diff --git a/go.mod b/go.mod index 3a69c37956cd..52f99ecaeba8 100644 --- a/go.mod +++ b/go.mod @@ -31,7 +31,7 @@ require ( github.com/charithe/durationcheck v0.0.10 github.com/ckaznocha/intrange v0.1.1 github.com/curioswitch/go-reassign v0.2.0 - github.com/daixiang0/gci v0.12.3 + github.com/daixiang0/gci v0.13.3 github.com/denis-tingaikin/go-header v0.5.0 github.com/fatih/color v1.16.0 github.com/firefart/nonamedreturns v1.0.4 @@ -44,6 +44,7 @@ require ( github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a github.com/golangci/gofmt v0.0.0-20231018234816-f50ced29576e github.com/golangci/misspell v0.4.1 + github.com/golangci/modinfo v0.3.4 github.com/golangci/plugin-module-register v0.1.1 github.com/golangci/revgrep v0.5.2 github.com/golangci/unconvert v0.0.0-20240309020433-c5143eacb3ed diff --git a/go.sum b/go.sum index 196d45d2c400..3768c274a6a1 100644 --- a/go.sum +++ b/go.sum @@ -122,8 +122,8 @@ github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGX github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/curioswitch/go-reassign v0.2.0 h1:G9UZyOcpk/d7Gd6mqYgd8XYWFMw/znxwGDUstnC9DIo= github.com/curioswitch/go-reassign v0.2.0/go.mod h1:x6OpXuWvgfQaMGks2BZybTngWjT84hqJfKoO8Tt/Roc= -github.com/daixiang0/gci v0.12.3 h1:yOZI7VAxAGPQmkb1eqt5g/11SUlwoat1fSblGLmdiQc= -github.com/daixiang0/gci v0.12.3/go.mod h1:xtHP9N7AHdNvtRNfcx9gwTDfw7FRJx4bZUsiEfiNNAI= +github.com/daixiang0/gci v0.13.3 h1:dyF6x6lKHzH+UK1/Ni7CVElxUiocdWWm5XdBxCJCr4M= +github.com/daixiang0/gci v0.13.3/go.mod h1:12etP2OniiIdP4q+kjUGrC/rUagga7ODbqsom5Eo5Yk= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -231,6 +231,8 @@ github.com/golangci/gofmt v0.0.0-20231018234816-f50ced29576e h1:ULcKCDV1LOZPFxGZ github.com/golangci/gofmt v0.0.0-20231018234816-f50ced29576e/go.mod h1:Pm5KhLPA8gSnQwrQ6ukebRcapGb/BG9iUkdaiCcGHJM= github.com/golangci/misspell v0.4.1 h1:+y73iSicVy2PqyX7kmUefHusENlrP9YwuHZHPLGQj/g= github.com/golangci/misspell v0.4.1/go.mod h1:9mAN1quEo3DlpbaIKKyEvRxK1pwqR9s/Sea1bJCtlNI= +github.com/golangci/modinfo v0.3.4 h1:oU5huX3fbxqQXdfspamej74DFX0kyGLkw1ppvXoJ8GA= +github.com/golangci/modinfo v0.3.4/go.mod h1:wytF1M5xl9u0ij8YSvhkEVPP3M5Mc7XLl1pxH3B2aUM= github.com/golangci/plugin-module-register v0.1.1 h1:TCmesur25LnyJkpsVrupv1Cdzo+2f7zX0H6Jkw1Ol6c= github.com/golangci/plugin-module-register v0.1.1/go.mod h1:TTpqoB6KkwOJMV8u7+NyXMrkwwESJLOkfl9TxR1DGFc= github.com/golangci/revgrep v0.5.2 h1:EndcWoRhcnfj2NHQ+28hyuXpLMF+dQmCN+YaeeIl4FU= diff --git a/jsonschema/golangci.next.jsonschema.json b/jsonschema/golangci.next.jsonschema.json index f411de6cbe3a..1c65d114f31f 100644 --- a/jsonschema/golangci.next.jsonschema.json +++ b/jsonschema/golangci.next.jsonschema.json @@ -1020,7 +1020,21 @@ "description": "Section configuration to compare against.", "type": "array", "items": { - "type": "string" + "anyOf": [ + { + "enum": [ + "standard", + "default", + "blank", + "dot", + "alias", + "localmodule" + ] + }, + { + "type": "string" + } + ] }, "default": ["standard", "default"] }, diff --git a/pkg/golinters/gci.go b/pkg/golinters/gci.go index 7f869930c6cd..9fe75aa99ebd 100644 --- a/pkg/golinters/gci.go +++ b/pkg/golinters/gci.go @@ -2,12 +2,16 @@ package golinters import ( "fmt" + "sort" + "strings" "sync" gcicfg "github.com/daixiang0/gci/pkg/config" "github.com/daixiang0/gci/pkg/gci" "github.com/daixiang0/gci/pkg/io" "github.com/daixiang0/gci/pkg/log" + "github.com/daixiang0/gci/pkg/section" + "github.com/golangci/modinfo" "github.com/hexops/gotextdiff" "github.com/hexops/gotextdiff/myers" "github.com/hexops/gotextdiff/span" @@ -29,6 +33,9 @@ func NewGci(settings *config.GciSettings) *goanalysis.Linter { Name: gciName, Doc: goanalysis.TheOnlyanalyzerDoc, Run: goanalysis.DummyRun, + Requires: []*analysis.Analyzer{ + modinfo.Analyzer, + }, } var cfg *gcicfg.Config @@ -47,7 +54,7 @@ func NewGci(settings *config.GciSettings) *goanalysis.Linter { } var err error - cfg, err = rawCfg.Parse() + cfg, err = YamlConfig{origin: rawCfg}.Parse() if err != nil { internal.LinterLogger.Fatalf("gci: configuration parsing: %v", err) } @@ -62,6 +69,12 @@ func NewGci(settings *config.GciSettings) *goanalysis.Linter { nil, ).WithContextSetter(func(lintCtx *linter.Context) { analyzer.Run = func(pass *analysis.Pass) (any, error) { + var err error + cfg.Sections, err = hackSectionList(pass, cfg) + if err != nil { + return nil, err + } + issues, err := runGci(pass, lintCtx, cfg, &lock) if err != nil { return nil, err @@ -111,6 +124,57 @@ func runGci(pass *analysis.Pass, lintCtx *linter.Context, cfg *gcicfg.Config, lo return issues, nil } +func getIssuedTextGci(settings *config.LintersSettings) string { + text := "File is not `gci`-ed" + + hasOptions := settings.Gci.SkipGenerated || len(settings.Gci.Sections) > 0 + if !hasOptions { + return text + } + + text += " with" + + if settings.Gci.SkipGenerated { + text += " --skip-generated" + } + + if len(settings.Gci.Sections) > 0 { + for _, sect := range settings.Gci.Sections { + text += " -s " + sect + } + } + + if settings.Gci.CustomOrder { + text += " --custom-order" + } + + return text +} + +func hackSectionList(pass *analysis.Pass, cfg *gcicfg.Config) (section.SectionList, error) { + var sections section.SectionList + + for _, sect := range cfg.Sections { + // local module hack + if v, ok := sect.(*section.LocalModule); ok { + info, err := modinfo.FindModuleFromPass(pass) + if err != nil { + return nil, err + } + + if info.Path == "" { + continue + } + + v.Path = info.Path + } + + sections = append(sections, sect) + } + + return sections, nil +} + // diffFormattedFilesToArray is a copy of gci.DiffFormattedFilesToArray without io.StdInGenerator. // gci.DiffFormattedFilesToArray uses gci.processStdInAndGoFilesInPaths that uses io.StdInGenerator but stdin is not active on CI. // https://github.com/daixiang0/gci/blob/6f5cb16718ba07f0342a58de9b830ec5a6d58790/pkg/gci/gci.go#L63-L75 @@ -130,29 +194,55 @@ func diffFormattedFilesToArray(paths []string, cfg gcicfg.Config, diffs *[]strin }) } -func getIssuedTextGci(settings *config.LintersSettings) string { - text := "File is not `gci`-ed" +// Code bellow this comment is borrowed and modified from gci. +// https://github.com/daixiang0/gci/blob/4725b0c101801e7449530eee2ddb0c72592e3405/pkg/config/config.go + +var defaultOrder = map[string]int{ + section.StandardType: 0, + section.DefaultType: 1, + section.CustomType: 2, + section.BlankType: 3, + section.DotType: 4, + section.AliasType: 5, + section.LocalModuleType: 6, +} - hasOptions := settings.Gci.SkipGenerated || len(settings.Gci.Sections) > 0 - if !hasOptions { - return text - } +type YamlConfig struct { + origin gcicfg.YamlConfig +} - text += " with" +//nolint:gocritic // code borrowed from gci and modified to fix LocalModule section behavior. +func (g YamlConfig) Parse() (*gcicfg.Config, error) { + var err error - if settings.Gci.SkipGenerated { - text += " --skip-generated" + sections, err := section.Parse(g.origin.SectionStrings) + if err != nil { + return nil, err } - if len(settings.Gci.Sections) > 0 { - for _, section := range settings.Gci.Sections { - text += " -s " + section - } + if sections == nil { + sections = section.DefaultSections() } - if settings.Gci.CustomOrder { - text += " --custom-order" + // if default order sorted sections + if !g.origin.Cfg.CustomOrder { + sort.Slice(sections, func(i, j int) bool { + sectionI, sectionJ := sections[i].Type(), sections[j].Type() + + if strings.Compare(sectionI, sectionJ) == 0 { + return strings.Compare(sections[i].String(), sections[j].String()) < 0 + } + return defaultOrder[sectionI] < defaultOrder[sectionJ] + }) } - return text + sectionSeparators, err := section.Parse(g.origin.SectionSeparatorStrings) + if err != nil { + return nil, err + } + if sectionSeparators == nil { + sectionSeparators = section.DefaultSectionSeparators() + } + + return &gcicfg.Config{BoolConfig: g.origin.Cfg, Sections: sections, SectionSeparators: sectionSeparators}, nil }