Skip to content

Commit 3ab1456

Browse files
committed
feat(go): return empty map when current go module has no deps
1 parent bf1d23f commit 3ab1456

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

lang/golang/parser/parser.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ func getDeps(dir string) (map[string]string, error) {
154154
return nil, fmt.Errorf("failed to execute 'go mod tidy', err: %v, output: %s", err, string(output))
155155
}
156156

157+
if hasNoDeps(filepath.Join(dir, "go.mod")) {
158+
return map[string]string{}, nil
159+
}
160+
157161
cmd = exec.Command("go", "list", "-json", "all")
158162
cmd.Dir = dir
159163
output, err = cmd.CombinedOutput()

lang/golang/parser/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
"github.com/Knetic/govaluate"
3434
. "github.com/cloudwego/abcoder/lang/uniast"
35+
"golang.org/x/mod/modfile"
3536
)
3637

3738
func shouldIgnoreDir(path string) bool {
@@ -152,6 +153,15 @@ func getPackageAlias(importPath string) string {
152153
return alias
153154
}
154155

156+
func hasNoDeps(modFilePath string) bool {
157+
mod, err := modfile.Parse(modFilePath, nil, nil)
158+
if err != nil {
159+
return false
160+
}
161+
162+
return len(mod.Require) == 0
163+
}
164+
155165
func getModuleName(modFilePath string) (string, []byte, error) {
156166
file, err := os.Open(modFilePath)
157167
if err != nil {

0 commit comments

Comments
 (0)