Skip to content

Commit

Permalink
feat: use go/analysis.Pass.Module
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Nov 30, 2024
1 parent 643f097 commit 793042f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.0
require (
github.com/stretchr/testify v1.10.0
golang.org/x/mod v0.22.0
golang.org/x/tools v0.27.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/tools v0.27.0 h1:qEKojBykQkQ4EynWy4S8Weg69NumxKdn40Fce3uc/8o=
golang.org/x/tools v0.27.0/go.mod h1:sUi0ZgbwW9ZPAq26Ekut+weQPR5eIM6GQLQ1Yjm1H0Q=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
37 changes: 31 additions & 6 deletions module.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"

"golang.org/x/mod/modfile"
"golang.org/x/tools/go/analysis"
)

type modInfo struct {
Expand All @@ -20,26 +22,49 @@ type modInfo struct {
}

// GetModuleFile gets module file.
// It's better to use [GetGoModFile] instead of this function.
func GetModuleFile() (*modfile.File, error) {
goMod, err := getModulePath()
if err != nil {
return nil, err
}

if goMod == "" {
return nil, errors.New("working directory is not part of a module")
}

return parseGoMod(goMod)
}

// GetGoModFile gets module file.
func GetGoModFile(pass *analysis.Pass) (*modfile.File, error) {
if pass.Module != nil && pass.Module.Path != "" {
return parseGoMod(pass.Module.Path)
}

return GetModuleFile()
}

func getModulePath() (string, error) {
// https://github.com/golang/go/issues/44753#issuecomment-790089020
cmd := exec.Command("go", "list", "-m", "-json")

raw, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("command go list: %w: %s", err, string(raw))
return "", fmt.Errorf("command go list: %w: %s", err, string(raw))
}

var v modInfo
err = json.NewDecoder(bytes.NewBuffer(raw)).Decode(&v)
if err != nil {
return nil, fmt.Errorf("unmarshaling error: %w: %s", err, string(raw))
return "", fmt.Errorf("unmarshaling error: %w: %s", err, string(raw))
}

if v.GoMod == "" {
return nil, errors.New("working directory is not part of a module")
}
return v.GoMod, nil
}

raw, err = os.ReadFile(v.GoMod)
func parseGoMod(goMod string) (*modfile.File, error) {
raw, err := os.ReadFile(filepath.Clean(goMod))
if err != nil {
return nil, fmt.Errorf("reading go.mod file: %w", err)
}
Expand Down

0 comments on commit 793042f

Please sign in to comment.