Skip to content

Commit

Permalink
gci: fix standard packages list for go1.24 (#5402)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Feb 12, 2025
1 parent 82ad6b6 commit 08ae805
Show file tree
Hide file tree
Showing 8 changed files with 409 additions and 2 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ issues:
- test/testdata_etc # test files
- internal/go # extracted from Go code
- internal/x # extracted from x/tools code
- pkg/goformatters/gci/internal # extracted from gci code
exclude-files:
- pkg/goanalysis/runner_checker.go # extracted from x/tools code

Expand Down
9 changes: 7 additions & 2 deletions pkg/goformatters/gci/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ldez/grignotin/gomod"

"github.com/golangci/golangci-lint/pkg/config"
gcicfgi "github.com/golangci/golangci-lint/pkg/goformatters/gci/internal/config"
"github.com/golangci/golangci-lint/pkg/goformatters/internal"
)

Expand All @@ -28,7 +29,7 @@ func New(settings *config.GciSettings) (*Formatter, error) {
internal.FormatterLogger.Errorf("gci: %v", err)
}

cfg := gcicfg.YamlConfig{
cfg := gcicfgi.YamlConfig{
Cfg: gcicfg.BoolConfig{
NoInlineComments: settings.NoInlineComments,
NoPrefixComments: settings.NoPrefixComments,
Expand All @@ -53,7 +54,11 @@ func New(settings *config.GciSettings) (*Formatter, error) {
return nil, err
}

return &Formatter{config: parsedCfg}, nil
return &Formatter{config: &gcicfg.Config{
BoolConfig: parsedCfg.BoolConfig,
Sections: parsedCfg.Sections,
SectionSeparators: parsedCfg.SectionSeparators,
}}, nil
}

func (*Formatter) Name() string {
Expand Down
29 changes: 29 additions & 0 deletions pkg/goformatters/gci/internal/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
BSD 3-Clause License

Copyright (c) 2020, Xiang Dai
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
106 changes: 106 additions & 0 deletions pkg/goformatters/gci/internal/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package config

import (
"sort"
"strings"

"gopkg.in/yaml.v3"

"github.com/daixiang0/gci/pkg/config"
"github.com/daixiang0/gci/pkg/section"
)

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 Config struct {
config.BoolConfig
Sections section.SectionList
SectionSeparators section.SectionList
}

type YamlConfig struct {
Cfg config.BoolConfig `yaml:",inline"`
SectionStrings []string `yaml:"sections"`
SectionSeparatorStrings []string `yaml:"sectionseparators"`

// Since history issue, Golangci-lint needs Analyzer to run and GCI add an Analyzer layer to integrate.
// The ModPath param is only from analyzer.go, no need to set it in all other places.
ModPath string `yaml:"-"`
}

func (g YamlConfig) Parse() (*Config, error) {
var err error

sections, err := section.Parse(g.SectionStrings)
if err != nil {
return nil, err
}
if sections == nil {
sections = section.DefaultSections()
}
if err := configureSections(sections, g.ModPath); err != nil {
return nil, err
}

// if default order sorted sections
if !g.Cfg.CustomOrder {
sort.Slice(sections, func(i, j int) bool {
sectionI, sectionJ := sections[i].Type(), sections[j].Type()

if g.Cfg.NoLexOrder || strings.Compare(sectionI, sectionJ) != 0 {
return defaultOrder[sectionI] < defaultOrder[sectionJ]
}

return strings.Compare(sections[i].String(), sections[j].String()) < 0
})
}

sectionSeparators, err := section.Parse(g.SectionSeparatorStrings)
if err != nil {
return nil, err
}
if sectionSeparators == nil {
sectionSeparators = section.DefaultSectionSeparators()
}

return &Config{g.Cfg, sections, sectionSeparators}, nil
}

func ParseConfig(in string) (*Config, error) {
config := YamlConfig{}

err := yaml.Unmarshal([]byte(in), &config)
if err != nil {
return nil, err
}

gciCfg, err := config.Parse()
if err != nil {
return nil, err
}

return gciCfg, nil
}

// configureSections now only do golang module path finding.
// Since history issue, Golangci-lint needs Analyzer to run and GCI add an Analyzer layer to integrate.
// The path param is from analyzer.go, in all other places should pass empty string.
func configureSections(sections section.SectionList, path string) error {
for _, sec := range sections {
switch s := sec.(type) {
case *section.LocalModule:
if err := s.Configure(path); err != nil {
return err
}
}
}
return nil
}
3 changes: 3 additions & 0 deletions pkg/goformatters/gci/internal/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Code borrowed from gci and modified to use new std packages.

https://github.com/daixiang0/gci/pull/227
51 changes: 51 additions & 0 deletions pkg/goformatters/gci/internal/section/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package section

import (
"errors"
"fmt"
"strings"

"github.com/daixiang0/gci/pkg/section"
)

func Parse(data []string) (section.SectionList, error) {
if len(data) == 0 {
return nil, nil
}

var list section.SectionList
var errString string
for _, d := range data {
s := strings.ToLower(d)
if len(s) == 0 {
return nil, nil
}

if s == "default" {
list = append(list, section.Default{})
} else if s == "standard" {
list = append(list, Standard{})
} else if s == "newline" {
list = append(list, section.NewLine{})
} else if strings.HasPrefix(s, "prefix(") && len(d) > 8 {
list = append(list, section.Custom{Prefix: d[7 : len(d)-1]})
} else if strings.HasPrefix(s, "commentline(") && len(d) > 13 {
list = append(list, section.Custom{Prefix: d[12 : len(d)-1]})
} else if s == "dot" {
list = append(list, section.Dot{})
} else if s == "blank" {
list = append(list, section.Blank{})
} else if s == "alias" {
list = append(list, section.Alias{})
} else if s == "localmodule" {
// pointer because we need to mutate the section at configuration time
list = append(list, &section.LocalModule{})
} else {
errString += fmt.Sprintf(" %s", s)
}
}
if errString != "" {
return nil, errors.New(fmt.Sprintf("invalid params:%s", errString))
}
return list, nil
}
30 changes: 30 additions & 0 deletions pkg/goformatters/gci/internal/section/standard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package section

import (
"github.com/daixiang0/gci/pkg/parse"
"github.com/daixiang0/gci/pkg/specificity"
)

const StandardType = "standard"

type Standard struct{}

func (s Standard) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
if isStandard(spec.Path) {
return specificity.StandardMatch{}
}
return specificity.MisMatch{}
}

func (s Standard) String() string {
return StandardType
}

func (s Standard) Type() string {
return StandardType
}

func isStandard(pkg string) bool {
_, ok := standardPackages[pkg]
return ok
}
Loading

0 comments on commit 08ae805

Please sign in to comment.