@@ -269,14 +269,14 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
269
269
return "" , fmt .Errorf ("could not create env for %s: %w" , ip , err )
270
270
}
271
271
272
- tmpDir , err := ioutil .TempDir ("" , "ko" )
273
- if err != nil {
274
- return "" , err
275
- }
272
+ tmpDir := ""
276
273
277
274
if dir := os .Getenv ("KOCACHE" ); dir != "" {
278
275
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
+ }
280
280
if err := os .MkdirAll (dir , os .ModePerm ); err != nil && ! os .IsExist (err ) {
281
281
return "" , fmt .Errorf ("could not create KOCACHE dir %s: %w" , dir , err )
282
282
}
@@ -287,6 +287,11 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
287
287
// TODO(#264): if KOCACHE is unset, default to filepath.Join(os.TempDir(), "ko").
288
288
tmpDir = filepath .Join (dir , "bin" , ip , platform .String ())
289
289
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 {
290
295
return "" , err
291
296
}
292
297
}
@@ -311,7 +316,7 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
311
316
os .RemoveAll (tmpDir )
312
317
}
313
318
log .Printf ("Unexpected error running \" go build\" : %v\n %v" , err , output .String ())
314
- return "" , err
319
+ return "" , fmt . Errorf ( "go build: %w" , err )
315
320
}
316
321
return file , nil
317
322
}
@@ -326,15 +331,15 @@ func goversionm(ctx context.Context, file string, appPath string, appFileName st
326
331
cmd .Stdout = sbom
327
332
cmd .Stderr = os .Stderr
328
333
if err := cmd .Run (); err != nil {
329
- return nil , "" , err
334
+ return nil , "" , fmt . Errorf ( "go version -m %s: %w" , file , err )
330
335
}
331
336
332
337
// In order to get deterministics SBOMs replace our randomized
333
338
// file name with the path the app will get inside of the container.
334
339
s := []byte (strings .Replace (sbom .String (), file , appPath , 1 ))
335
340
336
341
if err := writeSBOM (s , appFileName , dir , "go.version-m" ); err != nil {
337
- return nil , "" , err
342
+ return nil , "" , fmt . Errorf ( "writing sbom: %w" , err )
338
343
}
339
344
340
345
return s , "application/vnd.go.version-m" , nil
@@ -513,13 +518,13 @@ func tarBinary(name, binary string, platform *v1.Platform) (*bytes.Buffer, error
513
518
// 0444, or 0666, none of which are executable.
514
519
Mode : 0555 ,
515
520
}); 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 )
517
522
}
518
523
}
519
524
520
525
file , err := os .Open (binary )
521
526
if err != nil {
522
- return nil , err
527
+ return nil , fmt . Errorf ( "opening binary: %w" , err )
523
528
}
524
529
defer file .Close ()
525
530
stat , err := file .Stat ()
@@ -544,11 +549,11 @@ func tarBinary(name, binary string, platform *v1.Platform) (*bytes.Buffer, error
544
549
}
545
550
// write the header to the tarball archive
546
551
if err := tw .WriteHeader (header ); err != nil {
547
- return nil , err
552
+ return nil , fmt . Errorf ( "writing tar header: %w" , err )
548
553
}
549
554
// copy the file data to the tarball
550
555
if _ , err := io .Copy (tw , file ); err != nil {
551
- return nil , err
556
+ return nil , fmt . Errorf ( "copying file to tar: %w" , err )
552
557
}
553
558
554
559
return buf , nil
@@ -828,7 +833,7 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
828
833
// Do the build into a temporary file.
829
834
file , err := g .build (ctx , ref .Path (), g .dir , * platform , g .configForImportPath (ref .Path ()))
830
835
if err != nil {
831
- return nil , err
836
+ return nil , fmt . Errorf ( "build: %w" , err )
832
837
}
833
838
if os .Getenv ("KOCACHE" ) == "" {
834
839
defer os .RemoveAll (filepath .Dir (file ))
@@ -839,7 +844,7 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
839
844
// Create a layer from the kodata directory under this import path.
840
845
dataLayerBuf , err := g .tarKoData (ref , platform )
841
846
if err != nil {
842
- return nil , err
847
+ return nil , fmt . Errorf ( "tarring kodata: %w" , err )
843
848
}
844
849
dataLayerBytes := dataLayerBuf .Bytes ()
845
850
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
868
873
869
874
binaryLayer , err := g .cache .get (ctx , file , miss )
870
875
if err != nil {
871
- return nil , err
876
+ return nil , fmt . Errorf ( "cache.get(%q): %w" , file , err )
872
877
}
873
878
874
879
layers = append (layers , mutate.Addendum {
@@ -948,7 +953,7 @@ func buildLayer(appPath, file string, platform *v1.Platform, layerMediaType type
948
953
// Construct a tarball with the binary and produce a layer.
949
954
binaryLayerBuf , err := tarBinary (appPath , file , platform )
950
955
if err != nil {
951
- return nil , err
956
+ return nil , fmt . Errorf ( "tarring binary: %w" , err )
952
957
}
953
958
binaryLayerBytes := binaryLayerBuf .Bytes ()
954
959
return tarball .LayerFromOpener (func () (io.ReadCloser , error ) {
@@ -987,7 +992,7 @@ func (g *gobuild) Build(ctx context.Context, s string) (Result, error) {
987
992
// early, and we lazily use the ctx within ggcr's remote package.
988
993
baseRef , base , err := g .getBase (g .ctx , s )
989
994
if err != nil {
990
- return nil , err
995
+ return nil , fmt . Errorf ( "fetching base image: %w" , err )
991
996
}
992
997
993
998
// 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
1069
1074
// Build an image for each matching platform from the base and append
1070
1075
// it to a new index to produce the result. We use the indices to
1071
1076
// preserve the base image ordering here.
1072
- errg , ctx := errgroup .WithContext (ctx )
1077
+ errg , gctx := errgroup .WithContext (ctx )
1073
1078
adds := make ([]ocimutate.IndexAddendum , len (matches ))
1074
1079
for i , desc := range matches {
1075
1080
i , desc := i , desc
@@ -1101,7 +1106,7 @@ func (g *gobuild) buildAll(ctx context.Context, ref string, baseRef name.Referen
1101
1106
specsv1 .AnnotationBaseImageName : baseRef .Name (),
1102
1107
}).(v1.Image )
1103
1108
1104
- img , err := g .buildOne (ctx , ref , baseImage , desc .Platform )
1109
+ img , err := g .buildOne (gctx , ref , baseImage , desc .Platform )
1105
1110
if err != nil {
1106
1111
return err
1107
1112
}
0 commit comments