diff --git a/pkg/gci/gci_test.go b/pkg/gci/gci_test.go index ff212ec..f6db3e0 100644 --- a/pkg/gci/gci_test.go +++ b/pkg/gci/gci_test.go @@ -21,10 +21,6 @@ func init() { } func TestRun(t *testing.T) { - // if runtime.GOOS == "windows" { - // t.Skip("Skipping test on Windows") - // } - for i := range testCases { t.Run(fmt.Sprintf("run case: %s", testCases[i].name), func(t *testing.T) { config, err := config.ParseConfig(testCases[i].config) @@ -56,10 +52,10 @@ func chdir(t *testing.T, dir string) { func readConfig(t *testing.T, configPath string) *config.Config { rawConfig, err := os.ReadFile(configPath) require.NoError(t, err) - config, err := config.ParseConfig(string(rawConfig)) + cfg, err := config.ParseConfig(string(rawConfig)) require.NoError(t, err) - return config + return cfg } func TestRunWithLocalModule(t *testing.T) { @@ -126,20 +122,5 @@ func TestRunWithLocalModuleWithPackageLoadFailure(t *testing.T) { chdir(t, dir) _, err := config.ParseConfig(configContent) - require.ErrorContains(t, err, "failed to load local modules: ") -} - -func TestRunWithLocalModuleWithModuleLookupError(t *testing.T) { - t.Skipf("In fact there is no error when no go files found in the project root") - - dir := t.TempDir() - // error from trying to list packages under module with no go files - configContent := "sections:\n - LocalModule\n" - goModContent := "module example.com/foo\n" - require.NoError(t, os.WriteFile(filepath.Join(dir, "go.mod"), []byte(goModContent), 0o644)) - - chdir(t, dir) - _, err := config.ParseConfig(configContent) - require.ErrorContains(t, err, "error reading local packages: ") - require.ErrorContains(t, err, dir) + require.ErrorContains(t, err, "go.mod: open go.mod:") } diff --git a/pkg/gci/testdata.go b/pkg/gci/testdata.go index 9723981..4f60e88 100644 --- a/pkg/gci/testdata.go +++ b/pkg/gci/testdata.go @@ -1293,29 +1293,6 @@ import ( "fmt" "net" ) -`, - }, - { - "basic module", - // implicitly relies on the local module name being: github.com/daixiang0/gci - `sections: - - Standard - - LocalModule -`, - `package main - -import ( - "os" - "github.com/daixiang0/gci/cmd/gci" -) -`, - `package main - -import ( - "os" - - "github.com/daixiang0/gci/cmd/gci" -) `, }, } diff --git a/pkg/section/local_module.go b/pkg/section/local_module.go index d7c1ee2..7ef6318 100644 --- a/pkg/section/local_module.go +++ b/pkg/section/local_module.go @@ -6,7 +6,6 @@ import ( "strings" "golang.org/x/mod/modfile" - "golang.org/x/tools/go/packages" "github.com/daixiang0/gci/pkg/parse" "github.com/daixiang0/gci/pkg/specificity" @@ -15,15 +14,12 @@ import ( const LocalModuleType = "localmodule" type LocalModule struct { - Paths []string + Path string } func (m *LocalModule) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity { - for _, modPath := range m.Paths { - // also check path etc. - if strings.HasPrefix(spec.Path, modPath) { - return specificity.LocalModule{} - } + if strings.HasPrefix(spec.Path, m.Path) { + return specificity.LocalModule{} } return specificity.MisMatch{} @@ -40,59 +36,21 @@ func (m *LocalModule) Type() string { // Configure configures the module section by finding the module // for the current path func (m *LocalModule) Configure() error { - modPaths, err := findLocalModules() + modPath, err := findLocalModule() if err != nil { return err } - m.Paths = modPaths - return nil -} - -func findLocalModules() ([]string, error) { - pkgs, err := packages.Load( - // find the package in the current dir and load its module - // NeedFiles so there is some more info in package errors - &packages.Config{Mode: packages.NeedModule | packages.NeedFiles}, - ".", - ) - if err != nil { - return nil, fmt.Errorf("failed to load local modules: %v", err) - } - - uniqueModules := make(map[string]struct{}) - - for _, pkg := range pkgs { - if len(pkg.Errors) != 0 && !errorsContainListError(pkg.Errors) { - return nil, fmt.Errorf("error reading local packages: %v", pkg.Errors) - } - if pkg.Module != nil { - uniqueModules[pkg.Module.Path] = struct{}{} - } else { - b, err := os.ReadFile("go.mod") - if err != nil { - return nil, fmt.Errorf("failed to read go.mod file directly: %v", err) - } - name := modfile.ModulePath(b) - uniqueModules[name] = struct{}{} + m.Path = modPath - } - } - - modPaths := make([]string, 0, len(uniqueModules)) - for mod := range uniqueModules { - modPaths = append(modPaths, mod) - } - - return modPaths, nil + return nil } -func errorsContainListError(errors []packages.Error) bool { - for _, v := range errors { - if v.Kind == packages.ListError { - return true - } +func findLocalModule() (string, error) { + b, err := os.ReadFile("go.mod") + if err != nil { + return "", fmt.Errorf("go.mod: %w", err) } - return false + return modfile.ModulePath(b), nil }