From d16726205e12c4bd80956f3cb83f6602ad7b6359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=A0?= Date: Sat, 20 Jan 2024 15:42:42 +0100 Subject: [PATCH] Add feature to make the runtime immutable This prevents installing and uninstalling the runtime using commands, and also stops patching the runtime when launching web apps. The runtime can still be patched manually from the extension settings or the patch command if the user has sufficient write permissions to do so. This is meant for immutable systems like Guix or Nix where runtime might be provided by the package manager and pre-patched. See #204 for more. --- native/Cargo.toml | 1 + native/src/components/runtime.rs | 2 ++ native/src/console/runtime.rs | 12 ++++++++++++ native/src/console/site.rs | 1 + 4 files changed, 16 insertions(+) diff --git a/native/Cargo.toml b/native/Cargo.toml index 9727b4ce..b148ae8d 100644 --- a/native/Cargo.toml +++ b/native/Cargo.toml @@ -22,6 +22,7 @@ lto = true [features] portable = ["dep:phf"] static = ["reqwest/native-tls-vendored", "bzip2/static"] +immutable-runtime = [] [dependencies] ab_glyph = "0.2.23" diff --git a/native/src/components/runtime.rs b/native/src/components/runtime.rs index 95fd4681..f6f37541 100644 --- a/native/src/components/runtime.rs +++ b/native/src/components/runtime.rs @@ -187,6 +187,7 @@ impl Runtime { Ok(Self { version, directory, executable, config }) } + #[cfg(not(feature = "immutable-runtime"))] pub fn install(self) -> Result<()> { const TEMP_FILE_ERROR: &str = "Failed to create a temporary file"; const DOWNLOAD_ERROR: &str = "Failed to download the runtime"; @@ -269,6 +270,7 @@ impl Runtime { Ok(()) } + #[cfg(not(feature = "immutable-runtime"))] pub fn uninstall(self) -> Result<()> { info!("Uninstalling the runtime"); remove_dir_contents(self.directory).context("Failed to remove runtime directory")?; diff --git a/native/src/console/runtime.rs b/native/src/console/runtime.rs index 01cfbb98..ccbe4ed8 100644 --- a/native/src/console/runtime.rs +++ b/native/src/console/runtime.rs @@ -7,6 +7,7 @@ use crate::console::Run; use crate::directories::ProjectDirs; impl Run for RuntimeInstallCommand { + #[cfg(not(feature = "immutable-runtime"))] fn run(&self) -> Result<()> { cfg_if! { if #[cfg(platform_windows)] { @@ -31,15 +32,26 @@ impl Run for RuntimeInstallCommand { Ok(()) } + + #[cfg(feature = "immutable-runtime")] + fn run(&self) -> Result<()> { + anyhow::bail!("Cannot install runtime when the immutable runtime feature is enabled") + } } impl Run for RuntimeUninstallCommand { + #[cfg(not(feature = "immutable-runtime"))] fn run(&self) -> Result<()> { let dirs = ProjectDirs::new()?; let runtime = Runtime::new(&dirs)?; runtime.uninstall().context("Failed to uninstall runtime") } + + #[cfg(feature = "immutable-runtime")] + fn run(&self) -> Result<()> { + anyhow::bail!("Cannot uninstall runtime when the immutable runtime feature is enabled") + } } impl Run for RuntimePatchCommand { diff --git a/native/src/console/site.rs b/native/src/console/site.rs index 3dacbb7c..dcb91e2c 100644 --- a/native/src/console/site.rs +++ b/native/src/console/site.rs @@ -78,6 +78,7 @@ impl Run for SiteLaunchCommand { }; if should_patch { + #[cfg(not(feature = "immutable-runtime"))] runtime.patch(&dirs, Some(site))?; profile.patch(&dirs)?; }