Skip to content

Commit 77499e9

Browse files
committed
fix: go work scene
1 parent a309e5c commit 77499e9

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

lang/golang/parser/parser.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ func newGoParser(name string, homePageDir string, opts Options) *GoParser {
9696
}
9797

9898
func (p *GoParser) collectGoMods(startDir string) error {
99+
hasGoWork := false
100+
deps := map[string]string{}
99101
err := filepath.Walk(startDir, func(path string, info fs.FileInfo, err error) error {
100102
if err != nil || !strings.HasSuffix(path, "go.mod") {
101103
return nil
@@ -113,7 +115,7 @@ func (p *GoParser) collectGoMods(startDir string) error {
113115
p.repo.Modules[name] = newModule(name, rel)
114116
p.modules = append(p.modules, newModuleInfo(name, rel, name))
115117

116-
deps, err := getDeps(filepath.Dir(path))
118+
deps, hasGoWork, err = getDeps(filepath.Dir(path), hasGoWork)
117119
if err != nil {
118120
return err
119121
}
@@ -148,28 +150,34 @@ type dep struct {
148150
} `json:"Module"`
149151
}
150152

151-
func getDeps(dir string) (map[string]string, error) {
153+
func getDeps(dir string, goWork bool) (a map[string]string, hasGoWork bool, err error) {
152154
// run go mod tidy first to ensure all dependencies are resolved
153155
cmd := exec.Command("go", "mod", "tidy", "-e")
154156
cmd.Dir = dir
155157
output, err := cmd.CombinedOutput()
156158
if err != nil {
157-
return nil, fmt.Errorf("failed to execute 'go mod tidy', err: %v, output: %s", err, string(output))
159+
return nil, hasGoWork, fmt.Errorf("failed to execute 'go mod tidy', err: %v, output: %s", err, string(output))
158160
}
159161
if hasNoDeps(filepath.Join(dir, "go.mod")) {
160-
return map[string]string{}, nil
162+
return map[string]string{}, hasGoWork, nil
161163
}
162164
// -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")
165+
// if go.work exist, it's no need to set -mod=mod
166+
if _, err = os.Stat(filepath.Join(dir, "go.work")); err == nil || goWork {
167+
hasGoWork = true
168+
cmd = exec.Command("go", "list", "-e", "-json", "all")
169+
} else {
170+
cmd = exec.Command("go", "list", "-e", "-json", "-mod=mod", "all")
171+
}
164172
cmd.Dir = dir
165173
output, err = cmd.CombinedOutput()
166174
if err != nil {
167-
return nil, fmt.Errorf("failed to execute 'go list -json all', err: %v, output: %s", err, string(output))
175+
return nil, hasGoWork, fmt.Errorf("failed to execute 'go list -json all', err: %v, output: %s, cmd string: %s, dir: %s", err, string(output), cmd.String(), dir)
168176
}
169177
// ignore content until first open
170178
index := strings.Index(string(output), "{")
171179
if index == -1 {
172-
return nil, fmt.Errorf("failed to find '{' in output, output: %s", string(output))
180+
return nil, hasGoWork, fmt.Errorf("failed to find '{' in output, output: %s", string(output))
173181
}
174182
if index > 0 {
175183
log.Info("go list skip prefix, output: %s", string(output[:index]))
@@ -183,7 +191,7 @@ func getDeps(dir string) (map[string]string, error) {
183191
if err.Error() == "EOF" {
184192
break
185193
}
186-
return nil, fmt.Errorf("failed to decode json: %v, output: %s", err, string(output))
194+
return nil, hasGoWork, fmt.Errorf("failed to decode json: %v, output: %s", err, string(output))
187195
}
188196
module := mod.Module
189197
// golang internal package, ignore it.
@@ -207,7 +215,7 @@ func getDeps(dir string) (map[string]string, error) {
207215
}
208216
}
209217

210-
return deps, nil
218+
return deps, hasGoWork, nil
211219
}
212220

213221
// ParseRepo parse the entiry repo from homePageDir recursively until end

0 commit comments

Comments
 (0)