Skip to content

Commit 1accda0

Browse files
committed
estimate: Use the default GOPATH to cache downloads
This avoids re-downloading the same modules (potentially hundreds of megabytes for moderately sized binaries) over and over. This makes `progressSize` not useful, so `go get`'s stdout and stderr are passed through and `-x` is passed to `go get` to get some progress indication.
1 parent 6eec767 commit 1accda0

File tree

1 file changed

+16
-41
lines changed

1 file changed

+16
-41
lines changed

estimate.go

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ func getSourcesInNew() (map[string]string, error) {
5454
return sourcesInNew, nil
5555
}
5656

57-
func get(gopath, repodir, repo, rev string) error {
57+
func get(repodir, repo, rev string) error {
5858
done := make(chan struct{})
5959
defer close(done)
60-
go progressSize("go get", gopath, done)
6160

6261
// As per https://groups.google.com/forum/#!topic/golang-nuts/N5apfenE4m4,
6362
// the arguments to “go get” are packages, not repositories. Hence, we
@@ -70,43 +69,31 @@ func get(gopath, repodir, repo, rev string) error {
7069
if rev != "" {
7170
packages += "@" + rev
7271
}
73-
cmd := exec.Command("go", "get", "-t", packages)
72+
cmd := exec.Command("go", "get", "-x", "-t", packages)
7473

75-
out := bytes.Buffer{}
7674
cmd.Dir = repodir
77-
cmd.Stderr = &out
78-
cmd.Env = append([]string{
79-
"GOPATH=" + gopath,
80-
}, passthroughEnv()...)
81-
err := cmd.Run()
82-
if err != nil {
83-
fmt.Fprint(os.Stderr, "\n", out.String())
84-
}
85-
return err
75+
cmd.Stderr = os.Stderr
76+
cmd.Stdout = os.Stdout
77+
return cmd.Run()
8678
}
8779

88-
// getModuleDir returns the path of the directory containing a module for the
89-
// given GOPATH and repository dir values.
90-
func getModuleDir(gopath, repodir, module string) (string, error) {
80+
// getModuleDir returns the path of the directory containing a module for the given repository dir.
81+
func getModuleDir(repodir, module string) (string, error) {
9182
cmd := exec.Command("go", "list", "-m", "-f", "{{.Dir}}", module)
9283
cmd.Dir = repodir
9384
cmd.Stderr = os.Stderr
94-
cmd.Env = append([]string{
95-
"GOPATH=" + gopath,
96-
}, passthroughEnv()...)
9785
out, err := cmd.Output()
9886
if err != nil {
9987
return "", fmt.Errorf("go list: args: %v; error: %w", cmd.Args, err)
10088
}
10189
return string(bytes.TrimSpace(out)), nil
10290
}
10391

104-
// getDirectDependencies returns a set of all the direct dependencies of a
105-
// module for the given GOPATH and repository dir values. It first finds the
106-
// directory that contains this module, then uses go list in this directory
107-
// to get its direct dependencies.
108-
func getDirectDependencies(gopath, repodir, module string) (map[string]bool, error) {
109-
dir, err := getModuleDir(gopath, repodir, module)
92+
// getDirectDependencies returns a set of all the direct dependencies of a module for the given
93+
// repository dir. It first finds the directory that contains this module, then uses go list in this
94+
// directory to get its direct dependencies.
95+
func getDirectDependencies(repodir, module string) (map[string]bool, error) {
96+
dir, err := getModuleDir(repodir, module)
11097
if err != nil {
11198
return nil, fmt.Errorf("get module dir: %w", err)
11299
}
@@ -119,9 +106,6 @@ func getDirectDependencies(gopath, repodir, module string) (map[string]bool, err
119106
cmd := exec.Command("go", "list", "-m", "-f", "{{if not .Indirect}}{{.Path}}{{end}}", "all")
120107
cmd.Dir = dir
121108
cmd.Stderr = os.Stderr
122-
cmd.Env = append([]string{
123-
"GOPATH=" + gopath,
124-
}, passthroughEnv()...)
125109
out, err := cmd.Output()
126110
if err != nil {
127111
return nil, fmt.Errorf("go list: args: %v; error: %w", cmd.Args, err)
@@ -206,13 +190,7 @@ func estimate(importpath, revision string) error {
206190
}
207191
}
208192

209-
// construct a separate GOPATH in a temporary directory
210-
gopath, err := os.MkdirTemp("", "dh-make-golang")
211-
if err != nil {
212-
return fmt.Errorf("create temp dir: %w", err)
213-
}
214-
defer removeTemp(gopath)
215-
// second temporary directosy for the repo sources
193+
// temporary directory for the repo sources
216194
repodir, err := os.MkdirTemp("", "dh-make-golang")
217195
if err != nil {
218196
return fmt.Errorf("create temp dir: %w", err)
@@ -225,7 +203,7 @@ func estimate(importpath, revision string) error {
225203
return fmt.Errorf("create dummymod: %w", err)
226204
}
227205

228-
if err := get(gopath, repodir, importpath, revision); err != nil {
206+
if err := get(repodir, importpath, revision); err != nil {
229207
return fmt.Errorf("go get: %w", err)
230208
}
231209

@@ -236,7 +214,7 @@ func estimate(importpath, revision string) error {
236214

237215
if found {
238216
// Fetch un-vendored dependencies
239-
if err := get(gopath, repodir, importpath, revision); err != nil {
217+
if err := get(repodir, importpath, revision); err != nil {
240218
return fmt.Errorf("fetch un-vendored: go get: %w", err)
241219
}
242220
}
@@ -245,16 +223,13 @@ func estimate(importpath, revision string) error {
245223
cmd := exec.Command("go", "mod", "graph")
246224
cmd.Dir = repodir
247225
cmd.Stderr = os.Stderr
248-
cmd.Env = append([]string{
249-
"GOPATH=" + gopath,
250-
}, passthroughEnv()...)
251226
out, err := cmd.Output()
252227
if err != nil {
253228
return fmt.Errorf("go mod graph: args: %v; error: %w", cmd.Args, err)
254229
}
255230

256231
// Get direct dependencies, to filter out indirect ones from go mod graph output
257-
directDeps, err := getDirectDependencies(gopath, repodir, importpath)
232+
directDeps, err := getDirectDependencies(repodir, importpath)
258233
if err != nil {
259234
return fmt.Errorf("get direct dependencies: %w", err)
260235
}

0 commit comments

Comments
 (0)