Skip to content

Commit a62112e

Browse files
author
Jay Conrod
committed
repo: fix module commands for Go 1.16 (#973)
* Fix a typo in the temporary module path: previously it ended with a literal '\n', which is not valid. * Add a 'go 1.15' declaration. * Run 'go get -d pkg' before 'go list pkg' since -mod=readonly is now the default. * Refactor error handling. Fixes #972
1 parent 9c1e61c commit a62112e

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

repo/remote.go

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package repo
1818
import (
1919
"bytes"
2020
"encoding/json"
21+
"errors"
2122
"fmt"
2223
"io/ioutil"
2324
"os"
@@ -337,11 +338,7 @@ func defaultHeadCmd(remote, vcs string) (string, error) {
337338
cmd := exec.Command("git", "ls-remote", remote, "HEAD")
338339
out, err := cmd.Output()
339340
if err != nil {
340-
var stdErr []byte
341-
if e, ok := err.(*exec.ExitError); ok {
342-
stdErr = e.Stderr
343-
}
344-
return "", fmt.Errorf("git ls-remote for %s : %v : %s", remote, err, stdErr)
341+
return "", fmt.Errorf("git ls-remote for %s: %v", remote, cleanCmdError(err))
345342
}
346343
ix := bytes.IndexByte(out, '\t')
347344
if ix < 0 {
@@ -414,18 +411,28 @@ func defaultModInfo(rc *RemoteCache, importPath string) (modPath string, err err
414411
if rc.tmpErr != nil {
415412
return "", rc.tmpErr
416413
}
414+
defer func() {
415+
if err != nil {
416+
err = fmt.Errorf("finding module path for import %s: %v", importPath, cleanCmdError(err))
417+
}
418+
}()
417419

418420
goTool := findGoTool()
419-
cmd := exec.Command(goTool, "list", "-find", "-f", "{{.Module.Path}}", "--", importPath)
421+
env := append(os.Environ(), "GO111MODULE=on")
422+
423+
cmd := exec.Command(goTool, "get", "-d", "--", importPath)
420424
cmd.Dir = rc.tmpDir
421-
cmd.Env = append(os.Environ(), "GO111MODULE=on")
425+
cmd.Env = env
426+
if _, err := cmd.Output(); err != nil {
427+
return "", err
428+
}
429+
430+
cmd = exec.Command(goTool, "list", "-find", "-f", "{{.Module.Path}}", "--", importPath)
431+
cmd.Dir = rc.tmpDir
432+
cmd.Env = env
422433
out, err := cmd.Output()
423434
if err != nil {
424-
var stdErr []byte
425-
if e, ok := err.(*exec.ExitError); ok {
426-
stdErr = e.Stderr
427-
}
428-
return "", fmt.Errorf("finding module path for import %s: %v: %s", importPath, err, stdErr)
435+
return "", fmt.Errorf("finding module path for import %s: %v", importPath, cleanCmdError(err))
429436
}
430437
return strings.TrimSpace(string(out)), nil
431438
}
@@ -472,24 +479,24 @@ func defaultModVersionInfo(rc *RemoteCache, modPath, query string) (version, sum
472479
if rc.tmpErr != nil {
473480
return "", "", rc.tmpErr
474481
}
482+
defer func() {
483+
if err != nil {
484+
err = fmt.Errorf("finding module version and sum for %s@%s: %v", modPath, query, cleanCmdError(err))
485+
}
486+
}()
475487

476488
goTool := findGoTool()
477489
cmd := exec.Command(goTool, "mod", "download", "-json", "--", modPath+"@"+query)
478490
cmd.Dir = rc.tmpDir
479491
cmd.Env = append(os.Environ(), "GO111MODULE=on")
480492
out, err := cmd.Output()
481493
if err != nil {
482-
var stdErr []byte
483-
if e, ok := err.(*exec.ExitError); ok {
484-
stdErr = e.Stderr
485-
}
486-
return "", "", fmt.Errorf("finding module version and sum for %s@%s: %v: %s", modPath, query, err, stdErr)
494+
return "", "", err
487495
}
488496

489497
var result struct{ Version, Sum string }
490498
if err := json.Unmarshal(out, &result); err != nil {
491-
fmt.Println(out)
492-
return "", "", fmt.Errorf("finding module version and sum for %s@%s: invalid output from 'go mod download': %v", modPath, query, err)
499+
return "", "", fmt.Errorf("invalid output from 'go mod download': %v", err)
493500
}
494501
return result.Version, result.Sum, nil
495502
}
@@ -538,7 +545,7 @@ func (rc *RemoteCache) initTmp() {
538545
if rc.tmpErr != nil {
539546
return
540547
}
541-
rc.tmpErr = ioutil.WriteFile(filepath.Join(rc.tmpDir, "go.mod"), []byte(`module gazelle_remote_cache__\n`), 0666)
548+
rc.tmpErr = ioutil.WriteFile(filepath.Join(rc.tmpDir, "go.mod"), []byte("module gazelle_remote_cache\ngo 1.15\n"), 0666)
542549
})
543550
}
544551

@@ -581,3 +588,18 @@ func findGoTool() string {
581588
}
582589
return path
583590
}
591+
592+
// cleanCmdError simplifies error messages from os/exec.Cmd.Run.
593+
// For ExitErrors, it trims and returns stderr. This is useful for go commands
594+
// that print well-formatted errors. By default, ExitError prints the exit
595+
// status but not stderr.
596+
//
597+
// cleanCmdError returns other errors unmodified.
598+
func cleanCmdError(err error) error {
599+
if xerr, ok := err.(*exec.ExitError); ok {
600+
if stderr := strings.TrimSpace(string(xerr.Stderr)); stderr != "" {
601+
return errors.New(stderr)
602+
}
603+
}
604+
return err
605+
}

0 commit comments

Comments
 (0)