diff --git a/Cargo.lock b/Cargo.lock index 6ff077c221..d12c239470 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4395,6 +4395,7 @@ dependencies = [ "tracing", "url", "wasmtime", + "wasmtime-cache", ] [[package]] diff --git a/crates/testing/src/lib.rs b/crates/testing/src/lib.rs index 7bf3f9c097..4a4b04b35b 100644 --- a/crates/testing/src/lib.rs +++ b/crates/testing/src/lib.rs @@ -102,7 +102,7 @@ impl HttpTestConfig { TriggerExecutorBuilder::new(self.build_loader()) .build( TEST_APP_URI.to_string(), - TriggerExecutorBuilderConfig::default(), + &TriggerExecutorBuilderConfig::default(), ) .await .unwrap() @@ -141,7 +141,7 @@ impl RedisTestConfig { TriggerExecutorBuilder::new(self.build_loader()) .build( TEST_APP_URI.to_string(), - TriggerExecutorBuilderConfig::default(), + &TriggerExecutorBuilderConfig::default(), ) .await .unwrap() diff --git a/crates/trigger/Cargo.toml b/crates/trigger/Cargo.toml index e3aa48b160..c830f76789 100644 --- a/crates/trigger/Cargo.toml +++ b/crates/trigger/Cargo.toml @@ -26,6 +26,7 @@ toml = "0.5.9" tracing = { version = "0.1", features = [ "log" ] } url = "2" wasmtime = "0.39.1" +wasmtime-cache = "0.39.1" [dev-dependencies] tempfile = "3.3.0" diff --git a/crates/trigger/src/cli.rs b/crates/trigger/src/cli.rs index becf264847..4be12c1c7a 100644 --- a/crates/trigger/src/cli.rs +++ b/crates/trigger/src/cli.rs @@ -1,6 +1,6 @@ -use std::path::PathBuf; +use std::path::{Path, PathBuf}; -use anyhow::{Context, Result}; +use anyhow::{anyhow, Context, Result}; use clap::{Args, IntoApp, Parser}; use serde::de::DeserializeOwned; @@ -9,9 +9,7 @@ use crate::{config::TriggerExecutorBuilderConfig, loader::TriggerLoader, stdio:: use crate::{TriggerExecutor, TriggerExecutorBuilder}; pub const APP_LOG_DIR: &str = "APP_LOG_DIR"; -pub const DISABLE_WASMTIME_CACHE: &str = "DISABLE_WASMTIME_CACHE"; pub const FOLLOW_LOG_OPT: &str = "FOLLOW_ID"; -pub const WASMTIME_CACHE_FILE: &str = "WASMTIME_CACHE_FILE"; pub const RUNTIME_CONFIG_FILE: &str = "RUNTIME_CONFIG_FILE"; // Set by `spin up` @@ -33,25 +31,6 @@ where )] pub log: Option, - /// Disable Wasmtime cache. - #[clap( - name = DISABLE_WASMTIME_CACHE, - long = "disable-cache", - env = DISABLE_WASMTIME_CACHE, - conflicts_with = WASMTIME_CACHE_FILE, - takes_value = false, - )] - pub disable_cache: bool, - - /// Wasmtime cache configuration file. - #[clap( - name = WASMTIME_CACHE_FILE, - long = "cache", - env = WASMTIME_CACHE_FILE, - conflicts_with = DISABLE_WASMTIME_CACHE, - )] - pub cache: Option, - /// Print output for given component(s) to stdout/stderr #[clap( name = FOLLOW_LOG_OPT, @@ -110,19 +89,32 @@ where let working_dir = std::env::var(SPIN_WORKING_DIR).context(SPIN_WORKING_DIR)?; let locked_url = std::env::var(SPIN_LOCKED_URL).context(SPIN_LOCKED_URL)?; - let loader = TriggerLoader::new(working_dir, self.allow_transient_write); + let loader = TriggerLoader::new(&working_dir, self.allow_transient_write); + let base_runtime_config_dir = match &self.runtime_config_file { + Some(p) => p.parent().ok_or_else(|| { + anyhow!( + "Failed to get containing directory for runtime config file '{}'", + &p.display() + ) + })?, + None => Path::new(&working_dir), + }; let trigger_config = TriggerExecutorBuilderConfig::load_from_file(self.runtime_config_file.clone())?; let executor: Executor = { let mut builder = TriggerExecutorBuilder::new(loader); - self.update_wasmtime_config(builder.wasmtime_config_mut())?; + self.update_wasmtime_config( + builder.wasmtime_config_mut(), + &trigger_config, + base_runtime_config_dir, + )?; let logging_hooks = StdioLoggingTriggerHooks::new(self.follow_components(), self.log); builder.hooks(logging_hooks); - builder.build(locked_url, trigger_config).await? + builder.build(locked_url, &trigger_config).await? }; let run_fut = executor.run(self.run_config); @@ -156,14 +148,27 @@ where } } - fn update_wasmtime_config(&self, config: &mut spin_core::wasmtime::Config) -> Result<()> { - // Apply --cache / --disable-cache - if !self.disable_cache { - match &self.cache { - Some(p) => config.cache_config_load(p)?, - None => config.cache_config_load_default()?, + fn update_wasmtime_config( + &self, + wasmtime_config: &mut spin_core::wasmtime::Config, + trigger_config: &TriggerExecutorBuilderConfig, + base_dir: impl AsRef, + ) -> Result<()> { + if let Some(p) = &trigger_config.wasmtime_config.cache_file { + let p = match Path::new(p).is_absolute() { + true => PathBuf::from(p), + false => base_dir.as_ref().join(p), }; + wasmtime_config.cache_config_load(p)?; } + + let wasm_backtrace_details = match &*trigger_config.wasmtime_config.wasm_backtrace_details { + "Enable" => wasmtime::WasmBacktraceDetails::Enable, + "Environment" => wasmtime::WasmBacktraceDetails::Environment, + _ => wasmtime::WasmBacktraceDetails::Disable, + }; + tracing::trace!("wasm_backtrace_details {:?}", wasm_backtrace_details); + wasmtime_config.wasm_backtrace_details(wasm_backtrace_details); Ok(()) } } diff --git a/crates/trigger/src/config.rs b/crates/trigger/src/config.rs index 46818359ad..094def0563 100644 --- a/crates/trigger/src/config.rs +++ b/crates/trigger/src/config.rs @@ -9,6 +9,9 @@ use toml; pub struct TriggerExecutorBuilderConfig { #[serde(rename = "config_provider", default)] pub config_providers: Vec, + + #[serde(rename = "wasmtime", default)] + pub wasmtime_config: WasmtimeConfig, } #[derive(Debug, Deserialize)] @@ -26,6 +29,15 @@ pub struct VaultConfig { pub prefix: Option, } +// Wasmtime config to initialize wasmtime engine +#[derive(Debug, Default, Deserialize)] +pub struct WasmtimeConfig { + pub cache_file: Option, + + #[serde(default = "default_wask_backtrace_details")] + pub wasm_backtrace_details: String, +} + impl TriggerExecutorBuilderConfig { pub fn load_from_file(config_file: Option) -> Result { let config_file = match config_file { @@ -39,3 +51,7 @@ impl TriggerExecutorBuilderConfig { Ok(config) } } + +fn default_wask_backtrace_details() -> String { + "Disable".to_string() +} diff --git a/crates/trigger/src/lib.rs b/crates/trigger/src/lib.rs index 896766794f..b5fc9132c6 100644 --- a/crates/trigger/src/lib.rs +++ b/crates/trigger/src/lib.rs @@ -83,7 +83,7 @@ impl TriggerExecutorBuilder { pub async fn build( mut self, app_uri: String, - builder_config: config::TriggerExecutorBuilderConfig, + builder_config: &config::TriggerExecutorBuilderConfig, ) -> Result where Executor::TriggerConfig: DeserializeOwned, @@ -101,7 +101,7 @@ impl TriggerExecutorBuilder { self.loader.add_dynamic_host_component( &mut builder, spin_config::ConfigHostComponent::new( - self.get_config_providers(&app_uri, &builder_config), + self.get_config_providers(&app_uri, builder_config), ), )?; } diff --git a/tests/http/simple-spin-rust/runtime_config.toml b/tests/http/simple-spin-rust/runtime_config.toml new file mode 100644 index 0000000000..cd76b83f30 --- /dev/null +++ b/tests/http/simple-spin-rust/runtime_config.toml @@ -0,0 +1,3 @@ +[wasmtime] +cache_file = "wasmtime_config.toml" +wasm_backtrace_details = "Enable" diff --git a/tests/http/simple-spin-rust/spin.toml b/tests/http/simple-spin-rust/spin.toml index 160e7fd29f..80362bd074 100644 --- a/tests/http/simple-spin-rust/spin.toml +++ b/tests/http/simple-spin-rust/spin.toml @@ -16,3 +16,5 @@ files = [ { source = "assets", destination = "/" } ] route = "/hello/..." [component.config] message = "I'm a {{object}}" +[component.build] +command = "cargo build --target wasm32-wasi --release" diff --git a/tests/http/simple-spin-rust/wasmtime_config.toml b/tests/http/simple-spin-rust/wasmtime_config.toml new file mode 100644 index 0000000000..8b6fb0ea4c --- /dev/null +++ b/tests/http/simple-spin-rust/wasmtime_config.toml @@ -0,0 +1,6 @@ +# Comment out certain settings to use default values. +# For more settings, please refer to the documentation: +# https://bytecodealliance.github.io/wasmtime/cli-cache.html + +[cache] +enabled = false