Skip to content

Commit 142fe25

Browse files
committed
internal/govulncheck: fail in GOPATH mode
Follows https://go-review.git.corp.google.com/c/vuln/+/395241 Fixes golang/go#53741 Change-Id: I9d751cd40530fd31c1d86c26dd4f718681b7719c Reviewed-on: https://go-review.googlesource.com/c/vuln/+/443455 Run-TryBot: Zvonimir Pavlinovic <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent ff566fe commit 142fe25

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

cmd/govulncheck/testdata/nogomod.ct

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ govulncheck is an experimental tool. Share feedback at https://go.dev/s/govulnch
66
Scanning for dependencies with known vulnerabilities...
77
govulncheck: no go.mod file
88

9-
govulncheck only works Go with modules. To make your project a module, run go mod init.
9+
govulncheck only works Go with modules. Try navigating to your module directory.
10+
Otherwise, run go mod init to make your project a module.
1011

1112
See https://go.dev/doc/modules/managing-dependencies for more information.

internal/govulncheck/errors.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"errors"
99
"os"
1010
"strings"
11+
12+
"golang.org/x/vuln/vulncheck"
1113
)
1214

1315
var (
@@ -30,7 +32,8 @@ govulncheck with the current Go version.`)
3032
// ErrNoGoSum indicates that a go.mod file was not found in this module.
3133
ErrNoGoMod = errors.New(`no go.mod file
3234
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.
3437
3538
See https://go.dev/doc/modules/managing-dependencies for more information.`)
3639

@@ -39,6 +42,14 @@ See https://go.dev/doc/modules/managing-dependencies for more information.`)
3942
4043
Your module is missing a go.sum file. Try running go mod tidy.
4144
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+
4253
See https://go.dev/doc/modules/managing-dependencies for more information.`)
4354
)
4455

@@ -65,3 +76,34 @@ func isGoVersionMismatchError(err error) bool {
6576
return strings.Contains(msg, "This application uses version go") &&
6677
strings.Contains(msg, "It may fail to process source files")
6778
}
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+
}

internal/govulncheck/legacy_run.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func LegacyRun(ctx context.Context, cfg LegacyConfig) (*Result, error) {
6969
}
7070
return nil, err
7171
}
72+
// If we are in GOPATH mode, then no version information will be available.
73+
if inGoPathMode(pkgs) {
74+
return nil, ErrNoModVersion
75+
}
7276

7377
// Sort pkgs so that the PkgNodes returned by vulncheck.Source will be
7478
// deterministic.

0 commit comments

Comments
 (0)