-
Notifications
You must be signed in to change notification settings - Fork 65
Add basic attempt at local module handling #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
daixiang0
merged 1 commit into
daixiang0:master
from
matthewhughes934:first-attempt-at-local-module-handling
Feb 27, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.