WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit de144fb

Browse files
Arielwyy王雅仪
andauthored
fix: resolve index out of range when goModPath is empty (#22)
* feat: add numOfGoMod to count go.mod files in multi-module repositories * fix: resolve index out of range when goModPath is empty --------- Co-authored-by: 王雅仪 <[email protected]>
1 parent 44e6528 commit de144fb

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

parser-Go/main.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ func parseGoModule(rootDir string, output string) {
9898
var moduleName string
9999
// Read the module name from go.mod
100100
// Find the go.mod file
101-
goModPath, err := findAllGoMod(rootDir)
101+
goModPaths, err := findAllGoMod(rootDir)
102102
if err != nil {
103103
moduleName = "__unknown_module__"
104104
} else {
105-
name, _ := readModuleName(goModPath[0])
105+
name, _ := readModuleName(goModPaths[0])
106106
moduleName = name
107107
}
108108

@@ -111,7 +111,11 @@ func parseGoModule(rootDir string, output string) {
111111
// panic(err)
112112
//}
113113
//默认取找到的第一个go.mod
114-
buildAndPrint(moduleName, packages, fset, output, goModPath[0], len(goModPath))
114+
firstGoModPath := ""
115+
if goModPaths != nil {
116+
firstGoModPath = goModPaths[0]
117+
}
118+
buildAndPrint(moduleName, packages, fset, output, firstGoModPath, len(goModPaths))
115119
}
116120

117121
// findGoMod searches for a go.mod file starting from dir and recursing into subdirectories if not found
@@ -144,8 +148,11 @@ func findAllGoMod(dir string) ([]string, error) {
144148
}
145149
}
146150
}
147-
148-
return paths, nil
151+
if paths != nil {
152+
return paths, nil
153+
} else {
154+
return nil, fmt.Errorf("not found go.mod")
155+
}
149156
}
150157

151158
func buildAndPrint(moduleName string, packages map[string]*ast.Package, fset *token.FileSet, outputPath string, goModPath string, numOfGoMod int) {

0 commit comments

Comments
 (0)