Skip to content

Commit be1c52d

Browse files
internal: use go env to compute GOMODCACHE
Before, an approach with build.Default.GOPATH was used, which is incorrect. Fixes golang/go#57406 Change-Id: I821e9b1a762f2b7eca5b6006e00c7929d115e6cf Reviewed-on: https://go-review.googlesource.com/c/vuln/+/459036 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]> Run-TryBot: Zvonimir Pavlinovic <[email protected]>
1 parent 1bc6252 commit be1c52d

File tree

7 files changed

+89
-63
lines changed

7 files changed

+89
-63
lines changed

internal/goenv.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build !testmode
6+
// +build !testmode
7+
8+
package internal
9+
10+
import (
11+
"encoding/json"
12+
"os/exec"
13+
)
14+
15+
// GoEnv returns the value for key in `go env`. In
16+
// the unlikely case that `go env` fails, prints an
17+
// error message and returns an empty string.
18+
func GoEnv(key string) string {
19+
out, err := exec.Command("go", "env", "-json", key).Output()
20+
if err != nil {
21+
return ""
22+
}
23+
env := make(map[string]string)
24+
if err := json.Unmarshal(out, &env); err != nil {
25+
return ""
26+
}
27+
return env[key]
28+
}

internal/goenv_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package internal
6+
7+
import (
8+
"testing"
9+
)
10+
11+
func TestGoEnv(t *testing.T) {
12+
for _, key := range []string{"GOVERSION", "GOROOT", "GOPATH", "GOMODCACHE"} {
13+
if GoEnv(key) == "" {
14+
t.Errorf("want something for go env %s; got nothing", key)
15+
}
16+
}
17+
}

internal/goenv_testmode.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build testmode
6+
// +build testmode
7+
8+
package internal
9+
10+
import (
11+
"encoding/json"
12+
"os"
13+
"os/exec"
14+
)
15+
16+
// GoEnv returns the value for key in `go env`. In
17+
// the unlikely case that `go env` fails, prints an
18+
// error message and returns an empty string.
19+
//
20+
// For debugging and testing purposes, the value of
21+
// undocumented environment variable TEST_GOVERSION
22+
// is used for go env GOVERSION.
23+
func GoEnv(key string) string {
24+
out, err := exec.Command("go", "env", "-json", key).Output()
25+
if err != nil {
26+
return ""
27+
}
28+
env := make(map[string]string)
29+
if err := json.Unmarshal(out, &env); err != nil {
30+
return ""
31+
}
32+
33+
if v := os.Getenv("TEST_GOVERSION"); v != "" {
34+
// Unlikely to happen in practice, mostly used for testing.
35+
env["GOVERSION"] = v
36+
}
37+
38+
return env[key]
39+
}

internal/goversion.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

internal/goversion_testmode.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

internal/govulncheck/cache.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ package govulncheck
66

77
import (
88
"encoding/json"
9-
"go/build"
109
"os"
1110
"path/filepath"
1211
"sync"
1312
"time"
1413

1514
"golang.org/x/vuln/client"
15+
"golang.org/x/vuln/internal"
1616
"golang.org/x/vuln/osv"
1717
)
1818

@@ -22,7 +22,7 @@ import (
2222
// the index was retrieved from the vulnerability database. The JSON
2323
// format is as follows:
2424
//
25-
// $GOPATH/pkg/mod/cache/download/vulndb/{db hostname}/indexes/index.json
25+
// $GOMODCACHE/cache/download/vulndb/{db hostname}/indexes/index.json
2626
// {
2727
// Retrieved time.Time
2828
// Index client.DBIndex
@@ -31,7 +31,7 @@ import (
3131
// Each package also has a JSON file which contains the array of vulnerability
3232
// entries for the package. The JSON format is as follows:
3333
//
34-
// $GOPATH/pkg/mod/cache/download/vulndb/{db hostname}/{import path}/vulns.json
34+
// $GOMODCACHE/cache/download/vulndb/{db hostname}/{import path}/vulns.json
3535
// []*osv.Entry
3636

3737
// FSCache is a thread-safe file-system cache implementing osv.Cache
@@ -45,8 +45,7 @@ type FSCache struct {
4545
// Assert that *FSCache implements client.Cache.
4646
var _ client.Cache = (*FSCache)(nil)
4747

48-
// use cfg.GOMODCACHE available in cmd/go/internal?
49-
var defaultCacheRoot = filepath.Join(build.Default.GOPATH, "/pkg/mod/cache/download/vulndb")
48+
var defaultCacheRoot = filepath.Join(internal.GoEnv("GOMODCACHE"), "/cache/download/vulndb")
5049

5150
func DefaultCache() *FSCache {
5251
return &FSCache{rootDir: defaultCacheRoot}

vulncheck/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func Source(ctx context.Context, pkgs []*Package, cfg *Config) (_ *Result, err e
5151
if cfg.SourceGoVersion != "" {
5252
stdlibModule.Version = semver.GoTagToSemver(cfg.SourceGoVersion)
5353
} else {
54-
stdlibModule.Version = semver.GoTagToSemver(internal.GoVersion())
54+
stdlibModule.Version = semver.GoTagToSemver(internal.GoEnv("GOVERSION"))
5555
}
5656

5757
mods := extractModules(pkgs)

0 commit comments

Comments
 (0)