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

fix: detect when go mod tidy needs to be run, and retry #145

Merged
merged 1 commit into from
Apr 1, 2024
Merged
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
22 changes: 20 additions & 2 deletions internal/gobin/gobin.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,34 @@ func buildRepository(ctx context.Context, rootPath, buildDirPath, buildPath stri
string(filepath.Separator))
}

output, err := goBuild(ctx, binPath, buildDirPath, buildPath)
if err != nil {
fmt.Fprintln(os.Stderr, output)
if strings.Contains(output, "updates to go.mod needed") {
cmd := exec.CommandContext(ctx, "go", "mod", "tidy")
cmd.Dir = buildDirPath
if err := cmd.Run(); err != nil {
return errors.Wrap(err, "failed to run `go mod tidy`")
}
fmt.Fprintln(os.Stderr, "Automatically ran `go mod tidy`, retrying")
output, err = goBuild(ctx, binPath, buildDirPath, buildPath)
fmt.Fprintln(os.Stderr, output)
}
}
return err
}

// goBuild runs `go build` in buildDirPath.
func goBuild(ctx context.Context, binPath, buildDirPath, buildPath string) (string, error) {
args := []string{"build", "-o", binPath, buildPath}
cmd := exec.CommandContext(ctx, "go", args...)
cmd.Dir = buildDirPath

b, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("ran command \"go %s\"\n", strings.Join(args, " "))
fmt.Fprintln(os.Stderr, string(b))
}
return err
return string(b), err
}

// generateToolVersions generates a .tool-versions off of the output of asdf current
Expand Down