1pub mod wit;
2
3use crate::{ExtensionManifest, ExtensionRegistrationHooks};
4use anyhow::{anyhow, bail, Context as _, Result};
5use async_trait::async_trait;
6use extension::{
7 CodeLabel, Command, Completion, KeyValueStoreDelegate, SlashCommand,
8 SlashCommandArgumentCompletion, SlashCommandOutput, Symbol, WorktreeDelegate,
9};
10use fs::{normalize_path, Fs};
11use futures::future::LocalBoxFuture;
12use futures::{
13 channel::{
14 mpsc::{self, UnboundedSender},
15 oneshot,
16 },
17 future::BoxFuture,
18 Future, FutureExt, StreamExt as _,
19};
20use gpui::{AppContext, AsyncAppContext, BackgroundExecutor, Task};
21use http_client::HttpClient;
22use language::LanguageName;
23use lsp::LanguageServerName;
24use node_runtime::NodeRuntime;
25use release_channel::ReleaseChannel;
26use semantic_version::SemanticVersion;
27use std::{
28 path::{Path, PathBuf},
29 sync::{Arc, OnceLock},
30};
31use wasmtime::{
32 component::{Component, ResourceTable},
33 Engine, Store,
34};
35use wasmtime_wasi::{self as wasi, WasiView};
36use wit::Extension;
37pub use wit::ExtensionProject;
38
39pub struct WasmHost {
40 engine: Engine,
41 release_channel: ReleaseChannel,
42 http_client: Arc<dyn HttpClient>,
43 node_runtime: NodeRuntime,
44 pub registration_hooks: Arc<dyn ExtensionRegistrationHooks>,
45 fs: Arc<dyn Fs>,
46 pub work_dir: PathBuf,
47 _main_thread_message_task: Task<()>,
48 main_thread_message_tx: mpsc::UnboundedSender<MainThreadCall>,
49}
50
51#[derive(Clone)]
52pub struct WasmExtension {
53 tx: UnboundedSender<ExtensionCall>,
54 pub manifest: Arc<ExtensionManifest>,
55 pub work_dir: Arc<Path>,
56 #[allow(unused)]
57 pub zed_api_version: SemanticVersion,
58}
59
60#[async_trait]
61impl extension::Extension for WasmExtension {
62 fn manifest(&self) -> Arc<ExtensionManifest> {
63 self.manifest.clone()
64 }
65
66 fn work_dir(&self) -> Arc<Path> {
67 self.work_dir.clone()
68 }
69
70 async fn language_server_command(
71 &self,
72 language_server_id: LanguageServerName,
73 language_name: LanguageName,
74 worktree: Arc<dyn WorktreeDelegate>,
75 ) -> Result<Command> {
76 self.call(|extension, store| {
77 async move {
78 let resource = store.data_mut().table().push(worktree)?;
79 let command = extension
80 .call_language_server_command(
81 store,
82 &language_server_id,
83 &language_name,
84 resource,
85 )
86 .await?
87 .map_err(|err| anyhow!("{err}"))?;
88
89 Ok(command.into())
90 }
91 .boxed()
92 })
93 .await
94 }
95
96 async fn language_server_initialization_options(
97 &self,
98 language_server_id: LanguageServerName,
99 language_name: LanguageName,
100 worktree: Arc<dyn WorktreeDelegate>,
101 ) -> Result<Option<String>> {
102 self.call(|extension, store| {
103 async move {
104 let resource = store.data_mut().table().push(worktree)?;
105 let options = extension
106 .call_language_server_initialization_options(
107 store,
108 &language_server_id,
109 &language_name,
110 resource,
111 )
112 .await?
113 .map_err(|err| anyhow!("{err}"))?;
114 anyhow::Ok(options)
115 }
116 .boxed()
117 })
118 .await
119 }
120
121 async fn language_server_workspace_configuration(
122 &self,
123 language_server_id: LanguageServerName,
124 worktree: Arc<dyn WorktreeDelegate>,
125 ) -> Result<Option<String>> {
126 self.call(|extension, store| {
127 async move {
128 let resource = store.data_mut().table().push(worktree)?;
129 let options = extension
130 .call_language_server_workspace_configuration(
131 store,
132 &language_server_id,
133 resource,
134 )
135 .await?
136 .map_err(|err| anyhow!("{err}"))?;
137 anyhow::Ok(options)
138 }
139 .boxed()
140 })
141 .await
142 }
143
144 async fn labels_for_completions(
145 &self,
146 language_server_id: LanguageServerName,
147 completions: Vec<Completion>,
148 ) -> Result<Vec<Option<CodeLabel>>> {
149 self.call(|extension, store| {
150 async move {
151 let labels = extension
152 .call_labels_for_completions(
153 store,
154 &language_server_id,
155 completions.into_iter().map(Into::into).collect(),
156 )
157 .await?
158 .map_err(|err| anyhow!("{err}"))?;
159
160 Ok(labels
161 .into_iter()
162 .map(|label| label.map(Into::into))
163 .collect())
164 }
165 .boxed()
166 })
167 .await
168 }
169
170 async fn labels_for_symbols(
171 &self,
172 language_server_id: LanguageServerName,
173 symbols: Vec<Symbol>,
174 ) -> Result<Vec<Option<CodeLabel>>> {
175 self.call(|extension, store| {
176 async move {
177 let labels = extension
178 .call_labels_for_symbols(
179 store,
180 &language_server_id,
181 symbols.into_iter().map(Into::into).collect(),
182 )
183 .await?
184 .map_err(|err| anyhow!("{err}"))?;
185
186 Ok(labels
187 .into_iter()
188 .map(|label| label.map(Into::into))
189 .collect())
190 }
191 .boxed()
192 })
193 .await
194 }
195
196 async fn complete_slash_command_argument(
197 &self,
198 command: SlashCommand,
199 arguments: Vec<String>,
200 ) -> Result<Vec<SlashCommandArgumentCompletion>> {
201 self.call(|extension, store| {
202 async move {
203 let completions = extension
204 .call_complete_slash_command_argument(store, &command.into(), &arguments)
205 .await?
206 .map_err(|err| anyhow!("{err}"))?;
207
208 Ok(completions.into_iter().map(Into::into).collect())
209 }
210 .boxed()
211 })
212 .await
213 }
214
215 async fn run_slash_command(
216 &self,
217 command: SlashCommand,
218 arguments: Vec<String>,
219 delegate: Option<Arc<dyn WorktreeDelegate>>,
220 ) -> Result<SlashCommandOutput> {
221 self.call(|extension, store| {
222 async move {
223 let resource = if let Some(delegate) = delegate {
224 Some(store.data_mut().table().push(delegate)?)
225 } else {
226 None
227 };
228
229 let output = extension
230 .call_run_slash_command(store, &command.into(), &arguments, resource)
231 .await?
232 .map_err(|err| anyhow!("{err}"))?;
233
234 Ok(output.into())
235 }
236 .boxed()
237 })
238 .await
239 }
240
241 async fn suggest_docs_packages(&self, provider: Arc<str>) -> Result<Vec<String>> {
242 self.call(|extension, store| {
243 async move {
244 let packages = extension
245 .call_suggest_docs_packages(store, provider.as_ref())
246 .await?
247 .map_err(|err| anyhow!("{err:?}"))?;
248
249 Ok(packages)
250 }
251 .boxed()
252 })
253 .await
254 }
255
256 async fn index_docs(
257 &self,
258 provider: Arc<str>,
259 package_name: Arc<str>,
260 kv_store: Arc<dyn KeyValueStoreDelegate>,
261 ) -> Result<()> {
262 self.call(|extension, store| {
263 async move {
264 let kv_store_resource = store.data_mut().table().push(kv_store)?;
265 extension
266 .call_index_docs(
267 store,
268 provider.as_ref(),
269 package_name.as_ref(),
270 kv_store_resource,
271 )
272 .await?
273 .map_err(|err| anyhow!("{err:?}"))?;
274
275 anyhow::Ok(())
276 }
277 .boxed()
278 })
279 .await
280 }
281}
282
283pub struct WasmState {
284 manifest: Arc<ExtensionManifest>,
285 pub table: ResourceTable,
286 ctx: wasi::WasiCtx,
287 pub host: Arc<WasmHost>,
288}
289
290type MainThreadCall =
291 Box<dyn Send + for<'a> FnOnce(&'a mut AsyncAppContext) -> LocalBoxFuture<'a, ()>>;
292
293type ExtensionCall = Box<
294 dyn Send + for<'a> FnOnce(&'a mut Extension, &'a mut Store<WasmState>) -> BoxFuture<'a, ()>,
295>;
296
297fn wasm_engine() -> wasmtime::Engine {
298 static WASM_ENGINE: OnceLock<wasmtime::Engine> = OnceLock::new();
299
300 WASM_ENGINE
301 .get_or_init(|| {
302 let mut config = wasmtime::Config::new();
303 config.wasm_component_model(true);
304 config.async_support(true);
305 wasmtime::Engine::new(&config).unwrap()
306 })
307 .clone()
308}
309
310impl WasmHost {
311 pub fn new(
312 fs: Arc<dyn Fs>,
313 http_client: Arc<dyn HttpClient>,
314 node_runtime: NodeRuntime,
315 registration_hooks: Arc<dyn ExtensionRegistrationHooks>,
316 work_dir: PathBuf,
317 cx: &mut AppContext,
318 ) -> Arc<Self> {
319 let (tx, mut rx) = mpsc::unbounded::<MainThreadCall>();
320 let task = cx.spawn(|mut cx| async move {
321 while let Some(message) = rx.next().await {
322 message(&mut cx).await;
323 }
324 });
325 Arc::new(Self {
326 engine: wasm_engine(),
327 fs,
328 work_dir,
329 http_client,
330 node_runtime,
331 registration_hooks,
332 release_channel: ReleaseChannel::global(cx),
333 _main_thread_message_task: task,
334 main_thread_message_tx: tx,
335 })
336 }
337
338 pub fn load_extension(
339 self: &Arc<Self>,
340 wasm_bytes: Vec<u8>,
341 manifest: &Arc<ExtensionManifest>,
342 executor: BackgroundExecutor,
343 ) -> Task<Result<WasmExtension>> {
344 let this = self.clone();
345 let manifest = manifest.clone();
346 executor.clone().spawn(async move {
347 let zed_api_version = parse_wasm_extension_version(&manifest.id, &wasm_bytes)?;
348
349 let component = Component::from_binary(&this.engine, &wasm_bytes)
350 .context("failed to compile wasm component")?;
351
352 let mut store = wasmtime::Store::new(
353 &this.engine,
354 WasmState {
355 ctx: this.build_wasi_ctx(&manifest).await?,
356 manifest: manifest.clone(),
357 table: ResourceTable::new(),
358 host: this.clone(),
359 },
360 );
361
362 let mut extension = Extension::instantiate_async(
363 &mut store,
364 this.release_channel,
365 zed_api_version,
366 &component,
367 )
368 .await?;
369
370 extension
371 .call_init_extension(&mut store)
372 .await
373 .context("failed to initialize wasm extension")?;
374
375 let (tx, mut rx) = mpsc::unbounded::<ExtensionCall>();
376 executor
377 .spawn(async move {
378 while let Some(call) = rx.next().await {
379 (call)(&mut extension, &mut store).await;
380 }
381 })
382 .detach();
383
384 Ok(WasmExtension {
385 manifest: manifest.clone(),
386 work_dir: this.work_dir.join(manifest.id.as_ref()).into(),
387 tx,
388 zed_api_version,
389 })
390 })
391 }
392
393 async fn build_wasi_ctx(&self, manifest: &Arc<ExtensionManifest>) -> Result<wasi::WasiCtx> {
394 let extension_work_dir = self.work_dir.join(manifest.id.as_ref());
395 self.fs
396 .create_dir(&extension_work_dir)
397 .await
398 .context("failed to create extension work dir")?;
399
400 let file_perms = wasi::FilePerms::all();
401 let dir_perms = wasi::DirPerms::all();
402
403 Ok(wasi::WasiCtxBuilder::new()
404 .inherit_stdio()
405 .preopened_dir(&extension_work_dir, ".", dir_perms, file_perms)?
406 .preopened_dir(
407 &extension_work_dir,
408 extension_work_dir.to_string_lossy(),
409 dir_perms,
410 file_perms,
411 )?
412 .env("PWD", extension_work_dir.to_string_lossy())
413 .env("RUST_BACKTRACE", "full")
414 .build())
415 }
416
417 pub fn writeable_path_from_extension(&self, id: &Arc<str>, path: &Path) -> Result<PathBuf> {
418 let extension_work_dir = self.work_dir.join(id.as_ref());
419 let path = normalize_path(&extension_work_dir.join(path));
420 if path.starts_with(&extension_work_dir) {
421 Ok(path)
422 } else {
423 Err(anyhow!("cannot write to path {}", path.display()))
424 }
425 }
426}
427
428pub fn parse_wasm_extension_version(
429 extension_id: &str,
430 wasm_bytes: &[u8],
431) -> Result<SemanticVersion> {
432 let mut version = None;
433
434 for part in wasmparser::Parser::new(0).parse_all(wasm_bytes) {
435 if let wasmparser::Payload::CustomSection(s) =
436 part.context("error parsing wasm extension")?
437 {
438 if s.name() == "zed:api-version" {
439 version = parse_wasm_extension_version_custom_section(s.data());
440 if version.is_none() {
441 bail!(
442 "extension {} has invalid zed:api-version section: {:?}",
443 extension_id,
444 s.data()
445 );
446 }
447 }
448 }
449 }
450
451 // The reason we wait until we're done parsing all of the Wasm bytes to return the version
452 // is to work around a panic that can happen inside of Wasmtime when the bytes are invalid.
453 //
454 // By parsing the entirety of the Wasm bytes before we return, we're able to detect this problem
455 // earlier as an `Err` rather than as a panic.
456 version.ok_or_else(|| anyhow!("extension {} has no zed:api-version section", extension_id))
457}
458
459fn parse_wasm_extension_version_custom_section(data: &[u8]) -> Option<SemanticVersion> {
460 if data.len() == 6 {
461 Some(SemanticVersion::new(
462 u16::from_be_bytes([data[0], data[1]]) as _,
463 u16::from_be_bytes([data[2], data[3]]) as _,
464 u16::from_be_bytes([data[4], data[5]]) as _,
465 ))
466 } else {
467 None
468 }
469}
470
471impl WasmExtension {
472 pub async fn load(
473 extension_dir: PathBuf,
474 manifest: &Arc<ExtensionManifest>,
475 wasm_host: Arc<WasmHost>,
476 cx: &AsyncAppContext,
477 ) -> Result<Self> {
478 let path = extension_dir.join("extension.wasm");
479
480 let mut wasm_file = wasm_host
481 .fs
482 .open_sync(&path)
483 .await
484 .context("failed to open wasm file")?;
485
486 let mut wasm_bytes = Vec::new();
487 wasm_file
488 .read_to_end(&mut wasm_bytes)
489 .context("failed to read wasm")?;
490
491 wasm_host
492 .load_extension(wasm_bytes, manifest, cx.background_executor().clone())
493 .await
494 .with_context(|| format!("failed to load wasm extension {}", manifest.id))
495 }
496
497 pub async fn call<T, Fn>(&self, f: Fn) -> T
498 where
499 T: 'static + Send,
500 Fn: 'static
501 + Send
502 + for<'a> FnOnce(&'a mut Extension, &'a mut Store<WasmState>) -> BoxFuture<'a, T>,
503 {
504 let (return_tx, return_rx) = oneshot::channel();
505 self.tx
506 .clone()
507 .unbounded_send(Box::new(move |extension, store| {
508 async {
509 let result = f(extension, store).await;
510 return_tx.send(result).ok();
511 }
512 .boxed()
513 }))
514 .expect("wasm extension channel should not be closed yet");
515 return_rx.await.expect("wasm extension channel")
516 }
517}
518
519impl WasmState {
520 fn on_main_thread<T, Fn>(&self, f: Fn) -> impl 'static + Future<Output = T>
521 where
522 T: 'static + Send,
523 Fn: 'static + Send + for<'a> FnOnce(&'a mut AsyncAppContext) -> LocalBoxFuture<'a, T>,
524 {
525 let (return_tx, return_rx) = oneshot::channel();
526 self.host
527 .main_thread_message_tx
528 .clone()
529 .unbounded_send(Box::new(move |cx| {
530 async {
531 let result = f(cx).await;
532 return_tx.send(result).ok();
533 }
534 .boxed_local()
535 }))
536 .expect("main thread message channel should not be closed yet");
537 async move { return_rx.await.expect("main thread message channel") }
538 }
539
540 fn work_dir(&self) -> PathBuf {
541 self.host.work_dir.join(self.manifest.id.as_ref())
542 }
543}
544
545impl wasi::WasiView for WasmState {
546 fn table(&mut self) -> &mut ResourceTable {
547 &mut self.table
548 }
549
550 fn ctx(&mut self) -> &mut wasi::WasiCtx {
551 &mut self.ctx
552 }
553}