@@ -470,7 +470,17 @@ function write_env_usage(source_file::AbstractString, usage_filepath::AbstractSt
470470 while true
471471 # read existing usage file
472472 usage = if isfile (usage_file)
473- TOML. parsefile (usage_file)
473+ retries = 0
474+ @label retry1
475+ try
476+ # If the file existed, but disappears or is mangled during parse, keep trying and error if retries fail
477+ TOML. parsefile (usage_file)
478+ catch
479+ retries += 1
480+ retries > 5 && rethrow ()
481+ sleep (0.1 )
482+ @goto retry1
483+ end
474484 else
475485 Dict {String, Any} ()
476486 end
@@ -490,12 +500,24 @@ function write_env_usage(source_file::AbstractString, usage_filepath::AbstractSt
490500 TOML. print (io, usage, sorted= true )
491501 end
492502
493- # Move the temp file into place, replacing the original
494- mv (temp_usage_file, usage_file, force = true )
503+ # Create or overwrite the original (this is as fast as mv, but doesn't rm the original to overwrite)
504+ open (usage_file, " w" ) do io
505+ write (io, read (temp_usage_file, String))
506+ end
495507
496508 # Check that the new file has what we want in it
497509 new_usage = if isfile (usage_file)
498- TOML. parsefile (usage_file)
510+ retries = 0
511+ @label retry2
512+ try
513+ # If the file existed, but disappears or is mangled during parse, keep trying and error if retries fail
514+ TOML. parsefile (usage_file)
515+ catch
516+ retries += 1
517+ retries > 5 && rethrow ()
518+ sleep (0.1 )
519+ @goto retry2
520+ end
499521 else
500522 Dict {String, Any} ()
501523 end
0 commit comments