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