-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic attempt at local module handling
The idea here is to support a new formatting section, `module`, which is the module we're currently running in as a replacement for `prefix(module/we/are/running/in)`. Since this is dependent on the directory structure and where things are run, some tests have been added which run on a real module structure. This approach just focusses on the use-case of running `gci` from the top level of a module. It does not, for example support: * Discovering modules with a nested structure * Discovering modules defined in a `go.work` file It was considered to get the module from `go list -m`, but this would drag in all modules defined under the current directory (i.e. those in `go.work`) which might be confusing if those modules are importing each other. This could be improved, if we know the name of the file whose imports we're processing we could check if that file is under the directory of a given local module (possible improvement for the future). Another alternative to consider would be using query patterns with `golang.org/x/tools/go/packages` e.g. config := packages.Config{Mode: packages.NeedModule | packages.NeedFiles | packages.NeedName} // where each "file=" maps to a file given as an argument to gci packages, err := packages.Load( &config, "file=./path/to/file.go", "file=./path/to/other.go", ) Signed-off-by: Matthew Hughes <matthewhughes934@gmail.com>
- Loading branch information
1 parent
19d84f7
commit 6d38e65
Showing
17 changed files
with
305 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# try and stop git running on Windows from converting line endings from | ||
# in all Go files under this directory. This is to avoid inconsistent test | ||
# results when `gci` strips "\r" characters | ||
**/*.go eol=lf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
sections: | ||
- Standard | ||
- Default | ||
- LocalModule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module example.com/simple | ||
|
||
go 1.20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package foo | ||
|
||
import ( | ||
"example.com/simple/internal/bar" | ||
"log" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package foo | ||
|
||
import ( | ||
"log" | ||
|
||
"example.com/simple/internal/bar" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package internal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package lib | ||
|
||
import ( | ||
"golang.org/x/net" | ||
"example.com/simple/internal" | ||
"example.com/simple/internal/foo" | ||
"example.com/simple/internal/bar" | ||
"log" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package lib | ||
|
||
import ( | ||
"log" | ||
|
||
"golang.org/x/net" | ||
|
||
"example.com/simple/internal" | ||
"example.com/simple/internal/bar" | ||
"example.com/simple/internal/foo" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package section | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/packages" | ||
|
||
"github.com/daixiang0/gci/pkg/parse" | ||
"github.com/daixiang0/gci/pkg/specificity" | ||
) | ||
|
||
const LocalModuleType = "localmodule" | ||
|
||
type LocalModule struct { | ||
Paths []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{} | ||
} | ||
} | ||
|
||
return specificity.MisMatch{} | ||
} | ||
|
||
func (m *LocalModule) String() string { | ||
return LocalModuleType | ||
} | ||
|
||
func (m *LocalModule) Type() string { | ||
return LocalModuleType | ||
} | ||
|
||
// Configure configures the module section by finding the module | ||
// for the current path | ||
func (m *LocalModule) Configure() error { | ||
modPaths, err := findLocalModules() | ||
if err != nil { | ||
return err | ||
} | ||
m.Paths = modPaths | ||
return nil | ||
} | ||
|
||
func findLocalModules() ([]string, error) { | ||
packages, 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 packages { | ||
if len(pkg.Errors) != 0 { | ||
return nil, fmt.Errorf("error reading local packages: %v", pkg.Errors) | ||
} | ||
if pkg.Module != nil { | ||
uniqueModules[pkg.Module.Path] = struct{}{} | ||
} | ||
} | ||
|
||
modPaths := make([]string, 0, len(uniqueModules)) | ||
for mod := range uniqueModules { | ||
modPaths = append(modPaths, mod) | ||
} | ||
|
||
return modPaths, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package specificity | ||
|
||
type LocalModule struct{} | ||
|
||
func (m LocalModule) IsMoreSpecific(than MatchSpecificity) bool { | ||
return isMoreSpecific(m, than) | ||
} | ||
|
||
func (m LocalModule) Equal(to MatchSpecificity) bool { | ||
return equalSpecificity(m, to) | ||
} | ||
|
||
func (LocalModule) class() specificityClass { | ||
return LocalModuleClass | ||
} |
Oops, something went wrong.