Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cmd/fetch_repo] make cache corruption failures more clear #1782

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions cmd/fetch_repo/copy_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ func copyTree(destRoot, srcRoot string) error {

if info.IsDir() {
return os.Mkdir(dest, 0o777)
} else {
r, err := os.Open(src)
if err != nil {
return err
}
defer r.Close()
w, err := os.Create(dest)
if err != nil {
return err
}
defer func() {
if cerr := w.Close(); err == nil && cerr != nil {
err = cerr
}
}()
_, err = io.Copy(w, r)
}

r, err := os.Open(src)
if err != nil {
return err
}
defer r.Close()
w, err := os.Create(dest)
if err != nil {
return err
}
defer func() {
if cerr := w.Close(); err == nil && cerr != nil {
err = cerr
}
}()
_, err = io.Copy(w, r)
return err
})
}
20 changes: 7 additions & 13 deletions cmd/fetch_repo/go_mod_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func findGoPath() string {
}

func runGoModDownload(dl *GoModDownloadResult, dest string, importpath string, version string) error {
buf := &bytes.Buffer{}
bufErr := &bytes.Buffer{}
buf := bytes.NewBuffer(nil)
bufErr := bytes.NewBuffer(nil)
cmd := exec.Command(findGoPath(), "mod", "download", "-json")
cmd.Dir = dest
cmd.Args = append(cmd.Args, "-modcacherw")
Expand All @@ -44,34 +44,28 @@ func runGoModDownload(dl *GoModDownloadResult, dest string, importpath string, v

cmd.Stdout = buf
cmd.Stderr = bufErr
fmt.Printf("Running: %s %s\n", cmd.Path, strings.Join(cmd.Args, " "))
fmeum marked this conversation as resolved.
Show resolved Hide resolved
dlErr := cmd.Run()
if dlErr != nil {
if _, ok := dlErr.(*exec.ExitError); !ok {
if bufErr.Len() > 0 {
return fmt.Errorf("!%s %s: %s", cmd.Path, strings.Join(cmd.Args, " "), bufErr.Bytes())
tyler-french marked this conversation as resolved.
Show resolved Hide resolved
} else {
return fmt.Errorf("!!%s %s: %v", cmd.Path, strings.Join(cmd.Args, " "), dlErr)
return fmt.Errorf("go mod download exec error: %s %q: %s, %w", cmd.Path, strings.Join(cmd.Args, " "), bufErr.String(), dlErr)
}
return fmt.Errorf("go mod download exec error: %s %s: %v", cmd.Path, strings.Join(cmd.Args, " "), dlErr)
}
}

// Parse the JSON output.
if err := json.Unmarshal(buf.Bytes(), &dl); err != nil {
if bufErr.Len() > 0 {
return fmt.Errorf("3%s %s: %s", cmd.Path, strings.Join(cmd.Args, " "), bufErr.Bytes())
tyler-french marked this conversation as resolved.
Show resolved Hide resolved
} else {
return fmt.Errorf("4%s %s: error parsing JSON: %v error: %v", cmd.Path, strings.Join(cmd.Args, " "), buf, err)
return fmt.Errorf("go mod download output format: `%s %s`: parsing JSON: %q stderr: %q error: %w", cmd.Path, strings.Join(cmd.Args, " "), buf.String(), bufErr.String(), err)
}
return fmt.Errorf("go mod download output format: `%s %s`: parsing JSON: %q error: %w", cmd.Path, strings.Join(cmd.Args, " "), buf.String(), err)
}
if dl.Error != "" {
return errors.New(dl.Error)
return errors.Join(errors.New(dl.Error), dlErr)
tyler-french marked this conversation as resolved.
Show resolved Hide resolved
}
if dlErr != nil {
return dlErr
}

fmt.Printf("Downloaded: %s\n", dl.Dir)

return nil
}
9 changes: 5 additions & 4 deletions cmd/fetch_repo/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,25 @@ func fetchModule(dest, importpath, version, sum string) error {
var dl = GoModDownloadResult{}
err = runGoModDownload(&dl, dest, importpath, version)
os.Remove("go.mod")

if err != nil {
return err
}

// Copy the module to the destination.
err = copyTree(dest, dl.Dir)
if err != nil {
if err := copyTree(dest, dl.Dir); err != nil {
return fmt.Errorf("failed copying repo: %w", err)
}

// Verify sum
// Verify sum of the directory itself against the go.sum.
repoSum, err := dirhash.HashDir(dest, importpath+"@"+version, dirhash.Hash1)
if err != nil {
return fmt.Errorf("failed computing sum: %w", err)
}

if repoSum != sum {
if goModCache := os.Getenv("GOMODCACHE"); goModCache != "" {
return fmt.Errorf("resulting module with sum %s; expected sum %s, Please try clearing your module cache directory %q", repoSum, sum, goModCache)
}
return fmt.Errorf("resulting module with sum %s; expected sum %s", repoSum, sum)
}

Expand Down