Detect failure to access os.TempDir

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof
2025-10-10 13:46:51 +02:00
committed by Guillaume Lours
parent 2681ed17a7
commit 27f59d7f42
2 changed files with 18 additions and 7 deletions

2
go.mod
View File

@@ -24,6 +24,7 @@ require (
github.com/fsnotify/fsevents v0.2.0
github.com/go-viper/mapstructure/v2 v2.4.0
github.com/google/go-cmp v0.7.0
github.com/google/uuid v1.6.0
github.com/hashicorp/go-version v1.7.0
github.com/jonboulle/clockwork v0.5.0
github.com/mattn/go-shellwords v1.0.12
@@ -108,7 +109,6 @@ require (
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.26.1 // indirect

View File

@@ -25,7 +25,7 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"io/fs"
"os"
"os/exec"
"path/filepath"
@@ -40,6 +40,7 @@ import (
"github.com/docker/cli/cli/command/image/build"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/progress"
"github.com/google/uuid"
"github.com/moby/buildkit/client"
gitutil "github.com/moby/buildkit/frontend/dockerfile/dfgitutil"
"github.com/moby/buildkit/util/progress/progressui"
@@ -282,13 +283,20 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
}
logrus.Debugf("bake build config:\n%s", string(b))
tmpdir := os.TempDir()
var metadataFile string
for {
// we don't use os.CreateTemp here as we need a temporary file name, but don't want it actually created
// as bake relies on atomicwriter and this creates conflict during rename
metadataFile = filepath.Join(os.TempDir(), fmt.Sprintf("compose-build-metadataFile-%d.json", rand.Int31()))
if _, err = os.Stat(metadataFile); os.IsNotExist(err) {
break
metadataFile = filepath.Join(tmpdir, fmt.Sprintf("compose-build-metadataFile-%s.json", uuid.New().String()))
if _, err = os.Stat(metadataFile); err != nil {
if os.IsNotExist(err) {
break
}
var pathError *fs.PathError
if errors.As(err, &pathError) {
return nil, fmt.Errorf("can't acces os.tempDir %s: %w", tmpdir, pathError.Err)
}
}
}
defer func() {
@@ -361,9 +369,12 @@ func (s *composeService) doBuildBake(ctx context.Context, project *types.Project
if readErr != nil {
if readErr == io.EOF {
break
} else {
return nil, fmt.Errorf("failed to execute bake: %w", readErr)
}
if errors.Is(readErr, os.ErrClosed) {
logrus.Debugf("bake stopped")
break
}
return nil, fmt.Errorf("failed to execute bake: %w", readErr)
}
decoder := json.NewDecoder(strings.NewReader(line))
var status client.SolveStatus