@@ -30,6 +30,7 @@ import (
3030 "regexp"
3131 "strings"
3232
33+ "github.com/cloudwego/abcoder/lang/log"
3334 . "github.com/cloudwego/abcoder/lang/uniast"
3435)
3536
@@ -148,25 +149,21 @@ type dep struct {
148149}
149150
150151func getDeps (dir string ) (map [string ]string , error ) {
151- // run go mod tidy first to ensure all dependencies are resolved
152- cmd := exec .Command ("go" , "mod" , "tidy" )
152+ cmd := exec .Command ("go" , "list" , "-e" , "-json" , "all" )
153153 cmd .Dir = dir
154154 output , err := cmd .CombinedOutput ()
155155 if err != nil {
156- return nil , fmt .Errorf ("failed to execute 'go mod tidy ', err: %v, output: %s" , err , string (output ))
156+ return nil , fmt .Errorf ("failed to execute 'go list -json all ', err: %v, output: %s" , err , string (output ))
157157 }
158-
159- if hasNoDeps (filepath .Join (dir , "go.mod" )) {
160- return map [string ]string {}, nil
158+ // ignore content until first open
159+ index := strings .Index (string (output ), "{" )
160+ if index == - 1 {
161+ return nil , fmt .Errorf ("failed to find '{' in output, output: %s" , string (output ))
161162 }
162-
163- cmd = exec .Command ("go" , "list" , "-json" , "all" )
164- cmd .Dir = dir
165- output , err = cmd .Output ()
166- if err != nil {
167- return nil , fmt .Errorf ("failed to execute 'go list -json all', err: %v, output: %s" , err , string (output ))
163+ if index > 0 {
164+ log .Info ("go list skip prefix, output: %s" , string (output [:index ]))
165+ output = output [index :]
168166 }
169-
170167 deps := make (map [string ]string )
171168 decoder := json .NewDecoder (bytes .NewReader (output ))
172169 for {
@@ -175,7 +172,7 @@ func getDeps(dir string) (map[string]string, error) {
175172 if err .Error () == "EOF" {
176173 break
177174 }
178- return nil , fmt .Errorf ("failed to decode json: %w , output: %s" , err , string (output ))
175+ return nil , fmt .Errorf ("failed to decode json: %v , output: %s" , err , string (output ))
179176 }
180177 module := mod .Module
181178 // golang internal package, ignore it.
0 commit comments