From 1975aa04de83b18fee1dfd7bcf7d9309a50714e7 Mon Sep 17 00:00:00 2001 From: David Allsopp Date: Thu, 22 Apr 2021 17:26:05 +0100 Subject: [PATCH 1/2] Add OpamPath.state_cache_dir --- src/format/opamPath.ml | 4 +++- src/format/opamPath.mli | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/format/opamPath.ml b/src/format/opamPath.ml index 8de398a84e7..3e16410e9e3 100644 --- a/src/format/opamPath.ml +++ b/src/format/opamPath.ml @@ -25,7 +25,9 @@ let init_config_files () = OpamFilename.Dir.of_string (OpamStd.Sys.home ()) // ".opamrc"; ] -let state_cache t = t / "repo" // "state.cache" +let state_cache_dir t = t / "repo" + +let state_cache t = state_cache_dir t // "state.cache" let lock t = t // "lock" diff --git a/src/format/opamPath.mli b/src/format/opamPath.mli index 0e49452c6ee..e151355dc26 100644 --- a/src/format/opamPath.mli +++ b/src/format/opamPath.mli @@ -21,6 +21,9 @@ type t = dirname (** State cache *) val state_cache: t -> filename +(** Directory containing state cache *) +val state_cache_dir: t -> dirname + (** Global lock file for the whole opamroot. Opam should generally read-lock this (e.g. initialisation and format upgrades require a write lock) *) val lock: t -> filename From 2d35b75fa3dc858ba9ae48f89321e1d84cef750e Mon Sep 17 00:00:00 2001 From: David Allsopp Date: Thu, 22 Apr 2021 17:28:47 +0100 Subject: [PATCH 2/2] Make state.cache unique to version Instead of writing state.cache, opam now writes state-magic.cache which means that two opam binaries / libraries no longer keep regenerating the state cache just because they can't read the one which was there before. Just prior to _writing_ the state cache (e.g. after opam update or any repo operation), all the .cache files in the directory are erased. opam 2.0 is either unable to read a 2.1 root or will never write to it (opam 2.0.9), so this change is backwards compatible. --- master_changes.md | 4 +++- src/format/opamPath.ml | 2 +- src/state/opamRepositoryState.ml | 15 ++++++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/master_changes.md b/master_changes.md index 828d0556e47..99250aca912 100644 --- a/master_changes.md +++ b/master_changes.md @@ -111,7 +111,9 @@ New option/command/subcommand are prefixed with ◈. * ## State - * + * Rename state.cache to include the OpamVersion.magic() string. All .cache files are deleted if any + cache file is written to, allowing multiple versions of the library to co-exist without constantly + regenerating it [#4642 @dra27 - fix #4554] # Opam file format * diff --git a/src/format/opamPath.ml b/src/format/opamPath.ml index 3e16410e9e3..06a1b544f30 100644 --- a/src/format/opamPath.ml +++ b/src/format/opamPath.ml @@ -27,7 +27,7 @@ let init_config_files () = let state_cache_dir t = t / "repo" -let state_cache t = state_cache_dir t // "state.cache" +let state_cache t = state_cache_dir t // Printf.sprintf "state-%s.cache" (OpamVersion.magic ()) let lock t = t // "lock" diff --git a/src/state/opamRepositoryState.ml b/src/state/opamRepositoryState.ml index 7a550a42164..883063fd04d 100644 --- a/src/state/opamRepositoryState.ml +++ b/src/state/opamRepositoryState.ml @@ -27,6 +27,15 @@ module Cache = struct let name = "repository" end) + let remove () = + let root = OpamStateConfig.(!r.root_dir) in + let cache_dir = OpamPath.state_cache_dir root in + let remove_cache_file file = + if OpamFilename.check_suffix file ".cache" then + OpamFilename.remove file + in + List.iter remove_cache_file (OpamFilename.files cache_dir) + let save rt = let file = OpamPath.state_cache rt.repos_global.root in (* Repository without remote are not cached, they are intended to be @@ -49,6 +58,7 @@ module Cache = struct (filter_out_nourl rt.repo_opams); } in + remove (); C.save file t let load root = @@ -60,11 +70,6 @@ module Cache = struct OpamRepositoryName.Map.of_list cache.cached_opams) | None -> None - let remove () = - let root = OpamStateConfig.(!r.root_dir) in - let file = OpamPath.state_cache root in - C.remove file - end let load_opams_from_dir repo_name repo_root =