diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml
index c4e23bb0..85f9737b 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 3a69c379..52f99eca 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 196d45d2..3768c274 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 f411de6c..1c65d114 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 7f869930..9fe75aa9 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
- 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 _, section := range settings.Gci.Sections {
- text += " -s " + section
- }
- }
-
- if settings.Gci.CustomOrder {
- text += " --custom-order"
- }
-
- return text
+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,
+}
+
+type YamlConfig struct {
+ origin gcicfg.YamlConfig
+}
+
+//nolint:gocritic // code borrowed from gci and modified to fix LocalModule section behavior.
+func (g YamlConfig) Parse() (*gcicfg.Config, error) {
+ var err error
+
+ sections, err := section.Parse(g.origin.SectionStrings)
+ if err != nil {
+ return nil, err
+ }
+
+ if sections == nil {
+ sections = section.DefaultSections()
+ }
+
+ // 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]
+ })
+ }
+
+ 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
}