@@ -12,36 +12,79 @@ import (
1212func findModuleDirective (rootDir string ) string {
1313 goModFile := findGoModFile (rootDir )
1414 if goModFile == "" {
15- logger .L .Warn ().Str ("dir" , rootDir ).Msg ("could not find go.mod file in root dir" )
15+ logger .L .Warn ().Str ("dir" , rootDir ).
16+ Msg ("go.mod file not found in root directory (consider setting up source dir)" )
1617 return ""
1718 }
1819
20+ logger .L .Debug ().Str ("file" , goModFile ).Msg ("go.mod file found" )
21+
1922 module := readModuleDirective (goModFile )
2023 if module == "" { // coverage-ignore
2124 logger .L .Warn ().Msg ("`module` directive not found" )
2225 }
2326
27+ logger .L .Debug ().Str ("module" , module ).Msg ("using module directive" )
28+
2429 return module
2530}
2631
2732func findGoModFile (rootDir string ) string {
28- var goModFile string
33+ goModFile := findGoModFromRoot (rootDir )
34+ if goModFile != "" {
35+ return goModFile
36+ }
37+
38+ // fallback to find first go mod file wherever it may be
39+ // not really sure if we really need this ???
40+ return findGoModWithWalk (rootDir )
41+ }
42+
43+ func findGoModWithWalk (rootDir string ) string { // coverage-ignore
44+ var goModFiles []string
2945
30- //nolint:errcheck // error ignored because there is fallback mechanism for finding files
31- filepath .Walk (rootDir , func (file string , info os.FileInfo , err error ) error {
46+ err := filepath .Walk (rootDir , func (file string , info os.FileInfo , err error ) error {
3247 if err != nil { // coverage-ignore
3348 return err
3449 }
3550
3651 if info .Name () == "go.mod" {
37- goModFile = file
38- return filepath .SkipAll
52+ goModFiles = append (goModFiles , file )
3953 }
4054
4155 return nil
4256 })
57+ if err != nil {
58+ logger .L .Error ().Err (err ).Msg ("listing files (go.mod search)" )
59+ }
60+
61+ if len (goModFiles ) == 0 {
62+ logger .L .Warn ().Msg ("go.mod file not found via walk method" )
63+ return ""
64+ }
65+
66+ if len (goModFiles ) > 1 {
67+ logger .L .Warn ().Msg ("found multiple go.mod files via walk method" )
68+ return ""
69+ }
70+
71+ return goModFiles [0 ]
72+ }
73+
74+ func findGoModFromRoot (rootDir string ) string {
75+ files , err := os .ReadDir (rootDir )
76+ if err != nil { // coverage-ignore
77+ logger .L .Error ().Err (err ).Msg ("reading directory" )
78+ return ""
79+ }
80+
81+ for _ , info := range files {
82+ if info .Name () == "go.mod" {
83+ return filepath .Join (rootDir , info .Name ())
84+ }
85+ }
4386
44- return goModFile
87+ return ""
4588}
4689
4790func readModuleDirective (filename string ) string {
0 commit comments