diff --git a/lib/cli/src/commands/run/wasi.rs b/lib/cli/src/commands/run/wasi.rs index 62cc6f3a6d8..4ab6cda58b6 100644 --- a/lib/cli/src/commands/run/wasi.rs +++ b/lib/cli/src/commands/run/wasi.rs @@ -8,6 +8,7 @@ use anyhow::{Context, Result}; use bytes::Bytes; use clap::Parser; use tokio::runtime::Handle; +use url::Url; use virtual_fs::{DeviceFile, FileSystem, PassthruFileSystem, RootFileSystemBuilder}; use wasmer::{Engine, Function, Instance, Memory32, Memory64, Module, RuntimeError, Store, Value}; use wasmer_wasix::{ @@ -110,6 +111,10 @@ pub struct Wasi { /// Require WASI modules to only import 1 version of WASI. #[clap(long = "deny-multiple-wasi-versions")] pub deny_multiple_wasi_versions: bool, + + /// The registry to use. + #[clap(long, env = "WASMER_REGISTRY", value_parser = parse_registry)] + pub registry: Option, } pub struct RunProperties { @@ -479,14 +484,10 @@ impl Wasi { wasmer_dir: &Path, client: Arc, ) -> Result { - // FIXME(Michael-F-Bryan): Ideally, all of this would live in some sort - // of from_env() constructor, but we don't want to add wasmer-registry - // as a dependency of wasmer-wasix just yet. - let config = - wasmer_registry::WasmerConfig::from_file(wasmer_dir).map_err(anyhow::Error::msg)?; - let mut registry = MultiSourceRegistry::new(); + // Note: This should be first so our "preloaded" sources get a chance to + // override the main registry. let mut preloaded = InMemorySource::new(); for path in &self.include_webcs { preloaded @@ -495,12 +496,7 @@ impl Wasi { } registry.add_source(preloaded); - // Note: This should be last so our "preloaded" sources get a chance to - // override the main registry. - let graphql_endpoint = config.registry.get_graphql_url(); - let graphql_endpoint = graphql_endpoint - .parse() - .with_context(|| format!("Unable to parse \"{graphql_endpoint}\" as a URL"))?; + let graphql_endpoint = self.graphql_endpoint(wasmer_dir)?; registry.add_source(WapmSource::new(graphql_endpoint, Arc::clone(&client))); let cache_dir = WebSource::default_cache_dir(wasmer_dir); @@ -508,4 +504,24 @@ impl Wasi { Ok(registry) } + + fn graphql_endpoint(&self, wasmer_dir: &Path) -> Result { + if let Some(endpoint) = &self.registry { + return Ok(endpoint.clone()); + } + + let config = + wasmer_registry::WasmerConfig::from_file(wasmer_dir).map_err(anyhow::Error::msg)?; + let graphql_endpoint = config.registry.get_graphql_url(); + let graphql_endpoint = graphql_endpoint + .parse() + .with_context(|| format!("Unable to parse \"{graphql_endpoint}\" as a URL"))?; + + Ok(graphql_endpoint) + } +} + +fn parse_registry(r: &str) -> Result { + let url = wasmer_registry::format_graphql(r).parse()?; + Ok(url) } diff --git a/lib/wasi/src/runtime/package_loader/builtin_loader.rs b/lib/wasi/src/runtime/package_loader/builtin_loader.rs index 9870227bb5f..05295dc76b8 100644 --- a/lib/wasi/src/runtime/package_loader/builtin_loader.rs +++ b/lib/wasi/src/runtime/package_loader/builtin_loader.rs @@ -57,8 +57,8 @@ impl BuiltinPackageLoader { wasmer_dir.as_ref().join("checkouts") } - /// Create a new [`BuiltinLoader`] based on `$WASMER_DIR` and the global - /// Wasmer config. + /// Create a new [`BuiltinPackageLoader`] based on `$WASMER_DIR` and the + /// global Wasmer config. pub fn from_env() -> Result { let wasmer_dir = discover_wasmer_dir().context("Unable to determine $WASMER_DIR")?; let client = crate::http::default_http_client().context("No HTTP client available")?; diff --git a/lib/wasi/src/runtime/resolver/web_source.rs b/lib/wasi/src/runtime/resolver/web_source.rs index 4f4c33e6e92..44634368040 100644 --- a/lib/wasi/src/runtime/resolver/web_source.rs +++ b/lib/wasi/src/runtime/resolver/web_source.rs @@ -67,7 +67,7 @@ impl WebSource { } /// Download a package and cache it locally. - #[tracing::instrument(skip(self))] + #[tracing::instrument(skip_all, fields(%url))] async fn get_locally_cached_file(&self, url: &Url) -> Result { // This function is a bit tricky because we go to great lengths to avoid // unnecessary downloads. @@ -227,7 +227,7 @@ impl Source for WebSource { _ => return Ok(Vec::new()), }; - let local_path = self.get_locally_cached_file(url).await?; + let local_path = self.get_locally_cached_file(url).await.context("Unable to get the locally cached file")?; // FIXME: this will block let webc_sha256 = WebcHash::for_file(&local_path)?;