1pub mod extension_builder;
2mod extension_indexed_docs_provider;
3mod extension_lsp_adapter;
4mod extension_manifest;
5mod extension_settings;
6mod extension_slash_command;
7mod wasm_host;
8
9#[cfg(test)]
10mod extension_store_test;
11
12use crate::extension_indexed_docs_provider::ExtensionIndexedDocsProvider;
13use crate::extension_manifest::SchemaVersion;
14use crate::extension_slash_command::ExtensionSlashCommand;
15use crate::{extension_lsp_adapter::ExtensionLspAdapter, wasm_host::wit};
16use anyhow::{anyhow, bail, Context as _, Result};
17use assistant_slash_command::SlashCommandRegistry;
18use async_compression::futures::bufread::GzipDecoder;
19use async_tar::Archive;
20use client::{telemetry::Telemetry, Client, ExtensionMetadata, GetExtensionsResponse};
21use collections::{btree_map, BTreeMap, HashSet};
22use extension_builder::{CompileExtensionOptions, ExtensionBuilder};
23use fs::{Fs, RemoveOptions};
24use futures::{
25 channel::{
26 mpsc::{unbounded, UnboundedSender},
27 oneshot,
28 },
29 io::BufReader,
30 select_biased, AsyncReadExt as _, Future, FutureExt as _, StreamExt as _,
31};
32use gpui::{
33 actions, AppContext, AsyncAppContext, Context, EventEmitter, Global, Model, ModelContext, Task,
34 WeakModel,
35};
36use http_client::{AsyncBody, HttpClient, HttpClientWithUrl};
37use indexed_docs::{IndexedDocsRegistry, ProviderId};
38use language::{
39 LanguageConfig, LanguageMatcher, LanguageQueries, LanguageRegistry, QUERY_FILENAME_PREFIXES,
40};
41use node_runtime::NodeRuntime;
42use project::ContextProviderWithTasks;
43use release_channel::ReleaseChannel;
44use semantic_version::SemanticVersion;
45use serde::{Deserialize, Serialize};
46use settings::Settings;
47use snippet_provider::SnippetRegistry;
48use std::ops::RangeInclusive;
49use std::str::FromStr;
50use std::{
51 cmp::Ordering,
52 path::{self, Path, PathBuf},
53 sync::Arc,
54 time::{Duration, Instant},
55};
56use theme::{ThemeRegistry, ThemeSettings};
57use url::Url;
58use util::{maybe, ResultExt};
59use wasm_host::{
60 wit::{is_supported_wasm_api_version, wasm_api_version_range},
61 WasmExtension, WasmHost,
62};
63
64pub use extension_manifest::{
65 ExtensionLibraryKind, ExtensionManifest, GrammarManifestEntry, OldExtensionManifest,
66};
67pub use extension_settings::ExtensionSettings;
68
69const RELOAD_DEBOUNCE_DURATION: Duration = Duration::from_millis(200);
70const FS_WATCH_LATENCY: Duration = Duration::from_millis(100);
71
72/// The current extension [`SchemaVersion`] supported by Zed.
73const CURRENT_SCHEMA_VERSION: SchemaVersion = SchemaVersion(1);
74
75/// Returns the [`SchemaVersion`] range that is compatible with this version of Zed.
76pub fn schema_version_range() -> RangeInclusive<SchemaVersion> {
77 SchemaVersion::ZERO..=CURRENT_SCHEMA_VERSION
78}
79
80/// Returns whether the given extension version is compatible with this version of Zed.
81pub fn is_version_compatible(
82 release_channel: ReleaseChannel,
83 extension_version: &ExtensionMetadata,
84) -> bool {
85 let schema_version = extension_version.manifest.schema_version.unwrap_or(0);
86 if CURRENT_SCHEMA_VERSION.0 < schema_version {
87 return false;
88 }
89
90 if let Some(wasm_api_version) = extension_version
91 .manifest
92 .wasm_api_version
93 .as_ref()
94 .and_then(|wasm_api_version| SemanticVersion::from_str(wasm_api_version).ok())
95 {
96 if !is_supported_wasm_api_version(release_channel, wasm_api_version) {
97 return false;
98 }
99 }
100
101 true
102}
103
104pub struct ExtensionStore {
105 builder: Arc<ExtensionBuilder>,
106 extension_index: ExtensionIndex,
107 fs: Arc<dyn Fs>,
108 http_client: Arc<HttpClientWithUrl>,
109 telemetry: Option<Arc<Telemetry>>,
110 reload_tx: UnboundedSender<Option<Arc<str>>>,
111 reload_complete_senders: Vec<oneshot::Sender<()>>,
112 installed_dir: PathBuf,
113 outstanding_operations: BTreeMap<Arc<str>, ExtensionOperation>,
114 index_path: PathBuf,
115 language_registry: Arc<LanguageRegistry>,
116 theme_registry: Arc<ThemeRegistry>,
117 slash_command_registry: Arc<SlashCommandRegistry>,
118 indexed_docs_registry: Arc<IndexedDocsRegistry>,
119 snippet_registry: Arc<SnippetRegistry>,
120 modified_extensions: HashSet<Arc<str>>,
121 wasm_host: Arc<WasmHost>,
122 wasm_extensions: Vec<(Arc<ExtensionManifest>, WasmExtension)>,
123 tasks: Vec<Task<()>>,
124}
125
126#[derive(Clone, Copy)]
127pub enum ExtensionOperation {
128 Upgrade,
129 Install,
130 Remove,
131}
132
133#[derive(Clone)]
134pub enum Event {
135 ExtensionsUpdated,
136 StartedReloading,
137 ExtensionInstalled(Arc<str>),
138 ExtensionFailedToLoad(Arc<str>),
139}
140
141impl EventEmitter<Event> for ExtensionStore {}
142
143struct GlobalExtensionStore(Model<ExtensionStore>);
144
145impl Global for GlobalExtensionStore {}
146
147#[derive(Debug, Deserialize, Serialize, Default, PartialEq, Eq)]
148pub struct ExtensionIndex {
149 pub extensions: BTreeMap<Arc<str>, ExtensionIndexEntry>,
150 pub themes: BTreeMap<Arc<str>, ExtensionIndexThemeEntry>,
151 pub languages: BTreeMap<Arc<str>, ExtensionIndexLanguageEntry>,
152}
153
154#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
155pub struct ExtensionIndexEntry {
156 pub manifest: Arc<ExtensionManifest>,
157 pub dev: bool,
158}
159
160#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Deserialize, Serialize)]
161pub struct ExtensionIndexThemeEntry {
162 extension: Arc<str>,
163 path: PathBuf,
164}
165
166#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Deserialize, Serialize)]
167pub struct ExtensionIndexLanguageEntry {
168 extension: Arc<str>,
169 path: PathBuf,
170 matcher: LanguageMatcher,
171 grammar: Option<Arc<str>>,
172}
173
174actions!(zed, [ReloadExtensions]);
175
176pub fn init(
177 fs: Arc<dyn Fs>,
178 client: Arc<Client>,
179 node_runtime: Arc<dyn NodeRuntime>,
180 language_registry: Arc<LanguageRegistry>,
181 theme_registry: Arc<ThemeRegistry>,
182 cx: &mut AppContext,
183) {
184 ExtensionSettings::register(cx);
185
186 let store = cx.new_model(move |cx| {
187 ExtensionStore::new(
188 paths::extensions_dir().clone(),
189 None,
190 fs,
191 client.http_client().clone(),
192 Some(client.telemetry().clone()),
193 node_runtime,
194 language_registry,
195 theme_registry,
196 SlashCommandRegistry::global(cx),
197 IndexedDocsRegistry::global(cx),
198 SnippetRegistry::global(cx),
199 cx,
200 )
201 });
202
203 cx.on_action(|_: &ReloadExtensions, cx| {
204 let store = cx.global::<GlobalExtensionStore>().0.clone();
205 store.update(cx, |store, cx| drop(store.reload(None, cx)));
206 });
207
208 cx.set_global(GlobalExtensionStore(store));
209}
210
211impl ExtensionStore {
212 pub fn try_global(cx: &AppContext) -> Option<Model<Self>> {
213 cx.try_global::<GlobalExtensionStore>()
214 .map(|store| store.0.clone())
215 }
216
217 pub fn global(cx: &AppContext) -> Model<Self> {
218 cx.global::<GlobalExtensionStore>().0.clone()
219 }
220
221 #[allow(clippy::too_many_arguments)]
222 pub fn new(
223 extensions_dir: PathBuf,
224 build_dir: Option<PathBuf>,
225 fs: Arc<dyn Fs>,
226 http_client: Arc<HttpClientWithUrl>,
227 telemetry: Option<Arc<Telemetry>>,
228 node_runtime: Arc<dyn NodeRuntime>,
229 language_registry: Arc<LanguageRegistry>,
230 theme_registry: Arc<ThemeRegistry>,
231 slash_command_registry: Arc<SlashCommandRegistry>,
232 indexed_docs_registry: Arc<IndexedDocsRegistry>,
233 snippet_registry: Arc<SnippetRegistry>,
234 cx: &mut ModelContext<Self>,
235 ) -> Self {
236 let work_dir = extensions_dir.join("work");
237 let build_dir = build_dir.unwrap_or_else(|| extensions_dir.join("build"));
238 let installed_dir = extensions_dir.join("installed");
239 let index_path = extensions_dir.join("index.json");
240
241 let (reload_tx, mut reload_rx) = unbounded();
242 let mut this = Self {
243 extension_index: Default::default(),
244 installed_dir,
245 index_path,
246 builder: Arc::new(ExtensionBuilder::new(
247 // Construct a real HTTP client for the extension builder, as we
248 // don't want to use a fake one in the tests.
249 ::http_client::client(None, http_client.proxy().cloned()),
250 build_dir,
251 )),
252 outstanding_operations: Default::default(),
253 modified_extensions: Default::default(),
254 reload_complete_senders: Vec::new(),
255 wasm_host: WasmHost::new(
256 fs.clone(),
257 http_client.clone(),
258 node_runtime,
259 language_registry.clone(),
260 work_dir,
261 cx,
262 ),
263 wasm_extensions: Vec::new(),
264 fs,
265 http_client,
266 telemetry,
267 language_registry,
268 theme_registry,
269 slash_command_registry,
270 indexed_docs_registry,
271 snippet_registry,
272 reload_tx,
273 tasks: Vec::new(),
274 };
275
276 // The extensions store maintains an index file, which contains a complete
277 // list of the installed extensions and the resources that they provide.
278 // This index is loaded synchronously on startup.
279 let (index_content, index_metadata, extensions_metadata) =
280 cx.background_executor().block(async {
281 futures::join!(
282 this.fs.load(&this.index_path),
283 this.fs.metadata(&this.index_path),
284 this.fs.metadata(&this.installed_dir),
285 )
286 });
287
288 // Normally, there is no need to rebuild the index. But if the index file
289 // is invalid or is out-of-date according to the filesystem mtimes, then
290 // it must be asynchronously rebuilt.
291 let mut extension_index = ExtensionIndex::default();
292 let mut extension_index_needs_rebuild = true;
293 if let Some(index_content) = index_content.ok() {
294 if let Some(index) = serde_json::from_str(&index_content).log_err() {
295 extension_index = index;
296 if let (Ok(Some(index_metadata)), Ok(Some(extensions_metadata))) =
297 (index_metadata, extensions_metadata)
298 {
299 if index_metadata.mtime > extensions_metadata.mtime {
300 extension_index_needs_rebuild = false;
301 }
302 }
303 }
304 }
305
306 // Immediately load all of the extensions in the initial manifest. If the
307 // index needs to be rebuild, then enqueue
308 let load_initial_extensions = this.extensions_updated(extension_index, cx);
309 let mut reload_future = None;
310 if extension_index_needs_rebuild {
311 reload_future = Some(this.reload(None, cx));
312 }
313
314 cx.spawn(|this, mut cx| async move {
315 if let Some(future) = reload_future {
316 future.await;
317 }
318 this.update(&mut cx, |this, cx| this.auto_install_extensions(cx))
319 .ok();
320 this.update(&mut cx, |this, cx| this.check_for_updates(cx))
321 .ok();
322 })
323 .detach();
324
325 // Perform all extension loading in a single task to ensure that we
326 // never attempt to simultaneously load/unload extensions from multiple
327 // parallel tasks.
328 this.tasks.push(cx.spawn(|this, mut cx| {
329 async move {
330 load_initial_extensions.await;
331
332 let mut debounce_timer = cx
333 .background_executor()
334 .spawn(futures::future::pending())
335 .fuse();
336 loop {
337 select_biased! {
338 _ = debounce_timer => {
339 let index = this
340 .update(&mut cx, |this, cx| this.rebuild_extension_index(cx))?
341 .await;
342 this.update(&mut cx, |this, cx| this.extensions_updated(index, cx))?
343 .await;
344 }
345 extension_id = reload_rx.next() => {
346 let Some(extension_id) = extension_id else { break; };
347 this.update(&mut cx, |this, _| {
348 this.modified_extensions.extend(extension_id);
349 })?;
350 debounce_timer = cx
351 .background_executor()
352 .timer(RELOAD_DEBOUNCE_DURATION)
353 .fuse();
354 }
355 }
356 }
357
358 anyhow::Ok(())
359 }
360 .map(drop)
361 }));
362
363 // Watch the installed extensions directory for changes. Whenever changes are
364 // detected, rebuild the extension index, and load/unload any extensions that
365 // have been added, removed, or modified.
366 this.tasks.push(cx.background_executor().spawn({
367 let fs = this.fs.clone();
368 let reload_tx = this.reload_tx.clone();
369 let installed_dir = this.installed_dir.clone();
370 async move {
371 let (mut paths, _) = fs.watch(&installed_dir, FS_WATCH_LATENCY).await;
372 while let Some(paths) = paths.next().await {
373 for path in paths {
374 let Ok(event_path) = path.strip_prefix(&installed_dir) else {
375 continue;
376 };
377
378 if let Some(path::Component::Normal(extension_dir_name)) =
379 event_path.components().next()
380 {
381 if let Some(extension_id) = extension_dir_name.to_str() {
382 reload_tx.unbounded_send(Some(extension_id.into())).ok();
383 }
384 }
385 }
386 }
387 }
388 }));
389
390 this
391 }
392
393 fn reload(
394 &mut self,
395 modified_extension: Option<Arc<str>>,
396 cx: &mut ModelContext<Self>,
397 ) -> impl Future<Output = ()> {
398 let (tx, rx) = oneshot::channel();
399 self.reload_complete_senders.push(tx);
400 self.reload_tx
401 .unbounded_send(modified_extension)
402 .expect("reload task exited");
403 cx.emit(Event::StartedReloading);
404
405 async move {
406 rx.await.ok();
407 }
408 }
409
410 fn extensions_dir(&self) -> PathBuf {
411 self.installed_dir.clone()
412 }
413
414 pub fn outstanding_operations(&self) -> &BTreeMap<Arc<str>, ExtensionOperation> {
415 &self.outstanding_operations
416 }
417
418 pub fn installed_extensions(&self) -> &BTreeMap<Arc<str>, ExtensionIndexEntry> {
419 &self.extension_index.extensions
420 }
421
422 pub fn dev_extensions(&self) -> impl Iterator<Item = &Arc<ExtensionManifest>> {
423 self.extension_index
424 .extensions
425 .values()
426 .filter_map(|extension| extension.dev.then_some(&extension.manifest))
427 }
428
429 /// Returns the names of themes provided by extensions.
430 pub fn extension_themes<'a>(
431 &'a self,
432 extension_id: &'a str,
433 ) -> impl Iterator<Item = &'a Arc<str>> {
434 self.extension_index
435 .themes
436 .iter()
437 .filter_map(|(name, theme)| theme.extension.as_ref().eq(extension_id).then_some(name))
438 }
439
440 pub fn fetch_extensions(
441 &self,
442 search: Option<&str>,
443 cx: &mut ModelContext<Self>,
444 ) -> Task<Result<Vec<ExtensionMetadata>>> {
445 let version = CURRENT_SCHEMA_VERSION.to_string();
446 let mut query = vec![("max_schema_version", version.as_str())];
447 if let Some(search) = search {
448 query.push(("filter", search));
449 }
450
451 self.fetch_extensions_from_api("/extensions", &query, cx)
452 }
453
454 pub fn fetch_extensions_with_update_available(
455 &mut self,
456 cx: &mut ModelContext<Self>,
457 ) -> Task<Result<Vec<ExtensionMetadata>>> {
458 let schema_versions = schema_version_range();
459 let wasm_api_versions = wasm_api_version_range(ReleaseChannel::global(cx));
460 let extension_settings = ExtensionSettings::get_global(cx);
461 let extension_ids = self
462 .extension_index
463 .extensions
464 .iter()
465 .filter(|(id, entry)| !entry.dev && extension_settings.should_auto_update(id))
466 .map(|(id, _)| id.as_ref())
467 .collect::<Vec<_>>()
468 .join(",");
469 let task = self.fetch_extensions_from_api(
470 "/extensions/updates",
471 &[
472 ("min_schema_version", &schema_versions.start().to_string()),
473 ("max_schema_version", &schema_versions.end().to_string()),
474 (
475 "min_wasm_api_version",
476 &wasm_api_versions.start().to_string(),
477 ),
478 ("max_wasm_api_version", &wasm_api_versions.end().to_string()),
479 ("ids", &extension_ids),
480 ],
481 cx,
482 );
483 cx.spawn(move |this, mut cx| async move {
484 let extensions = task.await?;
485 this.update(&mut cx, |this, _cx| {
486 extensions
487 .into_iter()
488 .filter(|extension| {
489 this.extension_index.extensions.get(&extension.id).map_or(
490 true,
491 |installed_extension| {
492 installed_extension.manifest.version != extension.manifest.version
493 },
494 )
495 })
496 .collect()
497 })
498 })
499 }
500
501 pub fn fetch_extension_versions(
502 &self,
503 extension_id: &str,
504 cx: &mut ModelContext<Self>,
505 ) -> Task<Result<Vec<ExtensionMetadata>>> {
506 self.fetch_extensions_from_api(&format!("/extensions/{extension_id}"), &[], cx)
507 }
508
509 /// Installs any extensions that should be included with Zed by default.
510 ///
511 /// This can be used to make certain functionality provided by extensions
512 /// available out-of-the-box.
513 pub fn auto_install_extensions(&mut self, cx: &mut ModelContext<Self>) {
514 let extension_settings = ExtensionSettings::get_global(cx);
515
516 let extensions_to_install = extension_settings
517 .auto_install_extensions
518 .keys()
519 .filter(|extension_id| extension_settings.should_auto_install(extension_id))
520 .filter(|extension_id| {
521 let is_already_installed = self
522 .extension_index
523 .extensions
524 .contains_key(extension_id.as_ref());
525 !is_already_installed
526 })
527 .cloned()
528 .collect::<Vec<_>>();
529
530 cx.spawn(move |this, mut cx| async move {
531 for extension_id in extensions_to_install {
532 this.update(&mut cx, |this, cx| {
533 this.install_latest_extension(extension_id.clone(), cx);
534 })
535 .ok();
536 }
537 })
538 .detach();
539 }
540
541 pub fn check_for_updates(&mut self, cx: &mut ModelContext<Self>) {
542 let task = self.fetch_extensions_with_update_available(cx);
543 cx.spawn(move |this, mut cx| async move {
544 Self::upgrade_extensions(this, task.await?, &mut cx).await
545 })
546 .detach();
547 }
548
549 async fn upgrade_extensions(
550 this: WeakModel<Self>,
551 extensions: Vec<ExtensionMetadata>,
552 cx: &mut AsyncAppContext,
553 ) -> Result<()> {
554 for extension in extensions {
555 let task = this.update(cx, |this, cx| {
556 if let Some(installed_extension) =
557 this.extension_index.extensions.get(&extension.id)
558 {
559 let installed_version =
560 SemanticVersion::from_str(&installed_extension.manifest.version).ok()?;
561 let latest_version =
562 SemanticVersion::from_str(&extension.manifest.version).ok()?;
563
564 if installed_version >= latest_version {
565 return None;
566 }
567 }
568
569 Some(this.upgrade_extension(extension.id, extension.manifest.version, cx))
570 })?;
571
572 if let Some(task) = task {
573 task.await.log_err();
574 }
575 }
576 anyhow::Ok(())
577 }
578
579 fn fetch_extensions_from_api(
580 &self,
581 path: &str,
582 query: &[(&str, &str)],
583 cx: &mut ModelContext<'_, ExtensionStore>,
584 ) -> Task<Result<Vec<ExtensionMetadata>>> {
585 let url = self.http_client.build_zed_api_url(path, &query);
586 let http_client = self.http_client.clone();
587 cx.spawn(move |_, _| async move {
588 let mut response = http_client
589 .get(&url?.as_ref(), AsyncBody::empty(), true)
590 .await?;
591
592 let mut body = Vec::new();
593 response
594 .body_mut()
595 .read_to_end(&mut body)
596 .await
597 .context("error reading extensions")?;
598
599 if response.status().is_client_error() {
600 let text = String::from_utf8_lossy(body.as_slice());
601 bail!(
602 "status error {}, response: {text:?}",
603 response.status().as_u16()
604 );
605 }
606
607 let response: GetExtensionsResponse = serde_json::from_slice(&body)?;
608 Ok(response.data)
609 })
610 }
611
612 pub fn install_extension(
613 &mut self,
614 extension_id: Arc<str>,
615 version: Arc<str>,
616 cx: &mut ModelContext<Self>,
617 ) {
618 self.install_or_upgrade_extension(extension_id, version, ExtensionOperation::Install, cx)
619 .detach_and_log_err(cx);
620 }
621
622 fn install_or_upgrade_extension_at_endpoint(
623 &mut self,
624 extension_id: Arc<str>,
625 url: Url,
626 operation: ExtensionOperation,
627 cx: &mut ModelContext<Self>,
628 ) -> Task<Result<()>> {
629 let extension_dir = self.installed_dir.join(extension_id.as_ref());
630 let http_client = self.http_client.clone();
631 let fs = self.fs.clone();
632
633 match self.outstanding_operations.entry(extension_id.clone()) {
634 btree_map::Entry::Occupied(_) => return Task::ready(Ok(())),
635 btree_map::Entry::Vacant(e) => e.insert(operation),
636 };
637 cx.notify();
638
639 cx.spawn(move |this, mut cx| async move {
640 let _finish = util::defer({
641 let this = this.clone();
642 let mut cx = cx.clone();
643 let extension_id = extension_id.clone();
644 move || {
645 this.update(&mut cx, |this, cx| {
646 this.outstanding_operations.remove(extension_id.as_ref());
647 cx.notify();
648 })
649 .ok();
650 }
651 });
652
653 let mut response = http_client
654 .get(&url.as_ref(), Default::default(), true)
655 .await
656 .map_err(|err| anyhow!("error downloading extension: {}", err))?;
657
658 fs.remove_dir(
659 &extension_dir,
660 RemoveOptions {
661 recursive: true,
662 ignore_if_not_exists: true,
663 },
664 )
665 .await?;
666
667 let content_length = response
668 .headers()
669 .get(isahc::http::header::CONTENT_LENGTH)
670 .and_then(|value| value.to_str().ok()?.parse::<usize>().ok());
671
672 let mut body = BufReader::new(response.body_mut());
673 let mut tar_gz_bytes = Vec::new();
674 body.read_to_end(&mut tar_gz_bytes).await?;
675
676 if let Some(content_length) = content_length {
677 let actual_len = tar_gz_bytes.len();
678 if content_length != actual_len {
679 bail!("downloaded extension size {actual_len} does not match content length {content_length}");
680 }
681 }
682 let decompressed_bytes = GzipDecoder::new(BufReader::new(tar_gz_bytes.as_slice()));
683 let archive = Archive::new(decompressed_bytes);
684 archive.unpack(extension_dir).await?;
685 this.update(&mut cx, |this, cx| {
686 this.reload(Some(extension_id.clone()), cx)
687 })?
688 .await;
689
690 match operation {
691 ExtensionOperation::Install => {
692 this.update(&mut cx, |_, cx| {
693 cx.emit(Event::ExtensionInstalled(extension_id));
694 })
695 .ok();
696 }
697 _ => {}
698 }
699
700 anyhow::Ok(())
701 })
702 }
703
704 pub fn install_latest_extension(
705 &mut self,
706 extension_id: Arc<str>,
707 cx: &mut ModelContext<Self>,
708 ) {
709 log::info!("installing extension {extension_id} latest version");
710
711 let schema_versions = schema_version_range();
712 let wasm_api_versions = wasm_api_version_range(ReleaseChannel::global(cx));
713
714 let Some(url) = self
715 .http_client
716 .build_zed_api_url(
717 &format!("/extensions/{extension_id}/download"),
718 &[
719 ("min_schema_version", &schema_versions.start().to_string()),
720 ("max_schema_version", &schema_versions.end().to_string()),
721 (
722 "min_wasm_api_version",
723 &wasm_api_versions.start().to_string(),
724 ),
725 ("max_wasm_api_version", &wasm_api_versions.end().to_string()),
726 ],
727 )
728 .log_err()
729 else {
730 return;
731 };
732
733 self.install_or_upgrade_extension_at_endpoint(
734 extension_id,
735 url,
736 ExtensionOperation::Install,
737 cx,
738 )
739 .detach_and_log_err(cx);
740 }
741
742 pub fn upgrade_extension(
743 &mut self,
744 extension_id: Arc<str>,
745 version: Arc<str>,
746 cx: &mut ModelContext<Self>,
747 ) -> Task<Result<()>> {
748 self.install_or_upgrade_extension(extension_id, version, ExtensionOperation::Upgrade, cx)
749 }
750
751 fn install_or_upgrade_extension(
752 &mut self,
753 extension_id: Arc<str>,
754 version: Arc<str>,
755 operation: ExtensionOperation,
756 cx: &mut ModelContext<Self>,
757 ) -> Task<Result<()>> {
758 log::info!("installing extension {extension_id} {version}");
759 let Some(url) = self
760 .http_client
761 .build_zed_api_url(
762 &format!("/extensions/{extension_id}/{version}/download"),
763 &[],
764 )
765 .log_err()
766 else {
767 return Task::ready(Ok(()));
768 };
769
770 self.install_or_upgrade_extension_at_endpoint(extension_id, url, operation, cx)
771 }
772
773 pub fn uninstall_extension(&mut self, extension_id: Arc<str>, cx: &mut ModelContext<Self>) {
774 let extension_dir = self.installed_dir.join(extension_id.as_ref());
775 let fs = self.fs.clone();
776
777 match self.outstanding_operations.entry(extension_id.clone()) {
778 btree_map::Entry::Occupied(_) => return,
779 btree_map::Entry::Vacant(e) => e.insert(ExtensionOperation::Remove),
780 };
781
782 cx.spawn(move |this, mut cx| async move {
783 let _finish = util::defer({
784 let this = this.clone();
785 let mut cx = cx.clone();
786 let extension_id = extension_id.clone();
787 move || {
788 this.update(&mut cx, |this, cx| {
789 this.outstanding_operations.remove(extension_id.as_ref());
790 cx.notify();
791 })
792 .ok();
793 }
794 });
795
796 fs.remove_dir(
797 &extension_dir,
798 RemoveOptions {
799 recursive: true,
800 ignore_if_not_exists: true,
801 },
802 )
803 .await?;
804
805 this.update(&mut cx, |this, cx| this.reload(None, cx))?
806 .await;
807 anyhow::Ok(())
808 })
809 .detach_and_log_err(cx)
810 }
811
812 pub fn install_dev_extension(
813 &mut self,
814 extension_source_path: PathBuf,
815 cx: &mut ModelContext<Self>,
816 ) -> Task<Result<()>> {
817 let extensions_dir = self.extensions_dir();
818 let fs = self.fs.clone();
819 let builder = self.builder.clone();
820
821 cx.spawn(move |this, mut cx| async move {
822 let mut extension_manifest =
823 ExtensionManifest::load(fs.clone(), &extension_source_path).await?;
824 let extension_id = extension_manifest.id.clone();
825
826 if !this.update(&mut cx, |this, cx| {
827 match this.outstanding_operations.entry(extension_id.clone()) {
828 btree_map::Entry::Occupied(_) => return false,
829 btree_map::Entry::Vacant(e) => e.insert(ExtensionOperation::Remove),
830 };
831 cx.notify();
832 true
833 })? {
834 return Ok(());
835 }
836
837 let _finish = util::defer({
838 let this = this.clone();
839 let mut cx = cx.clone();
840 let extension_id = extension_id.clone();
841 move || {
842 this.update(&mut cx, |this, cx| {
843 this.outstanding_operations.remove(extension_id.as_ref());
844 cx.notify();
845 })
846 .ok();
847 }
848 });
849
850 cx.background_executor()
851 .spawn({
852 let extension_source_path = extension_source_path.clone();
853 async move {
854 builder
855 .compile_extension(
856 &extension_source_path,
857 &mut extension_manifest,
858 CompileExtensionOptions { release: false },
859 )
860 .await
861 }
862 })
863 .await?;
864
865 let output_path = &extensions_dir.join(extension_id.as_ref());
866 if let Some(metadata) = fs.metadata(&output_path).await? {
867 if metadata.is_symlink {
868 fs.remove_file(
869 &output_path,
870 RemoveOptions {
871 recursive: false,
872 ignore_if_not_exists: true,
873 },
874 )
875 .await?;
876 } else {
877 bail!("extension {extension_id} is already installed");
878 }
879 }
880
881 fs.create_symlink(output_path, extension_source_path)
882 .await?;
883
884 this.update(&mut cx, |this, cx| this.reload(None, cx))?
885 .await;
886 Ok(())
887 })
888 }
889
890 pub fn rebuild_dev_extension(&mut self, extension_id: Arc<str>, cx: &mut ModelContext<Self>) {
891 let path = self.installed_dir.join(extension_id.as_ref());
892 let builder = self.builder.clone();
893 let fs = self.fs.clone();
894
895 match self.outstanding_operations.entry(extension_id.clone()) {
896 btree_map::Entry::Occupied(_) => return,
897 btree_map::Entry::Vacant(e) => e.insert(ExtensionOperation::Upgrade),
898 };
899
900 cx.notify();
901 let compile = cx.background_executor().spawn(async move {
902 let mut manifest = ExtensionManifest::load(fs, &path).await?;
903 builder
904 .compile_extension(
905 &path,
906 &mut manifest,
907 CompileExtensionOptions { release: true },
908 )
909 .await
910 });
911
912 cx.spawn(|this, mut cx| async move {
913 let result = compile.await;
914
915 this.update(&mut cx, |this, cx| {
916 this.outstanding_operations.remove(&extension_id);
917 cx.notify();
918 })?;
919
920 if result.is_ok() {
921 this.update(&mut cx, |this, cx| this.reload(Some(extension_id), cx))?
922 .await;
923 }
924
925 result
926 })
927 .detach_and_log_err(cx)
928 }
929
930 /// Updates the set of installed extensions.
931 ///
932 /// First, this unloads any themes, languages, or grammars that are
933 /// no longer in the manifest, or whose files have changed on disk.
934 /// Then it loads any themes, languages, or grammars that are newly
935 /// added to the manifest, or whose files have changed on disk.
936 fn extensions_updated(
937 &mut self,
938 new_index: ExtensionIndex,
939 cx: &mut ModelContext<Self>,
940 ) -> Task<()> {
941 let old_index = &self.extension_index;
942
943 // Determine which extensions need to be loaded and unloaded, based
944 // on the changes to the manifest and the extensions that we know have been
945 // modified.
946 let mut extensions_to_unload = Vec::default();
947 let mut extensions_to_load = Vec::default();
948 {
949 let mut old_keys = old_index.extensions.iter().peekable();
950 let mut new_keys = new_index.extensions.iter().peekable();
951 loop {
952 match (old_keys.peek(), new_keys.peek()) {
953 (None, None) => break,
954 (None, Some(_)) => {
955 extensions_to_load.push(new_keys.next().unwrap().0.clone());
956 }
957 (Some(_), None) => {
958 extensions_to_unload.push(old_keys.next().unwrap().0.clone());
959 }
960 (Some((old_key, _)), Some((new_key, _))) => match old_key.cmp(&new_key) {
961 Ordering::Equal => {
962 let (old_key, old_value) = old_keys.next().unwrap();
963 let (new_key, new_value) = new_keys.next().unwrap();
964 if old_value != new_value || self.modified_extensions.contains(old_key)
965 {
966 extensions_to_unload.push(old_key.clone());
967 extensions_to_load.push(new_key.clone());
968 }
969 }
970 Ordering::Less => {
971 extensions_to_unload.push(old_keys.next().unwrap().0.clone());
972 }
973 Ordering::Greater => {
974 extensions_to_load.push(new_keys.next().unwrap().0.clone());
975 }
976 },
977 }
978 }
979 self.modified_extensions.clear();
980 }
981
982 if extensions_to_load.is_empty() && extensions_to_unload.is_empty() {
983 return Task::ready(());
984 }
985
986 let reload_count = extensions_to_unload
987 .iter()
988 .filter(|id| extensions_to_load.contains(id))
989 .count();
990
991 log::info!(
992 "extensions updated. loading {}, reloading {}, unloading {}",
993 extensions_to_load.len() - reload_count,
994 reload_count,
995 extensions_to_unload.len() - reload_count
996 );
997
998 if let Some(telemetry) = &self.telemetry {
999 for extension_id in &extensions_to_load {
1000 if let Some(extension) = new_index.extensions.get(extension_id) {
1001 telemetry.report_extension_event(
1002 extension_id.clone(),
1003 extension.manifest.version.clone(),
1004 );
1005 }
1006 }
1007 }
1008
1009 let themes_to_remove = old_index
1010 .themes
1011 .iter()
1012 .filter_map(|(name, entry)| {
1013 if extensions_to_unload.contains(&entry.extension) {
1014 Some(name.clone().into())
1015 } else {
1016 None
1017 }
1018 })
1019 .collect::<Vec<_>>();
1020 let languages_to_remove = old_index
1021 .languages
1022 .iter()
1023 .filter_map(|(name, entry)| {
1024 if extensions_to_unload.contains(&entry.extension) {
1025 Some(name.clone())
1026 } else {
1027 None
1028 }
1029 })
1030 .collect::<Vec<_>>();
1031 let mut grammars_to_remove = Vec::new();
1032 for extension_id in &extensions_to_unload {
1033 let Some(extension) = old_index.extensions.get(extension_id) else {
1034 continue;
1035 };
1036 grammars_to_remove.extend(extension.manifest.grammars.keys().cloned());
1037 for (language_server_name, config) in extension.manifest.language_servers.iter() {
1038 for language in config.languages() {
1039 self.language_registry
1040 .remove_lsp_adapter(&language, language_server_name);
1041 }
1042 }
1043 }
1044
1045 self.wasm_extensions
1046 .retain(|(extension, _)| !extensions_to_unload.contains(&extension.id));
1047 self.theme_registry.remove_user_themes(&themes_to_remove);
1048 self.language_registry
1049 .remove_languages(&languages_to_remove, &grammars_to_remove);
1050
1051 let languages_to_add = new_index
1052 .languages
1053 .iter()
1054 .filter(|(_, entry)| extensions_to_load.contains(&entry.extension))
1055 .collect::<Vec<_>>();
1056 let mut grammars_to_add = Vec::new();
1057 let mut themes_to_add = Vec::new();
1058 let mut snippets_to_add = Vec::new();
1059 for extension_id in &extensions_to_load {
1060 let Some(extension) = new_index.extensions.get(extension_id) else {
1061 continue;
1062 };
1063
1064 grammars_to_add.extend(extension.manifest.grammars.keys().map(|grammar_name| {
1065 let mut grammar_path = self.installed_dir.clone();
1066 grammar_path.extend([extension_id.as_ref(), "grammars"]);
1067 grammar_path.push(grammar_name.as_ref());
1068 grammar_path.set_extension("wasm");
1069 (grammar_name.clone(), grammar_path)
1070 }));
1071 themes_to_add.extend(extension.manifest.themes.iter().map(|theme_path| {
1072 let mut path = self.installed_dir.clone();
1073 path.extend([Path::new(extension_id.as_ref()), theme_path.as_path()]);
1074 path
1075 }));
1076 snippets_to_add.extend(extension.manifest.snippets.iter().map(|snippets_path| {
1077 let mut path = self.installed_dir.clone();
1078 path.extend([Path::new(extension_id.as_ref()), snippets_path.as_path()]);
1079 path
1080 }));
1081 }
1082
1083 self.language_registry
1084 .register_wasm_grammars(grammars_to_add);
1085
1086 for (language_name, language) in languages_to_add {
1087 let mut language_path = self.installed_dir.clone();
1088 language_path.extend([
1089 Path::new(language.extension.as_ref()),
1090 language.path.as_path(),
1091 ]);
1092 self.language_registry.register_language(
1093 language_name.clone(),
1094 language.grammar.clone(),
1095 language.matcher.clone(),
1096 move || {
1097 let config = std::fs::read_to_string(language_path.join("config.toml"))?;
1098 let config: LanguageConfig = ::toml::from_str(&config)?;
1099 let queries = load_plugin_queries(&language_path);
1100 let tasks = std::fs::read_to_string(language_path.join("tasks.json"))
1101 .ok()
1102 .and_then(|contents| {
1103 let definitions = serde_json_lenient::from_str(&contents).log_err()?;
1104 Some(Arc::new(ContextProviderWithTasks::new(definitions)) as Arc<_>)
1105 });
1106
1107 Ok((config, queries, tasks))
1108 },
1109 );
1110 }
1111
1112 let fs = self.fs.clone();
1113 let wasm_host = self.wasm_host.clone();
1114 let root_dir = self.installed_dir.clone();
1115 let theme_registry = self.theme_registry.clone();
1116 let snippet_registry = self.snippet_registry.clone();
1117 let extension_entries = extensions_to_load
1118 .iter()
1119 .filter_map(|name| new_index.extensions.get(name).cloned())
1120 .collect::<Vec<_>>();
1121
1122 self.extension_index = new_index;
1123 cx.notify();
1124 cx.emit(Event::ExtensionsUpdated);
1125
1126 cx.spawn(|this, mut cx| async move {
1127 cx.background_executor()
1128 .spawn({
1129 let fs = fs.clone();
1130 async move {
1131 for theme_path in &themes_to_add {
1132 theme_registry
1133 .load_user_theme(&theme_path, fs.clone())
1134 .await
1135 .log_err();
1136 }
1137
1138 for snippets_path in &snippets_to_add {
1139 if let Some(snippets_contents) = fs.load(snippets_path).await.log_err()
1140 {
1141 snippet_registry
1142 .register_snippets(snippets_path, &snippets_contents)
1143 .log_err();
1144 }
1145 }
1146 }
1147 })
1148 .await;
1149
1150 let mut wasm_extensions = Vec::new();
1151 for extension in extension_entries {
1152 if extension.manifest.lib.kind.is_none() {
1153 continue;
1154 };
1155
1156 let wasm_extension = maybe!(async {
1157 let mut path = root_dir.clone();
1158 path.extend([extension.manifest.clone().id.as_ref(), "extension.wasm"]);
1159 let mut wasm_file = fs
1160 .open_sync(&path)
1161 .await
1162 .context("failed to open wasm file")?;
1163
1164 let mut wasm_bytes = Vec::new();
1165 wasm_file
1166 .read_to_end(&mut wasm_bytes)
1167 .context("failed to read wasm")?;
1168
1169 wasm_host
1170 .load_extension(
1171 wasm_bytes,
1172 extension.manifest.clone().clone(),
1173 cx.background_executor().clone(),
1174 )
1175 .await
1176 .with_context(|| {
1177 format!("failed to load wasm extension {}", extension.manifest.id)
1178 })
1179 })
1180 .await;
1181
1182 if let Some(wasm_extension) = wasm_extension.log_err() {
1183 wasm_extensions.push((extension.manifest.clone(), wasm_extension));
1184 } else {
1185 this.update(&mut cx, |_, cx| {
1186 cx.emit(Event::ExtensionFailedToLoad(extension.manifest.id.clone()))
1187 })
1188 .ok();
1189 }
1190 }
1191
1192 this.update(&mut cx, |this, cx| {
1193 this.reload_complete_senders.clear();
1194
1195 for (manifest, wasm_extension) in &wasm_extensions {
1196 for (language_server_id, language_server_config) in &manifest.language_servers {
1197 for language in language_server_config.languages() {
1198 this.language_registry.register_lsp_adapter(
1199 language.clone(),
1200 Arc::new(ExtensionLspAdapter {
1201 extension: wasm_extension.clone(),
1202 host: this.wasm_host.clone(),
1203 language_server_id: language_server_id.clone(),
1204 config: wit::LanguageServerConfig {
1205 name: language_server_id.0.to_string(),
1206 language_name: language.to_string(),
1207 },
1208 }),
1209 );
1210 }
1211 }
1212
1213 for (slash_command_name, slash_command) in &manifest.slash_commands {
1214 this.slash_command_registry.register_command(
1215 ExtensionSlashCommand {
1216 command: crate::wit::SlashCommand {
1217 name: slash_command_name.to_string(),
1218 description: slash_command.description.to_string(),
1219 // We don't currently expose this as a configurable option, as it currently drives
1220 // the `menu_text` on the `SlashCommand` trait, which is not used for slash commands
1221 // defined in extensions, as they are not able to be added to the menu.
1222 tooltip_text: String::new(),
1223 requires_argument: slash_command.requires_argument,
1224 },
1225 extension: wasm_extension.clone(),
1226 host: this.wasm_host.clone(),
1227 },
1228 false,
1229 );
1230 }
1231
1232 for (provider_id, _provider) in &manifest.indexed_docs_providers {
1233 this.indexed_docs_registry.register_provider(Box::new(
1234 ExtensionIndexedDocsProvider {
1235 extension: wasm_extension.clone(),
1236 host: this.wasm_host.clone(),
1237 id: ProviderId(provider_id.clone()),
1238 },
1239 ));
1240 }
1241 }
1242
1243 this.wasm_extensions.extend(wasm_extensions);
1244 ThemeSettings::reload_current_theme(cx)
1245 })
1246 .ok();
1247 })
1248 }
1249
1250 fn rebuild_extension_index(&self, cx: &mut ModelContext<Self>) -> Task<ExtensionIndex> {
1251 let fs = self.fs.clone();
1252 let work_dir = self.wasm_host.work_dir.clone();
1253 let extensions_dir = self.installed_dir.clone();
1254 let index_path = self.index_path.clone();
1255 cx.background_executor().spawn(async move {
1256 let start_time = Instant::now();
1257 let mut index = ExtensionIndex::default();
1258
1259 fs.create_dir(&work_dir).await.log_err();
1260 fs.create_dir(&extensions_dir).await.log_err();
1261
1262 let extension_paths = fs.read_dir(&extensions_dir).await;
1263 if let Ok(mut extension_paths) = extension_paths {
1264 while let Some(extension_dir) = extension_paths.next().await {
1265 let Ok(extension_dir) = extension_dir else {
1266 continue;
1267 };
1268
1269 if extension_dir
1270 .file_name()
1271 .map_or(false, |file_name| file_name == ".DS_Store")
1272 {
1273 continue;
1274 }
1275
1276 Self::add_extension_to_index(fs.clone(), extension_dir, &mut index)
1277 .await
1278 .log_err();
1279 }
1280 }
1281
1282 if let Ok(index_json) = serde_json::to_string_pretty(&index) {
1283 fs.save(&index_path, &index_json.as_str().into(), Default::default())
1284 .await
1285 .context("failed to save extension index")
1286 .log_err();
1287 }
1288
1289 log::info!("rebuilt extension index in {:?}", start_time.elapsed());
1290 index
1291 })
1292 }
1293
1294 async fn add_extension_to_index(
1295 fs: Arc<dyn Fs>,
1296 extension_dir: PathBuf,
1297 index: &mut ExtensionIndex,
1298 ) -> Result<()> {
1299 let mut extension_manifest = ExtensionManifest::load(fs.clone(), &extension_dir).await?;
1300 let extension_id = extension_manifest.id.clone();
1301
1302 // TODO: distinguish dev extensions more explicitly, by the absence
1303 // of a checksum file that we'll create when downloading normal extensions.
1304 let is_dev = fs
1305 .metadata(&extension_dir)
1306 .await?
1307 .ok_or_else(|| anyhow!("directory does not exist"))?
1308 .is_symlink;
1309
1310 if let Ok(mut language_paths) = fs.read_dir(&extension_dir.join("languages")).await {
1311 while let Some(language_path) = language_paths.next().await {
1312 let language_path = language_path?;
1313 let Ok(relative_path) = language_path.strip_prefix(&extension_dir) else {
1314 continue;
1315 };
1316 let Ok(Some(fs_metadata)) = fs.metadata(&language_path).await else {
1317 continue;
1318 };
1319 if !fs_metadata.is_dir {
1320 continue;
1321 }
1322 let config = fs.load(&language_path.join("config.toml")).await?;
1323 let config = ::toml::from_str::<LanguageConfig>(&config)?;
1324
1325 let relative_path = relative_path.to_path_buf();
1326 if !extension_manifest.languages.contains(&relative_path) {
1327 extension_manifest.languages.push(relative_path.clone());
1328 }
1329
1330 index.languages.insert(
1331 config.name.clone(),
1332 ExtensionIndexLanguageEntry {
1333 extension: extension_id.clone(),
1334 path: relative_path,
1335 matcher: config.matcher,
1336 grammar: config.grammar,
1337 },
1338 );
1339 }
1340 }
1341
1342 if let Ok(mut theme_paths) = fs.read_dir(&extension_dir.join("themes")).await {
1343 while let Some(theme_path) = theme_paths.next().await {
1344 let theme_path = theme_path?;
1345 let Ok(relative_path) = theme_path.strip_prefix(&extension_dir) else {
1346 continue;
1347 };
1348
1349 let Some(theme_family) = ThemeRegistry::read_user_theme(&theme_path, fs.clone())
1350 .await
1351 .log_err()
1352 else {
1353 continue;
1354 };
1355
1356 let relative_path = relative_path.to_path_buf();
1357 if !extension_manifest.themes.contains(&relative_path) {
1358 extension_manifest.themes.push(relative_path.clone());
1359 }
1360
1361 for theme in theme_family.themes {
1362 index.themes.insert(
1363 theme.name.into(),
1364 ExtensionIndexThemeEntry {
1365 extension: extension_id.clone(),
1366 path: relative_path.clone(),
1367 },
1368 );
1369 }
1370 }
1371 }
1372
1373 let extension_wasm_path = extension_dir.join("extension.wasm");
1374 if fs.is_file(&extension_wasm_path).await {
1375 extension_manifest
1376 .lib
1377 .kind
1378 .get_or_insert(ExtensionLibraryKind::Rust);
1379 }
1380
1381 index.extensions.insert(
1382 extension_id.clone(),
1383 ExtensionIndexEntry {
1384 dev: is_dev,
1385 manifest: Arc::new(extension_manifest),
1386 },
1387 );
1388
1389 Ok(())
1390 }
1391}
1392
1393fn load_plugin_queries(root_path: &Path) -> LanguageQueries {
1394 let mut result = LanguageQueries::default();
1395 if let Some(entries) = std::fs::read_dir(root_path).log_err() {
1396 for entry in entries {
1397 let Some(entry) = entry.log_err() else {
1398 continue;
1399 };
1400 let path = entry.path();
1401 if let Some(remainder) = path.strip_prefix(root_path).ok().and_then(|p| p.to_str()) {
1402 if !remainder.ends_with(".scm") {
1403 continue;
1404 }
1405 for (name, query) in QUERY_FILENAME_PREFIXES {
1406 if remainder.starts_with(name) {
1407 if let Some(contents) = std::fs::read_to_string(&path).log_err() {
1408 match query(&mut result) {
1409 None => *query(&mut result) = Some(contents.into()),
1410 Some(r) => r.to_mut().push_str(contents.as_ref()),
1411 }
1412 }
1413 break;
1414 }
1415 }
1416 }
1417 }
1418 }
1419 result
1420}