From e1bad731d1777281dcbeac3429e69360b7e5bf09 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Fri, 16 Jun 2023 17:17:24 +0800 Subject: [PATCH] Added some quick'n'dirty cache invalidation to "wasmer publish" --- lib/cli/src/commands/publish.rs | 29 ++++++++++++++++++- lib/wasix/src/runtime/resolver/wapm_source.rs | 14 +++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/cli/src/commands/publish.rs b/lib/cli/src/commands/publish.rs index 307e686c83d..14aa1ef15a4 100644 --- a/lib/cli/src/commands/publish.rs +++ b/lib/cli/src/commands/publish.rs @@ -1,4 +1,7 @@ +use anyhow::Context; use clap::Parser; +use wasmer_registry::WasmerConfig; +use wasmer_wasix::runtime::resolver::WapmSource; /// Publish a package to the package registry. #[derive(Debug, Parser)] @@ -44,7 +47,16 @@ impl Publish { no_validate: self.no_validate, package_path: self.package_path.clone(), }; - publish.execute().map_err(on_error) + publish.execute().map_err(on_error)?; + + if let Err(e) = invalidate_graphql_query_cache() { + tracing::warn!( + error = &*e, + "Unable to invalidate the cache used for package version queries", + ); + } + + Ok(()) } } @@ -54,3 +66,18 @@ fn on_error(e: anyhow::Error) -> anyhow::Error { e } + +// HACK: We want to invalidate the cache used for GraphQL queries so +// the current user sees the results of publishing immediately. There +// are cleaner ways to achieve this, but for now we're just going to +// clear out the whole GraphQL query cache. +// See https://github.com/wasmerio/wasmer/pull/3983 for more +fn invalidate_graphql_query_cache() -> Result<(), anyhow::Error> { + let wasmer_dir = WasmerConfig::get_wasmer_dir() + .map_err(anyhow::Error::msg) + .context("Unable to determine the wasmer dir")?; + + let cache_dir = WapmSource::invalidate_local_cache(wasmer_dir)?; + + Ok(()) +} diff --git a/lib/wasix/src/runtime/resolver/wapm_source.rs b/lib/wasix/src/runtime/resolver/wapm_source.rs index 1ef69c148bf..71f7429b285 100644 --- a/lib/wasix/src/runtime/resolver/wapm_source.rs +++ b/lib/wasix/src/runtime/resolver/wapm_source.rs @@ -52,6 +52,20 @@ impl WapmSource { } } + /// Clean the local cache. + /// + /// This is a workaround used primarily when publishing and will probably + /// be removed in the future. + pub fn invalidate_local_cache(wasmer_dir: impl AsRef) -> Result<(), Error> { + let cache_dir = WapmSource::default_cache_dir(wasmer_dir); + + std::fs::remove_dir_all(cache_dir).with_context(|| { + format!("Unable to delete the \"{}\" directory", cache_dir.display()) + })?; + + Ok(()) + } + async fn lookup_package(&self, package_name: &str) -> Result { if let Some(cache) = &self.cache { match cache.lookup_cached_query(package_name) {