8
8
"errors"
9
9
"os"
10
10
"strings"
11
+
12
+ "golang.org/x/vuln/vulncheck"
11
13
)
12
14
13
15
var (
@@ -30,7 +32,8 @@ govulncheck with the current Go version.`)
30
32
// ErrNoGoSum indicates that a go.mod file was not found in this module.
31
33
ErrNoGoMod = errors .New (`no go.mod file
32
34
33
- govulncheck only works Go with modules. To make your project a module, run go mod init.
35
+ govulncheck only works Go with modules. Try navigating to your module directory.
36
+ Otherwise, run go mod init to make your project a module.
34
37
35
38
See https://go.dev/doc/modules/managing-dependencies for more information.` )
36
39
@@ -39,6 +42,14 @@ See https://go.dev/doc/modules/managing-dependencies for more information.`)
39
42
40
43
Your module is missing a go.sum file. Try running go mod tidy.
41
44
45
+ See https://go.dev/doc/modules/managing-dependencies for more information.` )
46
+
47
+ // ErrNoModVersion indicates that govulncheck cannot access module version information.
48
+ ErrNoModVersion = errors .New (`no module version information
49
+
50
+ This can happen when running govulncheck in GOPATH mode. govulncheck needs module
51
+ versions to correctly identify vulnerabilities.
52
+
42
53
See https://go.dev/doc/modules/managing-dependencies for more information.` )
43
54
)
44
55
@@ -65,3 +76,34 @@ func isGoVersionMismatchError(err error) bool {
65
76
return strings .Contains (msg , "This application uses version go" ) &&
66
77
strings .Contains (msg , "It may fail to process source files" )
67
78
}
79
+
80
+ // inGoPathMode checks if govulncheck is running in GOPATH mode by checking
81
+ // if module information is available.
82
+ func inGoPathMode (pkgs []* vulncheck.Package ) bool {
83
+ packageModule := func (p * vulncheck.Package ) * vulncheck.Module {
84
+ m := p .Module
85
+ if m == nil {
86
+ return nil
87
+ }
88
+ if r := m .Replace ; r != nil {
89
+ return r
90
+ }
91
+ return m
92
+ }
93
+
94
+ hasModuleInfo := false
95
+ var visit func (p * vulncheck.Package )
96
+ visit = func (p * vulncheck.Package ) {
97
+ if packageModule (p ) != nil {
98
+ hasModuleInfo = true
99
+ return
100
+ }
101
+ for _ , i := range p .Imports {
102
+ visit (i )
103
+ }
104
+ }
105
+ for _ , p := range pkgs {
106
+ visit (p )
107
+ }
108
+ return ! hasModuleInfo
109
+ }
0 commit comments