diff --git a/lang/golang/parser/parser.go b/lang/golang/parser/parser.go index 0cce04cc..d1865597 100644 --- a/lang/golang/parser/parser.go +++ b/lang/golang/parser/parser.go @@ -15,6 +15,7 @@ package parser import ( + "bytes" "fmt" "go/ast" "go/parser" @@ -22,6 +23,7 @@ import ( "go/types" "io/fs" "os" + "os/exec" "path/filepath" "regexp" "strings" @@ -142,6 +144,15 @@ func (p *GoParser) ParseRepo() (Repository, error) { func (p *GoParser) ParseModule(mod *Module, dir string) (err error) { filepath.Walk(dir, func(path string, info fs.FileInfo, e error) error { + // run go mod tidy before parse + cmd := exec.Command("go", "mod", "tidy") + cmd.Dir = dir + buf := bytes.NewBuffer(nil) + cmd.Stderr = buf + if err := cmd.Run(); err != nil { + return fmt.Errorf("run go mod tidy failed in %s: %v", dir, buf.String()) + } + if info != nil && info.IsDir() && filepath.Base(path) == ".git" { return filepath.SkipDir } diff --git a/lang/golang/parser/pkg.go b/lang/golang/parser/pkg.go index 61845902..ce75aca8 100644 --- a/lang/golang/parser/pkg.go +++ b/lang/golang/parser/pkg.go @@ -151,7 +151,7 @@ func (p *GoParser) loadPackages(mod *Module, dir string, pkgPath PkgPath) (err e if mm := p.repo.Modules[mod.Name]; mm != nil && (*mm).Packages[pkgPath] != nil { return nil } - fmt.Fprintf(os.Stderr, "[loadPackages] mod: %s, dir: %s, pkgPath: %s", mod.Name, dir, pkgPath) + fmt.Fprintf(os.Stderr, "[loadPackages] mod: %s, dir: %s, pkgPath: %s\n", mod.Name, dir, pkgPath) fset := token.NewFileSet() loadCount++ // slow-path: load packages in the dir, including sub pakcages @@ -181,6 +181,10 @@ func (p *GoParser) loadPackages(mod *Module, dir string, pkgPath PkgPath) (err e } next_file: for idx, file := range pkg.Syntax { + if idx >= len(pkg.GoFiles) { + fmt.Fprintf(os.Stderr, "skip file %s by loader\n", file.Name) + continue + } filePath := pkg.GoFiles[idx] for _, exclude := range p.exclues { if exclude.MatchString(filePath) {