Skip to content

Commit a309e5c

Browse files
authored
feat: execute go mod tidy (#133)
1 parent 00a0ea6 commit a309e5c

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

lang/golang/parser/parser.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,20 @@ type dep struct {
149149
}
150150

151151
func getDeps(dir string) (map[string]string, error) {
152-
cmd := exec.Command("go", "list", "-e", "-json", "all")
152+
// run go mod tidy first to ensure all dependencies are resolved
153+
cmd := exec.Command("go", "mod", "tidy", "-e")
153154
cmd.Dir = dir
154155
output, err := cmd.CombinedOutput()
156+
if err != nil {
157+
return nil, fmt.Errorf("failed to execute 'go mod tidy', err: %v, output: %s", err, string(output))
158+
}
159+
if hasNoDeps(filepath.Join(dir, "go.mod")) {
160+
return map[string]string{}, nil
161+
}
162+
// -mod=mod to use go mod when go mod is inconsistent with go vendor
163+
cmd = exec.Command("go", "list", "-e", "-json", "-mod=mod", "all")
164+
cmd.Dir = dir
165+
output, err = cmd.CombinedOutput()
155166
if err != nil {
156167
return nil, fmt.Errorf("failed to execute 'go list -json all', err: %v, output: %s", err, string(output))
157168
}

0 commit comments

Comments
 (0)