@@ -12,10 +12,13 @@ import (
12
12
func findModuleDirective (rootDir string ) string {
13
13
goModFile := findGoModFile (rootDir )
14
14
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)" )
16
17
return ""
17
18
}
18
19
20
+ logger .L .Debug ().Str ("file" , goModFile ).Msg ("go.mod file found" )
21
+
19
22
module := readModuleDirective (goModFile )
20
23
if module == "" { // coverage-ignore
21
24
logger .L .Warn ().Msg ("`module` directive not found" )
@@ -27,23 +30,61 @@ func findModuleDirective(rootDir string) string {
27
30
}
28
31
29
32
func findGoModFile (rootDir string ) string {
30
- 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
31
45
32
- //nolint:errcheck // error ignored because there is fallback mechanism for finding files
33
- 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 {
34
47
if err != nil { // coverage-ignore
35
48
return err
36
49
}
37
50
38
51
if info .Name () == "go.mod" {
39
- goModFile = file
40
- return filepath .SkipAll
52
+ goModFiles = append (goModFiles , file )
41
53
}
42
54
43
55
return nil
44
56
})
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
+ }
45
86
46
- return goModFile
87
+ return ""
47
88
}
48
89
49
90
func readModuleDirective (filename string ) string {
0 commit comments