Skip to content

Commit 4b54a8b

Browse files
author
Maceo Thompson
committed
internal/vulncheck: remove -mod=mod flag from LoadModules
This change is a hotfix removing the -mod=mod flag from the go list call in LoadModules. A proper fix to support vendor directories will be coming shortly. Fixes golang/go#65155 Fixes golang/go#65130 Change-Id: I3faf90227154e019ab70201c9e04a1b185bc5f3a Reviewed-on: https://go-review.googlesource.com/c/vuln/+/556775 Reviewed-by: Zvonimir Pavlinovic <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent e313109 commit 4b54a8b

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

internal/vulncheck/packages.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"errors"
1111
"fmt"
1212
"os/exec"
13+
"path/filepath"
1314
"strings"
1415

1516
"golang.org/x/tools/go/packages"
@@ -37,7 +38,17 @@ func NewPackageGraph(goVersion string) *PackageGraph {
3738
}
3839

3940
func (g *PackageGraph) LoadModules(cfg *packages.Config) (mods []*packages.Module, err error) {
40-
cmd := exec.Command("go", "list", "-m", "-json", "-mod=mod", "all")
41+
cmd := exec.Command("go", "list", "-m", "-json")
42+
// Quick fix for go.dev/issue/65155
43+
// TODO: Fix go.dev/issue/65124
44+
// This check makes it so that govulncheck doesn't crash if running on a
45+
// vendored module from the root of a module. Essentially only here so that
46+
// the vendor test doesn't fail until #65124 is fixed.
47+
if fileExists(filepath.Join(cfg.Dir, "vendor")) {
48+
cmd.Args = append(cmd.Args, "-mod=readonly")
49+
}
50+
51+
cmd.Args = append(cmd.Args, "all")
4152
cmd.Env = append(cmd.Env, cfg.Env...)
4253
cmd.Dir = cfg.Dir
4354
out, err := cmd.Output()

internal/vulncheck/utils.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ package vulncheck
77
import (
88
"bytes"
99
"context"
10+
"errors"
1011
"go/token"
1112
"go/types"
13+
"os"
1214
"sort"
1315
"strings"
1416

@@ -357,3 +359,17 @@ func modVersion(mod *packages.Module) string {
357359
}
358360
return mod.Version
359361
}
362+
363+
// fileExists checks if file path exists. Returns true
364+
// if the file exists or it cannot prove that it does
365+
// not exist. Otherwise, returns false.
366+
func fileExists(path string) bool {
367+
if _, err := os.Stat(path); err == nil {
368+
return true
369+
} else if errors.Is(err, os.ErrNotExist) {
370+
return false
371+
}
372+
// Conservatively return true if os.Stat fails
373+
// for some other reason.
374+
return true
375+
}

0 commit comments

Comments
 (0)