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