Skip to content
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
19 changes: 14 additions & 5 deletions core/bazel/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package bazel

import (
"context"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -113,7 +114,7 @@ func detectBazelExecutable(ctx context.Context, bazelCommand string) (string, er
}

// ensureBazelisk returns the path to a cached bazelisk binary,
func ensureBazelisk(ctx context.Context) (string, error) {
func ensureBazelisk(ctx context.Context) (_ string, retErr error) {
cacheDir, err := os.UserCacheDir()
if err != nil {
return "", fmt.Errorf("cache dir: %w", err)
Expand Down Expand Up @@ -148,13 +149,21 @@ func ensureBazelisk(ctx context.Context) (string, error) {
return "", fmt.Errorf("create temp file: %w", err)
}
tmpPath := tmp.Name()
defer os.Remove(tmpPath)
// On failure, remove the temp file and fold any removal error into the
// returned error. On the success path the file has been renamed into
// place, so there is nothing to remove.
defer func() {
if retErr != nil {
retErr = errors.Join(retErr, os.Remove(tmpPath))
}
}()

if _, err := io.Copy(tmp, resp.Body); err != nil {
tmp.Close()
return "", fmt.Errorf("write bazelisk: %w", err)
return "", fmt.Errorf("write bazelisk: %w", errors.Join(err, tmp.Close()))
}
if err := tmp.Close(); err != nil {
return "", fmt.Errorf("close bazelisk temp: %w", err)
}
tmp.Close()
if err := os.Chmod(tmpPath, 0o755); err != nil {
return "", fmt.Errorf("chmod bazelisk: %w", err)
}
Expand Down
6 changes: 5 additions & 1 deletion example/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ func main() {

grpcTransport := yarpcgrpc.NewTransport()
out := grpcTransport.NewSingleOutbound(*addr)
zl, _ := zap.NewDevelopment()
zl, err := zap.NewDevelopment()
if err != nil {
fmt.Fprintf(os.Stderr, "init logger: %v\n", err)
os.Exit(1)
}
defer zl.Sync()
logger := zl.Sugar()
dispatcher := yarpc.NewDispatcher(yarpc.Config{
Expand Down
5 changes: 4 additions & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func main() {
}

func run() error {
zl, _ := zap.NewDevelopment()
zl, err := zap.NewDevelopment()
if err != nil {
return fmt.Errorf("init logger: %w", err)
}
defer zl.Sync()
logger := zl.Sugar()

Expand Down
Loading