@@ -15,6 +15,7 @@ import ..Pkg, ..Registry
1515import .. Pkg: GitTools, depots, depots1, logdir, set_readonly, safe_realpath, pkg_server, stdlib_dir, stdlib_path, isurl, stderr_f
1616import Base. BinaryPlatforms: Platform
1717using .. Pkg. Versions
18+ import .. Pkg. Pidfile
1819
1920import Base: SHA1
2021using SHA
@@ -477,16 +478,31 @@ function write_env_usage(source_file::AbstractString, usage_filepath::AbstractSt
477478 # Ensure that log dir exists
478479 ! ispath (logdir ()) && mkpath (logdir ())
479480
480- # Generate entire entry as a string first
481- entry = sprint () do io
482- TOML. print (io, Dict (source_file => [Dict (" time" => now ())]))
483- end
484-
485- # Append entry to log file in one chunk
486481 usage_file = joinpath (logdir (), usage_filepath)
487- open (usage_file, append= true ) do io
488- write (io, entry)
482+ timestamp = now ()
483+
484+ # # Atomically write usage file using process id locking
485+ Pidfile. mkpidlock (usage_file * " .pidlock" ) do
486+ usage = if isfile (usage_file)
487+ TOML. parsefile (usage_file)
488+ else
489+ Dict {String, Any} ()
490+ end
491+
492+ # record new usage
493+ usage[source_file] = [Dict (" time" => timestamp)]
494+
495+ # keep only latest usage info
496+ for k in keys (usage)
497+ times = map (d -> Dates. DateTime (d[" time" ]), usage[k])
498+ usage[k] = [Dict (" time" => maximum (times))]
499+ end
500+
501+ open (usage_file, " w" ) do io
502+ TOML. print (io, usage, sorted= true )
503+ end
489504 end
505+ return
490506end
491507
492508function read_package (path:: String )
0 commit comments