Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
extension: Check for rustup before calling it (#56090)
Karol Broda2620c2acc1d8
diff --git a/Cargo.lock b/Cargo.lockindex 747170fd5f2..03b609350fe 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -6333,6 +6333,7 @@ dependencies = ["util","wasm-encoder 0.252.0","wasmparser 0.252.0",+ "which 6.0.3","ztracing",]diff --git a/crates/extension/Cargo.toml b/crates/extension/Cargo.tomlindex 55af1f4007a..776e84490cb 100644--- a/crates/extension/Cargo.toml+++ b/crates/extension/Cargo.toml@@ -38,6 +38,7 @@ url.workspace = trueutil.workspace = truewasm-encoder.workspace = truewasmparser.workspace = true+which.workspace = trueztracing.workspace = true[dev-dependencies]diff --git a/crates/extension/src/extension_builder.rs b/crates/extension/src/extension_builder.rsindex 03583f86a24..3049a785ff3 100644--- a/crates/extension/src/extension_builder.rs+++ b/crates/extension/src/extension_builder.rs@@ -447,23 +447,31 @@ impl ExtensionBuilder {async fn install_rust_wasm_target_if_needed(&self) -> Result<()> {let rustc_output = util::command::new_command("rustc")- .arg("--print")- .arg("sysroot")+ .args(["--print", "target-libdir", "--target", RUST_TARGET]).output().await.context("failed to run rustc")?;if !rustc_output.status.success() {bail!(- "failed to retrieve rust sysroot: {}",+ "failed to retrieve the `{RUST_TARGET}` target libdir: {}",String::from_utf8_lossy(&rustc_output.stderr));}- let sysroot = PathBuf::from(String::from_utf8(rustc_output.stdout)?.trim());- if sysroot.join("lib/rustlib").join(RUST_TARGET).exists() {+ let target_libdir = PathBuf::from(String::from_utf8(rustc_output.stdout)?.trim());+ if target_libdir.exists() {return Ok(());}+ if which::which("rustup").is_err() {+ bail!(+ "the `{RUST_TARGET}` target is not installed, and `rustup` is not available to \+ install it. Add the target to your Rust toolchain (e.g. `targets = \+ [\"{RUST_TARGET}\"]` for Nix rust-overlay/fenix toolchains) or install it via \+ your package manager"+ );+ }+let output = util::command::new_command("rustup").args(["target", "add", RUST_TARGET]).stderr(Stdio::piped())@@ -474,7 +482,7 @@ impl ExtensionBuilder {if !output.status.success() {bail!("failed to install the `{RUST_TARGET}` target: {}",- String::from_utf8_lossy(&rustc_output.stderr)+ String::from_utf8_lossy(&output.stderr));}@@ -482,6 +490,18 @@ impl ExtensionBuilder {}async fn install_wasi_sdk_if_needed(&self) -> Result<PathBuf> {+ if let Some(sdk_path) = env::var_os("WASI_SDK_PATH").filter(|path| !path.is_empty()) {+ let sdk_path = PathBuf::from(sdk_path);+ let clang_path = wasi_sdk_clang_path(&sdk_path);+ if fs::metadata(&clang_path).is_ok_and(|metadata| metadata.is_file()) {+ log::info!("using wasi-sdk from WASI_SDK_PATH: {sdk_path:?}");+ return Ok(clang_path);+ }+ log::warn!(+ "WASI_SDK_PATH is set to {sdk_path:?} but clang was not found at {clang_path:?}, falling back to download"+ );+ }+let url = if let Some(asset_name) = WASI_SDK_ASSET_NAME {format!("{WASI_SDK_URL}{asset_name}")} else {@@ -489,8 +509,7 @@ impl ExtensionBuilder {};let wasi_sdk_dir = self.cache_dir.join("wasi-sdk");- let mut clang_path = wasi_sdk_dir.clone();- clang_path.extend(["bin", &format!("clang{}", env::consts::EXE_SUFFIX)]);+ let clang_path = wasi_sdk_clang_path(&wasi_sdk_dir);log::info!("downloading wasi-sdk to {}", wasi_sdk_dir.display());@@ -776,6 +795,12 @@ fn file_newer_than_deps(target: &Path, dependencies: &[&Path]) -> Result<bool, sOk(true)}+fn wasi_sdk_clang_path(wasi_sdk_dir: &Path) -> PathBuf {+ let mut clang_path = wasi_sdk_dir.to_path_buf();+ clang_path.extend(["bin", &format!("clang{}", env::consts::EXE_SUFFIX)]);+ clang_path+}+#[cfg(test)]mod tests {use std::{diff --git a/docs/src/extensions/developing-extensions.md b/docs/src/extensions/developing-extensions.mdindex 13dfa2a4aaf..1e147f5db1a 100644--- a/docs/src/extensions/developing-extensions.md+++ b/docs/src/extensions/developing-extensions.md@@ -22,7 +22,9 @@ Extensions can provide:Before starting to develop an extension for Zed, be sure to [install Rust via rustup](https://www.rust-lang.org/tools/install).-> Rust must be installed via rustup. If you have Rust installed via homebrew or otherwise, installing dev extensions will not work.+> Zed uses the `wasm32-wasip2` Rust target to compile extensions. If Rust is installed via rustup, Zed will install the target automatically. If Rust is installed another way (e.g., via Homebrew or Nix), you must make the `wasm32-wasip2` target available yourself — for example, by adding it to the `targets` of a Nix rust-overlay or fenix toolchain.++Extensions that provide grammars additionally require the [wasi-sdk](https://github.com/WebAssembly/wasi-sdk) to compile Tree-sitter parsers. Zed downloads it automatically, but you can point Zed at an existing installation by setting the `WASI_SDK_PATH` environment variable to its root directory (the one containing `bin/clang`).When developing an extension, you can use it in Zed without needing to publish it by installing it as a _dev extension_.