Skip to content

Commit 4df323a

Browse files
authored
refactor: warn on output directory cleanup failure (#518)
- Adds `maxCleanupRetryAttempts` (5) and replaces the magic '5' in the cleanup loop with this constant. - Adds a log a warning if `dir.RemoveAll()` fails during `Run` instead of ignoring the error. These changes ensures that output directory removal failures are visible for debugging and the retry count is centralized. Signed-off-by: Ryan Johnson <ryan@tenthirtyam.org>
1 parent 9d43b6c commit 4df323a

1 file changed

Lines changed: 7 additions & 2 deletions

File tree

builder/vmware/common/step_output_dir.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import (
1414
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
1515
)
1616

17+
// maxCleanupRetryAttempts is the number of times cleanup retries output directory removal.
18+
const maxCleanupRetryAttempts = 5
19+
1720
// StepOutputDir manages the output directory configuration for a build step,
1821
// including forceful directory overwrite.
1922
type StepOutputDir struct {
@@ -65,7 +68,9 @@ func (s *StepOutputDir) Run(ctx context.Context, state multistep.StateBag) multi
6568
if exists {
6669
if s.Force {
6770
ui.Say("Deleting previous output directory...")
68-
_ = dir.RemoveAll()
71+
if err := dir.RemoveAll(); err != nil {
72+
log.Printf("[WARN] Failed to remove previous output dir %q: %s", dir.String(), err)
73+
}
6974
} else {
7075
state.Put("error", fmt.Errorf("output directory '%s' already exists", dir.String()))
7176
return multistep.ActionHalt
@@ -97,7 +102,7 @@ func (s *StepOutputDir) Cleanup(state multistep.StateBag) {
97102
exists, _ := dir.DirExists()
98103
if exists {
99104
ui.Say("Deleting output directory...")
100-
for range 5 {
105+
for range maxCleanupRetryAttempts {
101106
err := dir.RemoveAll()
102107
if err == nil {
103108
break

0 commit comments

Comments
 (0)