diff --git a/src/internal/packager/sbom/catalog.go b/src/internal/packager/sbom/catalog.go index cb336944da..d23f7376ad 100644 --- a/src/internal/packager/sbom/catalog.go +++ b/src/internal/packager/sbom/catalog.go @@ -141,7 +141,10 @@ func Catalog(componentSBOMs map[string]*types.ComponentSBOM, imgList []string, t return err } - _ = os.RemoveAll(builder.tmpSBOMPath) + if err := utils.RetryRemoveAll(builder.tmpSBOMPath, 3); err != nil { + return fmt.Errorf("unable to remove the temporary SBOM directory: %w", err) + } + builder.spinner.Success() return nil diff --git a/src/pkg/packager/create.go b/src/pkg/packager/create.go index ffcaea32aa..4d0b2acf09 100644 --- a/src/pkg/packager/create.go +++ b/src/pkg/packager/create.go @@ -191,8 +191,8 @@ func (p *Packager) Create(baseDir string) error { } // Remove the deflated component directory - if err := os.RemoveAll(componentPaths.Base); err != nil { - message.Debugf("unable to remove the component directory (%s): %s", componentPaths.Base, err.Error()) + if err := utils.RetryRemoveAll(componentPaths.Base, 3); err != nil { + return fmt.Errorf("unable to remove the component directory (%s): %w", componentPaths.Base, err) } } diff --git a/src/pkg/utils/io.go b/src/pkg/utils/io.go index 14b0f4eb33..48c6c1ae97 100644 --- a/src/pkg/utils/io.go +++ b/src/pkg/utils/io.go @@ -17,6 +17,7 @@ import ( "path/filepath" "regexp" "strings" + "time" "github.com/defenseunicorns/zarf/src/pkg/message" "github.com/otiai10/copy" @@ -253,3 +254,18 @@ func GetDirSize(path string) (int64, error) { return dirSize, err } + +// RetryRemoveAll will attempt to remove an item or directory up to the given number of retries. +// NOTE: This function is necessary because of a Windows bug with removing files that have been recently used: https://github.com/golang/go/issues/51442 +func RetryRemoveAll(path string, retries int) error { + for i := 0; i < retries; i++ { + err := os.RemoveAll(path) + if err == nil { + return nil + } + + time.Sleep(500 * time.Millisecond) + } + + return os.RemoveAll(path) +}