Skip to content

Commit 6a1169b

Browse files
authored
Add context to many gobuild errors (#1016)
1 parent ba63655 commit 6a1169b

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

pkg/build/cache.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strings"
2727
"sync"
2828

29+
"github.com/google/go-containerregistry/pkg/logs"
2930
v1 "github.com/google/go-containerregistry/pkg/v1"
3031
"github.com/google/go-containerregistry/pkg/v1/partial"
3132
)
@@ -53,12 +54,14 @@ func (c *layerCache) get(ctx context.Context, file string, miss layerFactory) (v
5354
desc: *desc,
5455
buildLayer: miss,
5556
}, nil
57+
} else {
58+
logs.Debug.Printf("getMeta(%q): %v", file, err)
5659
}
5760

5861
// Cache miss.
5962
layer, err := miss()
6063
if err != nil {
61-
return nil, err
64+
return nil, fmt.Errorf("miss(%q): %w", file, err)
6265
}
6366
if err := c.put(ctx, file, layer); err != nil {
6467
log.Printf("failed to cache metadata %s: %v", file, err)
@@ -73,7 +76,7 @@ func (c *layerCache) getMeta(ctx context.Context, file string) (*v1.Hash, *v1.De
7376
}
7477

7578
if buildid == "" {
76-
return nil, nil, fmt.Errorf("no buildid for %s", file)
79+
return nil, nil, fmt.Errorf("no buildid for %q", file)
7780
}
7881

7982
// TODO: Implement better per-file locking.
@@ -137,13 +140,13 @@ func (c *layerCache) put(ctx context.Context, file string, layer v1.Layer) error
137140

138141
btodf, err := os.OpenFile(filepath.Join(filepath.Dir(file), "buildid-to-diffid"), os.O_RDWR|os.O_CREATE, 0755)
139142
if err != nil {
140-
return err
143+
return fmt.Errorf("opening buildid-to-diffid: %w", err)
141144
}
142145
defer btodf.Close()
143146

144147
dtodf, err := os.OpenFile(filepath.Join(filepath.Dir(file), "diffid-to-descriptor"), os.O_RDWR|os.O_CREATE, 0755)
145148
if err != nil {
146-
return err
149+
return fmt.Errorf("opening diffid-to-descriptor: %w", err)
147150
}
148151
defer dtodf.Close()
149152

@@ -169,7 +172,7 @@ func (c *layerCache) readDiffToDesc(file string) (diffIDToDescriptor, error) {
169172

170173
dtodf, err := os.Open(filepath.Join(filepath.Dir(file), "diffid-to-descriptor"))
171174
if err != nil {
172-
return nil, err
175+
return nil, fmt.Errorf("opening diffid-to-descriptor: %w", err)
173176
}
174177
defer dtodf.Close()
175178

@@ -188,7 +191,7 @@ func (c *layerCache) readBuildToDiff(file string) (buildIDToDiffID, error) {
188191

189192
btodf, err := os.Open(filepath.Join(filepath.Dir(file), "buildid-to-diffid"))
190193
if err != nil {
191-
return nil, err
194+
return nil, fmt.Errorf("opening buildid-to-diffid: %w", err)
192195
}
193196
defer btodf.Close()
194197

@@ -210,7 +213,7 @@ func getBuildID(ctx context.Context, file string) (string, error) {
210213

211214
if err := cmd.Run(); err != nil {
212215
log.Printf("Unexpected error running \"go tool buildid %s\": %v\n%v", err, file, output.String())
213-
return "", err
216+
return "", fmt.Errorf("go tool buildid %s: %w", file, err)
214217
}
215218
return strings.TrimSpace(output.String()), nil
216219
}

pkg/build/gobuild.go

+24-19
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,14 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
269269
return "", fmt.Errorf("could not create env for %s: %w", ip, err)
270270
}
271271

272-
tmpDir, err := ioutil.TempDir("", "ko")
273-
if err != nil {
274-
return "", err
275-
}
272+
tmpDir := ""
276273

277274
if dir := os.Getenv("KOCACHE"); dir != "" {
278275
dirInfo, err := os.Stat(dir)
279-
if os.IsNotExist(err) {
276+
if err != nil {
277+
if !os.IsNotExist(err) {
278+
return "", fmt.Errorf("could not stat KOCACHE: %w", err)
279+
}
280280
if err := os.MkdirAll(dir, os.ModePerm); err != nil && !os.IsExist(err) {
281281
return "", fmt.Errorf("could not create KOCACHE dir %s: %w", dir, err)
282282
}
@@ -287,6 +287,11 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
287287
// TODO(#264): if KOCACHE is unset, default to filepath.Join(os.TempDir(), "ko").
288288
tmpDir = filepath.Join(dir, "bin", ip, platform.String())
289289
if err := os.MkdirAll(tmpDir, os.ModePerm); err != nil {
290+
return "", fmt.Errorf("creating KOCACHE bin dir: %w", err)
291+
}
292+
} else {
293+
tmpDir, err = ioutil.TempDir("", "ko")
294+
if err != nil {
290295
return "", err
291296
}
292297
}
@@ -311,7 +316,7 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
311316
os.RemoveAll(tmpDir)
312317
}
313318
log.Printf("Unexpected error running \"go build\": %v\n%v", err, output.String())
314-
return "", err
319+
return "", fmt.Errorf("go build: %w", err)
315320
}
316321
return file, nil
317322
}
@@ -326,15 +331,15 @@ func goversionm(ctx context.Context, file string, appPath string, appFileName st
326331
cmd.Stdout = sbom
327332
cmd.Stderr = os.Stderr
328333
if err := cmd.Run(); err != nil {
329-
return nil, "", err
334+
return nil, "", fmt.Errorf("go version -m %s: %w", file, err)
330335
}
331336

332337
// In order to get deterministics SBOMs replace our randomized
333338
// file name with the path the app will get inside of the container.
334339
s := []byte(strings.Replace(sbom.String(), file, appPath, 1))
335340

336341
if err := writeSBOM(s, appFileName, dir, "go.version-m"); err != nil {
337-
return nil, "", err
342+
return nil, "", fmt.Errorf("writing sbom: %w", err)
338343
}
339344

340345
return s, "application/vnd.go.version-m", nil
@@ -513,13 +518,13 @@ func tarBinary(name, binary string, platform *v1.Platform) (*bytes.Buffer, error
513518
// 0444, or 0666, none of which are executable.
514519
Mode: 0555,
515520
}); err != nil {
516-
return nil, fmt.Errorf("writing dir %q: %w", dir, err)
521+
return nil, fmt.Errorf("writing dir %q to tar: %w", dir, err)
517522
}
518523
}
519524

