Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lang/golang/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package parser

import (
"bytes"
"fmt"
"go/ast"
"go/parser"
"go/token"
"go/types"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 5 additions & 1 deletion lang/golang/parser/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down