Skip to content

Commit 8f9a4e9

Browse files
authored
Revert: keep standard package list (#151)
If no go in the PATH, packages will raise error: ``` err: go command required, not found: exec: "go": executable file not found in $PATH: stderr: ``` Signed-off-by: Loong <[email protected]>
1 parent 15e6842 commit 8f9a4e9

File tree

8 files changed

+271
-32
lines changed

8 files changed

+271
-32
lines changed

Makefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
.PHONY: clean test build
1+
.PHONY: clean generate test build
22

33
BIN_OUTPUT := $(if $(filter $(shell go env GOOS), windows), dist/gci.exe, dist/gci)
44

5-
default: clean test build
5+
default: clean generate test build
66

77
clean:
88
@echo BIN_OUTPUT: ${BIN_OUTPUT}
@@ -13,3 +13,6 @@ build: clean
1313

1414
test: clean
1515
@go test -v -count=1 -cover ./...
16+
17+
generate:
18+
@go generate ./...

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/daixiang0/gci
22

3-
go 1.20
3+
go 1.18
44

55
require (
66
github.com/hexops/gotextdiff v1.0.3

internal/generate.go

+87
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/section/parser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Parse(data []string) (SectionList, error) {
2222
if s == "default" {
2323
list = append(list, Default{})
2424
} else if s == "standard" {
25-
list = append(list, NewStandard())
25+
list = append(list, Standard{})
2626
} else if s == "newline" {
2727
list = append(list, NewLine{})
2828
} else if strings.HasPrefix(s, "prefix(") && len(d) > 8 {

pkg/section/section.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (list SectionList) String() []string {
2828
}
2929

3030
func DefaultSections() SectionList {
31-
return SectionList{NewStandard(), Default{}}
31+
return SectionList{Standard{}, Default{}}
3232
}
3333

3434
func DefaultSectionSeparators() SectionList {

pkg/section/standard.go

+7-19
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
11
package section
22

33
import (
4-
"golang.org/x/tools/go/packages"
5-
64
"github.com/daixiang0/gci/pkg/parse"
75
"github.com/daixiang0/gci/pkg/specificity"
86
)
97

108
const StandardType = "standard"
119

12-
type Standard struct {
13-
standardPackages map[string]struct{}
14-
}
15-
16-
func NewStandard() Standard {
17-
pkgs, err := packages.Load(nil, "std")
18-
if err != nil {
19-
panic(err)
20-
}
21-
22-
standardPackages := make(map[string]struct{})
23-
for _, p := range pkgs {
24-
standardPackages[p.PkgPath] = struct{}{}
25-
}
26-
return Standard{standardPackages: standardPackages}
27-
}
10+
type Standard struct{}
2811

2912
func (s Standard) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
30-
if _, ok := s.standardPackages[spec.Path]; ok {
13+
if isStandard(spec.Path) {
3114
return specificity.StandardMatch{}
3215
}
3316
return specificity.MisMatch{}
@@ -40,3 +23,8 @@ func (s Standard) String() string {
4023
func (s Standard) Type() string {
4124
return StandardType
4225
}
26+
27+
func isStandard(pkg string) bool {
28+
_, ok := standardPackages[pkg]
29+
return ok
30+
}

pkg/section/standard_list.go

+162
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/section/standard_test.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import (
77
)
88

99
func TestStandardPackageSpecificity(t *testing.T) {
10-
standard := NewStandard()
1110
testCases := []specificityTestData{
12-
{"context", standard, specificity.StandardMatch{}},
13-
{"contexts", standard, specificity.MisMatch{}},
14-
{"crypto", standard, specificity.StandardMatch{}},
15-
{"crypto1", standard, specificity.MisMatch{}},
16-
{"crypto/ae", standard, specificity.MisMatch{}},
17-
{"crypto/aes", standard, specificity.StandardMatch{}},
18-
{"crypto/aes2", standard, specificity.MisMatch{}},
11+
{"context", Standard{}, specificity.StandardMatch{}},
12+
{"contexts", Standard{}, specificity.MisMatch{}},
13+
{"crypto", Standard{}, specificity.StandardMatch{}},
14+
{"crypto1", Standard{}, specificity.MisMatch{}},
15+
{"crypto/ae", Standard{}, specificity.MisMatch{}},
16+
{"crypto/aes", Standard{}, specificity.StandardMatch{}},
17+
{"crypto/aes2", Standard{}, specificity.MisMatch{}},
1918
}
2019
testSpecificity(t, testCases)
2120
}

0 commit comments

Comments
 (0)