520525
file, err := os.Open(binary)
521526
if err != nil {
522-
return nil, err
527+
return nil, fmt.Errorf("opening binary: %w", err)
523528
}
524529
defer file.Close()
525530
stat, err := file.Stat()
@@ -544,11 +549,11 @@ func tarBinary(name, binary string, platform *v1.Platform) (*bytes.Buffer, error
544549
}
545550
// write the header to the tarball archive
546551
if err := tw.WriteHeader(header); err != nil {
547-
return nil, err
552+
return nil, fmt.Errorf("writing tar header: %w", err)
548553
}
549554
// copy the file data to the tarball
550555
if _, err := io.Copy(tw, file); err != nil {
551-
return nil, err
556+
return nil, fmt.Errorf("copying file to tar: %w", err)
552557
}
553558

554559
return buf, nil
@@ -828,7 +833,7 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
828833
// Do the build into a temporary file.
829834
file, err := g.build(ctx, ref.Path(), g.dir, *platform, g.configForImportPath(ref.Path()))
830835
if err != nil {
831-
return nil, err
836+
return nil, fmt.Errorf("build: %w", err)
832837
}
833838
if os.Getenv("KOCACHE") == "" {
834839
defer os.RemoveAll(filepath.Dir(file))
@@ -839,7 +844,7 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
839844
// Create a layer from the kodata directory under this import path.
840845
dataLayerBuf, err := g.tarKoData(ref, platform)
841846
if err != nil {
842-
return nil, err
847+
return nil, fmt.Errorf("tarring kodata: %w", err)
843848
}
844849
dataLayerBytes := dataLayerBuf.Bytes()
845850
dataLayer, err := tarball.LayerFromOpener(func() (io.ReadCloser, error) {
@@ -868,7 +873,7 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
868873

869874
binaryLayer, err := g.cache.get(ctx, file, miss)
870875
if err != nil {
871-
return nil, err
876+
return nil, fmt.Errorf("cache.get(%q): %w", file, err)
872877
}
873878

874879
layers = append(layers, mutate.Addendum{
@@ -948,7 +953,7 @@ func buildLayer(appPath, file string, platform *v1.Platform, layerMediaType type
948953
// Construct a tarball with the binary and produce a layer.
949954
binaryLayerBuf, err := tarBinary(appPath, file, platform)
950955
if err != nil {
951-
return nil, err
956+
return nil, fmt.Errorf("tarring binary: %w", err)
952957
}
953958
binaryLayerBytes := binaryLayerBuf.Bytes()
954959
return tarball.LayerFromOpener(func() (io.ReadCloser, error) {
@@ -987,7 +992,7 @@ func (g *gobuild) Build(ctx context.Context, s string) (Result, error) {
987992
// early, and we lazily use the ctx within ggcr's remote package.
988993
baseRef, base, err := g.getBase(g.ctx, s)
989994
if err != nil {
990-
return nil, err
995+
return nil, fmt.Errorf("fetching base image: %w", err)
991996
}
992997

993998
// Determine what kind of base we have and if we should publish an image or an index.
@@ -1069,7 +1074,7 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseRef name.Referen
10691074
// Build an image for each matching platform from the base and append
10701075
// it to a new index to produce the result. We use the indices to
10711076
// preserve the base image ordering here.
1072-
errg, ctx := errgroup.WithContext(ctx)
1077+
errg, gctx := errgroup.WithContext(ctx)
10731078
adds := make([]ocimutate.IndexAddendum, len(matches))
10741079
for i, desc := range matches {
10751080
i, desc := i, desc
@@ -1101,7 +1106,7 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseRef name.Referen
11011106
specsv1.AnnotationBaseImageName: baseRef.Name(),
11021107
}).(v1.Image)
11031108

1104-
img, err := g.buildOne(ctx, ref, baseImage, desc.Platform)
1109+
img, err := g.buildOne(gctx, ref, baseImage, desc.Platform)
11051110
if err != nil {
11061111
return err
11071112
}

0 commit comments

Comments
 (0)