Skip to content

Commit 05af382

Browse files
authored
fix(go): return empty map when current go module has no deps (#127)
* feat(go): return empty map when current go module has no deps * fix(go): read modfile correct; use modfile.ModulePath to get module path * chore(go): wrap error * chore(go): use %w to wrap error instead of %s
1 parent bf1d23f commit 05af382

File tree

3 files changed

+24
-26
lines changed

3 files changed

+24
-26
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.23.4
44

55
require (
66
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible
7-
github.com/bytedance/sonic v1.14.1
87
github.com/cloudwego/eino v0.3.52
98
github.com/cloudwego/eino-ext/components/model/ark v0.1.16
109
github.com/cloudwego/eino-ext/components/model/claude v0.1.1
@@ -43,6 +42,7 @@ require (
4342
github.com/bahlo/generic-list-go v0.2.0 // indirect
4443
github.com/buger/jsonparser v1.1.1 // indirect
4544
github.com/bytedance/gopkg v0.1.3 // indirect
45+
github.com/bytedance/sonic v1.14.1 // indirect
4646
github.com/bytedance/sonic/loader v0.3.0 // indirect
4747
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
4848
github.com/cloudwego/base64x v0.1.6 // indirect

lang/golang/parser/parser.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ func (p *GoParser) collectGoMods(startDir string) error {
9999
if err != nil || !strings.HasSuffix(path, "go.mod") {
100100
return nil
101101
}
102-
name, _, err := getModuleName(path)
102+
103+
name, err := getModuleName(path)
103104
if err != nil {
104-
return err
105+
return fmt.Errorf("failed to get module name: %w", err)
105106
}
107+
106108
rel, err := filepath.Rel(p.homePageDir, filepath.Dir(path))
107109
if err != nil {
108110
return fmt.Errorf("module path %v is not in the repo", path)
@@ -154,6 +156,10 @@ func getDeps(dir string) (map[string]string, error) {
154156
return nil, fmt.Errorf("failed to execute 'go mod tidy', err: %v, output: %s", err, string(output))
155157
}
156158

159+
if hasNoDeps(filepath.Join(dir, "go.mod")) {
160+
return map[string]string{}, nil
161+
}
162+
157163
cmd = exec.Command("go", "list", "-json", "all")
158164
cmd.Dir = dir
159165
output, err = cmd.CombinedOutput()

lang/golang/parser/utils.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515
package parser
1616

1717
import (
18-
"bufio"
19-
"bytes"
2018
"container/list"
2119
"fmt"
2220
"go/ast"
2321
"go/build"
2422
"go/types"
25-
"io"
2623
"os"
2724
"os/exec"
2825
"path"
@@ -32,6 +29,7 @@ import (
3229

3330
"github.com/Knetic/govaluate"
3431
. "github.com/cloudwego/abcoder/lang/uniast"
32+
"golang.org/x/mod/modfile"
3533
)
3634

3735
func shouldIgnoreDir(path string) bool {
@@ -152,33 +150,27 @@ func getPackageAlias(importPath string) string {
152150
return alias
153151
}
154152

155-
func getModuleName(modFilePath string) (string, []byte, error) {
156-
file, err := os.Open(modFilePath)
153+
func hasNoDeps(modFilePath string) bool {
154+
content, err := os.ReadFile(modFilePath)
157155
if err != nil {
158-
return "", nil, fmt.Errorf("failed to open file: %v", err)
156+
return false
159157
}
160-
defer file.Close()
161-
data, err := io.ReadAll(file)
158+
159+
modf, err := modfile.Parse(modFilePath, content, nil)
162160
if err != nil {
163-
return "", nil, fmt.Errorf("failed to read file: %v", err)
164-
}
165-
scanner := bufio.NewScanner(bytes.NewBuffer(data))
166-
for scanner.Scan() {
167-
line := scanner.Text()
168-
if strings.HasPrefix(line, "module") {
169-
// Assuming 'module' keyword is followed by module name
170-
parts := strings.Split(line, " ")
171-
if len(parts) > 1 {
172-
return parts[1], data, nil
173-
}
174-
}
161+
return false
175162
}
176163

177-
if err := scanner.Err(); err != nil {
178-
return "", data, fmt.Errorf("failed to scan file: %v", err)
164+
return len(modf.Require) == 0
165+
}
166+
167+
func getModuleName(modFilePath string) (string, error) {
168+
content, err := os.ReadFile(modFilePath)
169+
if err != nil {
170+
return "", fmt.Errorf("failed to read file: %w", err)
179171
}
180172

181-
return "", data, nil
173+
return modfile.ModulePath(content), nil
182174
}
183175

184176
func isGoBuiltins(name string) bool {

0 commit comments

Comments
 (0)