44 "context"
55 "errors"
66 "fmt"
7+ "io/fs"
78 "os"
89 "path/filepath"
910 "slices"
@@ -50,47 +51,45 @@ func (e *Executor) configureHTTPSInsteadOfSSH(ctx context.Context) error {
5051 ).Run (ctx , shell .ShowPrompt (false ))
5152}
5253
53- func (e * Executor ) removeCheckoutDir () error {
54+ func (e * Executor ) removeCheckoutDir (ctx context. Context ) error {
5455 checkoutPath , _ := e .shell .Env .Get ("BUILDKITE_BUILD_CHECKOUT_PATH" )
5556
5657 // on windows, sometimes removing large dirs can fail for various reasons
5758 // for instance having files open
5859 // see https://github.com/golang/go/issues/20841
59- for range 10 {
60+ return roko .NewRetrier (
61+ roko .WithMaxAttempts (10 ),
62+ roko .WithStrategy (roko .Constant (10 * time .Second )),
63+ ).DoWithContext (ctx , func (r * roko.Retrier ) error {
6064 e .shell .Commentf ("Removing %s" , checkoutPath )
61- if err := os .RemoveAll (checkoutPath ); err != nil {
62- e .shell .Errorf ("Failed to remove \" %s\" (%s)" , checkoutPath , err )
63- } else {
64- if _ , err := os .Stat (checkoutPath ); os .IsNotExist (err ) {
65- return nil
66- } else {
67- e .shell .Errorf ("Failed to remove %s" , checkoutPath )
68- }
69- }
70- e .shell .Commentf ("Waiting 10 seconds" )
71- <- time .After (time .Second * 10 )
72- }
7365
74- return fmt .Errorf ("failed to remove %s" , checkoutPath )
66+ if err := hardRemoveAll (checkoutPath ); err != nil {
67+ e .shell .Errorf ("Failed to remove %q (%s)" , checkoutPath , err )
68+ return err
69+ }
70+ if _ , err := os .Stat (checkoutPath ); err == nil || ! errors .Is (err , fs .ErrNotExist ) {
71+ e .shell .Errorf ("Failed to remove %q" , checkoutPath )
72+ return err
73+ }
74+ return nil
75+ })
7576}
7677
7778func (e * Executor ) createCheckoutDir () error {
7879 checkoutPath , _ := e .shell .Env .Get ("BUILDKITE_BUILD_CHECKOUT_PATH" )
7980
8081 if ! osutil .FileExists (checkoutPath ) {
8182 e .shell .Commentf ("Creating \" %s\" " , checkoutPath )
82- // Actual file permissions will be reduced by umask, and won't be 0o777 unless the user has manually changed the umask to 000
83- if err := os .MkdirAll (checkoutPath , 0o777 ); err != nil {
83+ // Actual file permissions will be reduced by umask, and won't be 0777
84+ // unless the user has manually changed the umask to 000
85+ if err := os .MkdirAll (checkoutPath , 0777 ); err != nil {
8486 return err
8587 }
8688 }
8789
8890 if e .shell .Getwd () != checkoutPath {
89- if err := e .shell .Chdir (checkoutPath ); err != nil {
90- return err
91- }
91+ return e .shell .Chdir (checkoutPath )
9292 }
93-
9493 return nil
9594}
9695
@@ -112,7 +111,7 @@ func (e *Executor) CheckoutPhase(ctx context.Context) error {
112111 // Remove the checkout directory if BUILDKITE_CLEAN_CHECKOUT is present
113112 if e .CleanCheckout {
114113 e .shell .Headerf ("Cleaning pipeline checkout" )
115- if err = e .removeCheckoutDir (); err != nil {
114+ if err = e .removeCheckoutDir (ctx ); err != nil {
116115 return err
117116 }
118117 }
@@ -202,7 +201,7 @@ func (e *Executor) CheckoutPhase(ctx context.Context) error {
202201 // Checkout can fail because of corrupted files in the checkout which can leave the agent in a state where it
203202 // keeps failing. This removes the checkout dir, which means the next checkout will be a lot slower (clone vs
204203 // fetch), but hopefully will allow the agent to self-heal
205- if err := e .removeCheckoutDir (); err != nil {
204+ if err := e .removeCheckoutDir (ctx ); err != nil {
206205 e .shell .Warningf ("Failed to remove checkout dir while cleaning up after a checkout error: %v" , err )
207206 }
208207
@@ -221,7 +220,7 @@ func (e *Executor) CheckoutPhase(ctx context.Context) error {
221220 e .shell .Warningf ("Checkout failed! %s (%s)" , err , r )
222221
223222 // If it's some kind of error that we don't know about, clean the checkout dir just to be safe
224- if err := e .removeCheckoutDir (); err != nil {
223+ if err := e .removeCheckoutDir (ctx ); err != nil {
225224 e .shell .Warningf ("Failed to remove checkout dir while cleaning up after a checkout error: %v" , err )
226225 }
227226
0 commit comments