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