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