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
349 .mtime
350 .bad_is_greater_than(extensions_metadata.mtime)
351 {
352 extension_index_needs_rebuild = false;
353 }
354 }
355 }
356 }
357
358 // Immediately load all of the extensions in the initial manifest. If the
359 // index needs to be rebuild, then enqueue
360 let load_initial_extensions = this.extensions_updated(extension_index, cx);
361 let mut reload_future = None;
362 if extension_index_needs_rebuild {
363 reload_future = Some(this.reload(None, cx));
364 }
365
366 cx.spawn(|this, mut cx| async move {
367 if let Some(future) = reload_future {
368 future.await;
369 }
370 this.update(&mut cx, |this, cx| this.auto_install_extensions(cx))
371 .ok();
372 this.update(&mut cx, |this, cx| this.check_for_updates(cx))
373 .ok();
374 })
375 .detach();
376
377 // Perform all extension loading in a single task to ensure that we
378 // never attempt to simultaneously load/unload extensions from multiple
379 // parallel tasks.
380 this.tasks.push(cx.spawn(|this, mut cx| {
381 async move {
382 load_initial_extensions.await;
383
384 let mut index_changed = false;
385 let mut debounce_timer = cx
386 .background_executor()
387 .spawn(futures::future::pending())
388 .fuse();
389 loop {
390 select_biased! {
391 _ = debounce_timer => {
392 if index_changed {
393 let index = this
394 .update(&mut cx, |this, cx| this.rebuild_extension_index(cx))?
395 .await;
396 this.update(&mut cx, |this, cx| this.extensions_updated(index, cx))?
397 .await;
398 index_changed = false;
399 }
400
401 Self::update_ssh_clients(&this, &mut cx).await?;
402 }
403 _ = connection_registered_rx.next() => {
404 debounce_timer = cx
405 .background_executor()
406 .timer(RELOAD_DEBOUNCE_DURATION)
407 .fuse();
408 }
409 extension_id = reload_rx.next() => {
410 let Some(extension_id) = extension_id else { break; };
411 this.update(&mut cx, |this, _| {
412 this.modified_extensions.extend(extension_id);
413 })?;
414 index_changed = true;
415 debounce_timer = cx
416 .background_executor()
417 .timer(RELOAD_DEBOUNCE_DURATION)
418 .fuse();
419 }
420 }
421 }
422
423 anyhow::Ok(())
424 }
425 .map(drop)
426 }));
427
428 // Watch the installed extensions directory for changes. Whenever changes are
429 // detected, rebuild the extension index, and load/unload any extensions that
430 // have been added, removed, or modified.
431 this.tasks.push(cx.background_executor().spawn({
432 let fs = this.fs.clone();
433 let reload_tx = this.reload_tx.clone();
434 let installed_dir = this.installed_dir.clone();
435 async move {
436 let (mut paths, _) = fs.watch(&installed_dir, FS_WATCH_LATENCY).await;
437 while let Some(events) = paths.next().await {
438 for event in events {
439 let Ok(event_path) = event.path.strip_prefix(&installed_dir) else {
440 continue;
441 };
442
443 if let Some(path::Component::Normal(extension_dir_name)) =
444 event_path.components().next()
445 {
446 if let Some(extension_id) = extension_dir_name.to_str() {
447 reload_tx.unbounded_send(Some(extension_id.into())).ok();
448 }
449 }
450 }
451 }
452 }
453 }));
454
455 this
456 }
457
458 pub fn reload(
459 &mut self,
460 modified_extension: Option<Arc<str>>,
461 cx: &mut ModelContext<Self>,
462 ) -> impl Future<Output = ()> {
463 let (tx, rx) = oneshot::channel();
464 self.reload_complete_senders.push(tx);
465 self.reload_tx
466 .unbounded_send(modified_extension)
467 .expect("reload task exited");
468 cx.emit(Event::StartedReloading);
469
470 async move {
471 rx.await.ok();
472 }
473 }
474
475 fn extensions_dir(&self) -> PathBuf {
476 self.installed_dir.clone()
477 }
478
479 pub fn outstanding_operations(&self) -> &BTreeMap<Arc<str>, ExtensionOperation> {
480 &self.outstanding_operations
481 }
482
483 pub fn installed_extensions(&self) -> &BTreeMap<Arc<str>, ExtensionIndexEntry> {
484 &self.extension_index.extensions
485 }
486
487 pub fn dev_extensions(&self) -> impl Iterator<Item = &Arc<ExtensionManifest>> {
488 self.extension_index
489 .extensions
490 .values()
491 .filter_map(|extension| extension.dev.then_some(&extension.manifest))
492 }
493
494 /// Returns the names of themes provided by extensions.
495 pub fn extension_themes<'a>(
496 &'a self,
497 extension_id: &'a str,
498 ) -> impl Iterator<Item = &'a Arc<str>> {
499 self.extension_index
500 .themes
501 .iter()
502 .filter_map(|(name, theme)| theme.extension.as_ref().eq(extension_id).then_some(name))
503 }
504
505 pub fn fetch_extensions(
506 &self,
507 search: Option<&str>,
508 cx: &mut ModelContext<Self>,
509 ) -> Task<Result<Vec<ExtensionMetadata>>> {
510 let version = CURRENT_SCHEMA_VERSION.to_string();
511 let mut query = vec![("max_schema_version", version.as_str())];
512 if let Some(search) = search {
513 query.push(("filter", search));
514 }
515
516 self.fetch_extensions_from_api("/extensions", &query, cx)
517 }
518
519 pub fn fetch_extensions_with_update_available(
520 &mut self,
521 cx: &mut ModelContext<Self>,
522 ) -> Task<Result<Vec<ExtensionMetadata>>> {
523 let schema_versions = schema_version_range();
524 let wasm_api_versions = wasm_api_version_range(ReleaseChannel::global(cx));
525 let extension_settings = ExtensionSettings::get_global(cx);
526 let extension_ids = self
527 .extension_index
528 .extensions
529 .iter()
530 .filter(|(id, entry)| !entry.dev && extension_settings.should_auto_update(id))
531 .map(|(id, _)| id.as_ref())
532 .collect::<Vec<_>>()
533 .join(",");
534 let task = self.fetch_extensions_from_api(
535 "/extensions/updates",
536 &[
537 ("min_schema_version", &schema_versions.start().to_string()),
538 ("max_schema_version", &schema_versions.end().to_string()),
539 (
540 "min_wasm_api_version",
541 &wasm_api_versions.start().to_string(),
542 ),
543 ("max_wasm_api_version", &wasm_api_versions.end().to_string()),
544 ("ids", &extension_ids),
545 ],
546 cx,
547 );
548 cx.spawn(move |this, mut cx| async move {
549 let extensions = task.await?;
550 this.update(&mut cx, |this, _cx| {
551 extensions
552 .into_iter()
553 .filter(|extension| {
554 this.extension_index.extensions.get(&extension.id).map_or(
555 true,
556 |installed_extension| {
557 installed_extension.manifest.version != extension.manifest.version
558 },
559 )
560 })
561 .collect()
562 })
563 })
564 }
565
566 pub fn fetch_extension_versions(
567 &self,
568 extension_id: &str,
569 cx: &mut ModelContext<Self>,
570 ) -> Task<Result<Vec<ExtensionMetadata>>> {
571 self.fetch_extensions_from_api(&format!("/extensions/{extension_id}"), &[], cx)
572 }
573
574 /// Installs any extensions that should be included with Zed by default.
575 ///
576 /// This can be used to make certain functionality provided by extensions
577 /// available out-of-the-box.
578 pub fn auto_install_extensions(&mut self, cx: &mut ModelContext<Self>) {
579 let extension_settings = ExtensionSettings::get_global(cx);
580
581 let extensions_to_install = extension_settings
582 .auto_install_extensions
583 .keys()
584 .filter(|extension_id| extension_settings.should_auto_install(extension_id))
585 .filter(|extension_id| {
586 let is_already_installed = self
587 .extension_index
588 .extensions
589 .contains_key(extension_id.as_ref());
590 !is_already_installed
591 })
592 .cloned()
593 .collect::<Vec<_>>();
594
595 cx.spawn(move |this, mut cx| async move {
596 for extension_id in extensions_to_install {
597 this.update(&mut cx, |this, cx| {
598 this.install_latest_extension(extension_id.clone(), cx);
599 })
600 .ok();
601 }
602 })
603 .detach();
604 }
605
606 pub fn check_for_updates(&mut self, cx: &mut ModelContext<Self>) {
607 let task = self.fetch_extensions_with_update_available(cx);
608 cx.spawn(move |this, mut cx| async move {
609 Self::upgrade_extensions(this, task.await?, &mut cx).await
610 })
611 .detach();
612 }
613
614 async fn upgrade_extensions(
615 this: WeakModel<Self>,
616 extensions: Vec<ExtensionMetadata>,
617 cx: &mut AsyncAppContext,
618 ) -> Result<()> {
619 for extension in extensions {
620 let task = this.update(cx, |this, cx| {
621 if let Some(installed_extension) =
622 this.extension_index.extensions.get(&extension.id)
623 {
624 let installed_version =
625 SemanticVersion::from_str(&installed_extension.manifest.version).ok()?;
626 let latest_version =
627 SemanticVersion::from_str(&extension.manifest.version).ok()?;
628
629 if installed_version >= latest_version {
630 return None;
631 }
632 }
633
634 Some(this.upgrade_extension(extension.id, extension.manifest.version, cx))
635 })?;
636
637 if let Some(task) = task {
638 task.await.log_err();
639 }
640 }
641 anyhow::Ok(())
642 }
643
644 fn fetch_extensions_from_api(
645 &self,
646 path: &str,
647 query: &[(&str, &str)],
648 cx: &mut ModelContext<'_, ExtensionStore>,
649 ) -> Task<Result<Vec<ExtensionMetadata>>> {
650 let url = self.http_client.build_zed_api_url(path, query);
651 let http_client = self.http_client.clone();
652 cx.spawn(move |_, _| async move {
653 let mut response = http_client
654 .get(url?.as_ref(), AsyncBody::empty(), true)
655 .await?;
656
657 let mut body = Vec::new();
658 response
659 .body_mut()
660 .read_to_end(&mut body)
661 .await
662 .context("error reading extensions")?;
663
664 if response.status().is_client_error() {
665 let text = String::from_utf8_lossy(body.as_slice());
666 bail!(
667 "status error {}, response: {text:?}",
668 response.status().as_u16()
669 );
670 }
671
672 let response: GetExtensionsResponse = serde_json::from_slice(&body)?;
673 Ok(response.data)
674 })
675 }
676
677 pub fn install_extension(
678 &mut self,
679 extension_id: Arc<str>,
680 version: Arc<str>,
681 cx: &mut ModelContext<Self>,
682 ) {
683 self.install_or_upgrade_extension(extension_id, version, ExtensionOperation::Install, cx)
684 .detach_and_log_err(cx);
685 }
686
687 fn install_or_upgrade_extension_at_endpoint(
688 &mut self,
689 extension_id: Arc<str>,
690 url: Url,
691 operation: ExtensionOperation,
692 cx: &mut ModelContext<Self>,
693 ) -> Task<Result<()>> {
694 let extension_dir = self.installed_dir.join(extension_id.as_ref());
695 let http_client = self.http_client.clone();
696 let fs = self.fs.clone();
697
698 match self.outstanding_operations.entry(extension_id.clone()) {
699 btree_map::Entry::Occupied(_) => return Task::ready(Ok(())),
700 btree_map::Entry::Vacant(e) => e.insert(operation),
701 };
702 cx.notify();
703
704 cx.spawn(move |this, mut cx| async move {
705 let _finish = util::defer({
706 let this = this.clone();
707 let mut cx = cx.clone();
708 let extension_id = extension_id.clone();
709 move || {
710 this.update(&mut cx, |this, cx| {
711 this.outstanding_operations.remove(extension_id.as_ref());
712 cx.notify();
713 })
714 .ok();
715 }
716 });
717
718 let mut response = http_client
719 .get(url.as_ref(), Default::default(), true)
720 .await
721 .map_err(|err| anyhow!("error downloading extension: {}", err))?;
722
723 fs.remove_dir(
724 &extension_dir,
725 RemoveOptions {
726 recursive: true,
727 ignore_if_not_exists: true,
728 },
729 )
730 .await?;
731
732 let content_length = response
733 .headers()
734 .get(http_client::http::header::CONTENT_LENGTH)
735 .and_then(|value| value.to_str().ok()?.parse::<usize>().ok());
736
737 let mut body = BufReader::new(response.body_mut());
738 let mut tar_gz_bytes = Vec::new();
739 body.read_to_end(&mut tar_gz_bytes).await?;
740
741 if let Some(content_length) = content_length {
742 let actual_len = tar_gz_bytes.len();
743 if content_length != actual_len {
744 bail!("downloaded extension size {actual_len} does not match content length {content_length}");
745 }
746 }
747 let decompressed_bytes = GzipDecoder::new(BufReader::new(tar_gz_bytes.as_slice()));
748 let archive = Archive::new(decompressed_bytes);
749 archive.unpack(extension_dir).await?;
750 this.update(&mut cx, |this, cx| {
751 this.reload(Some(extension_id.clone()), cx)
752 })?
753 .await;
754
755 if let ExtensionOperation::Install = operation {
756 this.update(&mut cx, |_, cx| {
757 cx.emit(Event::ExtensionInstalled(extension_id));
758 })
759 .ok();
760 }
761
762 anyhow::Ok(())
763 })
764 }
765
766 pub fn install_latest_extension(
767 &mut self,
768 extension_id: Arc<str>,
769 cx: &mut ModelContext<Self>,
770 ) {
771 log::info!("installing extension {extension_id} latest version");
772
773 let schema_versions = schema_version_range();
774 let wasm_api_versions = wasm_api_version_range(ReleaseChannel::global(cx));
775
776 let Some(url) = self
777 .http_client
778 .build_zed_api_url(
779 &format!("/extensions/{extension_id}/download"),
780 &[
781 ("min_schema_version", &schema_versions.start().to_string()),
782 ("max_schema_version", &schema_versions.end().to_string()),
783 (
784 "min_wasm_api_version",
785 &wasm_api_versions.start().to_string(),
786 ),
787 ("max_wasm_api_version", &wasm_api_versions.end().to_string()),
788 ],
789 )
790 .log_err()
791 else {
792 return;
793 };
794
795 self.install_or_upgrade_extension_at_endpoint(
796 extension_id,
797 url,
798 ExtensionOperation::Install,
799 cx,
800 )
801 .detach_and_log_err(cx);
802 }
803
804 pub fn upgrade_extension(
805 &mut self,
806 extension_id: Arc<str>,
807 version: Arc<str>,
808 cx: &mut ModelContext<Self>,
809 ) -> Task<Result<()>> {
810 self.install_or_upgrade_extension(extension_id, version, ExtensionOperation::Upgrade, cx)
811 }
812
813 fn install_or_upgrade_extension(
814 &mut self,
815 extension_id: Arc<str>,
816 version: Arc<str>,
817 operation: ExtensionOperation,
818 cx: &mut ModelContext<Self>,
819 ) -> Task<Result<()>> {
820 log::info!("installing extension {extension_id} {version}");
821 let Some(url) = self
822 .http_client
823 .build_zed_api_url(
824 &format!("/extensions/{extension_id}/{version}/download"),
825 &[],
826 )
827 .log_err()
828 else {
829 return Task::ready(Ok(()));
830 };
831
832 self.install_or_upgrade_extension_at_endpoint(extension_id, url, operation, cx)
833 }
834
835 pub fn uninstall_extension(&mut self, extension_id: Arc<str>, cx: &mut ModelContext<Self>) {
836 let extension_dir = self.installed_dir.join(extension_id.as_ref());
837 let work_dir = self.wasm_host.work_dir.join(extension_id.as_ref());
838 let fs = self.fs.clone();
839
840 match self.outstanding_operations.entry(extension_id.clone()) {
841 btree_map::Entry::Occupied(_) => return,
842 btree_map::Entry::Vacant(e) => e.insert(ExtensionOperation::Remove),
843 };
844
845 cx.spawn(move |this, mut cx| async move {
846 let _finish = util::defer({
847 let this = this.clone();
848 let mut cx = cx.clone();
849 let extension_id = extension_id.clone();
850 move || {
851 this.update(&mut cx, |this, cx| {
852 this.outstanding_operations.remove(extension_id.as_ref());
853 cx.notify();
854 })
855 .ok();
856 }
857 });
858
859 fs.remove_dir(
860 &work_dir,
861 RemoveOptions {
862 recursive: true,
863 ignore_if_not_exists: true,
864 },
865 )
866 .await?;
867
868 fs.remove_dir(
869 &extension_dir,
870 RemoveOptions {
871 recursive: true,
872 ignore_if_not_exists: true,
873 },
874 )
875 .await?;
876
877 this.update(&mut cx, |this, cx| this.reload(None, cx))?
878 .await;
879 anyhow::Ok(())
880 })
881 .detach_and_log_err(cx)
882 }
883
884 pub fn install_dev_extension(
885 &mut self,
886 extension_source_path: PathBuf,
887 cx: &mut ModelContext<Self>,
888 ) -> Task<Result<()>> {
889 let extensions_dir = self.extensions_dir();
890 let fs = self.fs.clone();
891 let builder = self.builder.clone();
892
893 cx.spawn(move |this, mut cx| async move {
894 let mut extension_manifest =
895 ExtensionManifest::load(fs.clone(), &extension_source_path).await?;
896 let extension_id = extension_manifest.id.clone();
897
898 if !this.update(&mut cx, |this, cx| {
899 match this.outstanding_operations.entry(extension_id.clone()) {
900 btree_map::Entry::Occupied(_) => return false,
901 btree_map::Entry::Vacant(e) => e.insert(ExtensionOperation::Remove),
902 };
903 cx.notify();
904 true
905 })? {
906 return Ok(());
907 }
908
909 let _finish = util::defer({
910 let this = this.clone();
911 let mut cx = cx.clone();
912 let extension_id = extension_id.clone();
913 move || {
914 this.update(&mut cx, |this, cx| {
915 this.outstanding_operations.remove(extension_id.as_ref());
916 cx.notify();
917 })
918 .ok();
919 }
920 });
921
922 cx.background_executor()
923 .spawn({
924 let extension_source_path = extension_source_path.clone();
925 async move {
926 builder
927 .compile_extension(
928 &extension_source_path,
929 &mut extension_manifest,
930 CompileExtensionOptions { release: false },
931 )
932 .await
933 }
934 })
935 .await?;
936
937 let output_path = &extensions_dir.join(extension_id.as_ref());
938 if let Some(metadata) = fs.metadata(output_path).await? {
939 if metadata.is_symlink {
940 fs.remove_file(
941 output_path,
942 RemoveOptions {
943 recursive: false,
944 ignore_if_not_exists: true,
945 },
946 )
947 .await?;
948 } else {
949 bail!("extension {extension_id} is already installed");
950 }
951 }
952
953 fs.create_symlink(output_path, extension_source_path)
954 .await?;
955
956 this.update(&mut cx, |this, cx| this.reload(None, cx))?
957 .await;
958 Ok(())
959 })
960 }
961
962 pub fn rebuild_dev_extension(&mut self, extension_id: Arc<str>, cx: &mut ModelContext<Self>) {
963 let path = self.installed_dir.join(extension_id.as_ref());
964 let builder = self.builder.clone();
965 let fs = self.fs.clone();
966
967 match self.outstanding_operations.entry(extension_id.clone()) {
968 btree_map::Entry::Occupied(_) => return,
969 btree_map::Entry::Vacant(e) => e.insert(ExtensionOperation::Upgrade),
970 };
971
972 cx.notify();
973 let compile = cx.background_executor().spawn(async move {
974 let mut manifest = ExtensionManifest::load(fs, &path).await?;
975 builder
976 .compile_extension(
977 &path,
978 &mut manifest,
979 CompileExtensionOptions { release: true },
980 )
981 .await
982 });
983
984 cx.spawn(|this, mut cx| async move {
985 let result = compile.await;
986
987 this.update(&mut cx, |this, cx| {
988 this.outstanding_operations.remove(&extension_id);
989 cx.notify();
990 })?;
991
992 if result.is_ok() {
993 this.update(&mut cx, |this, cx| this.reload(Some(extension_id), cx))?
994 .await;
995 }
996
997 result
998 })
999 .detach_and_log_err(cx)
1000 }
1001
1002 /// Updates the set of installed extensions.
1003 ///
1004 /// First, this unloads any themes, languages, or grammars that are
1005 /// no longer in the manifest, or whose files have changed on disk.
1006 /// Then it loads any themes, languages, or grammars that are newly
1007 /// added to the manifest, or whose files have changed on disk.
1008 fn extensions_updated(
1009 &mut self,
1010 new_index: ExtensionIndex,
1011 cx: &mut ModelContext<Self>,
1012 ) -> Task<()> {
1013 let old_index = &self.extension_index;
1014
1015 // Determine which extensions need to be loaded and unloaded, based
1016 // on the changes to the manifest and the extensions that we know have been
1017 // modified.
1018 let mut extensions_to_unload = Vec::default();
1019 let mut extensions_to_load = Vec::default();
1020 {
1021 let mut old_keys = old_index.extensions.iter().peekable();
1022 let mut new_keys = new_index.extensions.iter().peekable();
1023 loop {
1024 match (old_keys.peek(), new_keys.peek()) {
1025 (None, None) => break,
1026 (None, Some(_)) => {
1027 extensions_to_load.push(new_keys.next().unwrap().0.clone());
1028 }
1029 (Some(_), None) => {
1030 extensions_to_unload.push(old_keys.next().unwrap().0.clone());
1031 }
1032 (Some((old_key, _)), Some((new_key, _))) => match old_key.cmp(new_key) {
1033 Ordering::Equal => {
1034 let (old_key, old_value) = old_keys.next().unwrap();
1035 let (new_key, new_value) = new_keys.next().unwrap();
1036 if old_value != new_value || self.modified_extensions.contains(old_key)
1037 {
1038 extensions_to_unload.push(old_key.clone());
1039 extensions_to_load.push(new_key.clone());
1040 }
1041 }
1042 Ordering::Less => {
1043 extensions_to_unload.push(old_keys.next().unwrap().0.clone());
1044 }
1045 Ordering::Greater => {
1046 extensions_to_load.push(new_keys.next().unwrap().0.clone());
1047 }
1048 },
1049 }
1050 }
1051 self.modified_extensions.clear();
1052 }
1053
1054 if extensions_to_load.is_empty() && extensions_to_unload.is_empty() {
1055 return Task::ready(());
1056 }
1057
1058 let reload_count = extensions_to_unload
1059 .iter()
1060 .filter(|id| extensions_to_load.contains(id))
1061 .count();
1062
1063 log::info!(
1064 "extensions updated. loading {}, reloading {}, unloading {}",
1065 extensions_to_load.len() - reload_count,
1066 reload_count,
1067 extensions_to_unload.len() - reload_count
1068 );
1069
1070 if let Some(telemetry) = &self.telemetry {
1071 for extension_id in &extensions_to_load {
1072 if let Some(extension) = new_index.extensions.get(extension_id) {
1073 telemetry.report_extension_event(
1074 extension_id.clone(),
1075 extension.manifest.version.clone(),
1076 );
1077 }
1078 }
1079 }
1080
1081 let themes_to_remove = old_index
1082 .themes
1083 .iter()
1084 .filter_map(|(name, entry)| {
1085 if extensions_to_unload.contains(&entry.extension) {
1086 Some(name.clone().into())
1087 } else {
1088 None
1089 }
1090 })
1091 .collect::<Vec<_>>();
1092 let languages_to_remove = old_index
1093 .languages
1094 .iter()
1095 .filter_map(|(name, entry)| {
1096 if extensions_to_unload.contains(&entry.extension) {
1097 Some(name.clone())
1098 } else {
1099 None
1100 }
1101 })
1102 .collect::<Vec<_>>();
1103 let mut grammars_to_remove = Vec::new();
1104 for extension_id in &extensions_to_unload {
1105 let Some(extension) = old_index.extensions.get(extension_id) else {
1106 continue;
1107 };
1108 grammars_to_remove.extend(extension.manifest.grammars.keys().cloned());
1109 for (language_server_name, config) in extension.manifest.language_servers.iter() {
1110 for language in config.languages() {
1111 self.registration_hooks
1112 .remove_lsp_adapter(&language, language_server_name);
1113 }
1114 }
1115 }
1116
1117 self.wasm_extensions
1118 .retain(|(extension, _)| !extensions_to_unload.contains(&extension.id));
1119 self.registration_hooks.remove_user_themes(themes_to_remove);
1120 self.registration_hooks
1121 .remove_languages(&languages_to_remove, &grammars_to_remove);
1122
1123 let languages_to_add = new_index
1124 .languages
1125 .iter()
1126 .filter(|(_, entry)| extensions_to_load.contains(&entry.extension))
1127 .collect::<Vec<_>>();
1128 let mut grammars_to_add = Vec::new();
1129 let mut themes_to_add = Vec::new();
1130 let mut snippets_to_add = Vec::new();
1131 for extension_id in &extensions_to_load {
1132 let Some(extension) = new_index.extensions.get(extension_id) else {
1133 continue;
1134 };
1135
1136 grammars_to_add.extend(extension.manifest.grammars.keys().map(|grammar_name| {
1137 let mut grammar_path = self.installed_dir.clone();
1138 grammar_path.extend([extension_id.as_ref(), "grammars"]);
1139 grammar_path.push(grammar_name.as_ref());
1140 grammar_path.set_extension("wasm");
1141 (grammar_name.clone(), grammar_path)
1142 }));
1143 themes_to_add.extend(extension.manifest.themes.iter().map(|theme_path| {
1144 let mut path = self.installed_dir.clone();
1145 path.extend([Path::new(extension_id.as_ref()), theme_path.as_path()]);
1146 path
1147 }));
1148 snippets_to_add.extend(extension.manifest.snippets.iter().map(|snippets_path| {
1149 let mut path = self.installed_dir.clone();
1150 path.extend([Path::new(extension_id.as_ref()), snippets_path.as_path()]);
1151 path
1152 }));
1153 }
1154
1155 self.registration_hooks
1156 .register_wasm_grammars(grammars_to_add);
1157
1158 for (language_name, language) in languages_to_add {
1159 let mut language_path = self.installed_dir.clone();
1160 language_path.extend([
1161 Path::new(language.extension.as_ref()),
1162 language.path.as_path(),
1163 ]);
1164 self.registration_hooks.register_language(
1165 language_name.clone(),
1166 language.grammar.clone(),
1167 language.matcher.clone(),
1168 Arc::new(move || {
1169 let config = std::fs::read_to_string(language_path.join("config.toml"))?;
1170 let config: LanguageConfig = ::toml::from_str(&config)?;
1171 let queries = load_plugin_queries(&language_path);
1172 let context_provider =
1173 std::fs::read_to_string(language_path.join("tasks.json"))
1174 .ok()
1175 .and_then(|contents| {
1176 let definitions =
1177 serde_json_lenient::from_str(&contents).log_err()?;
1178 Some(Arc::new(ContextProviderWithTasks::new(definitions)) as Arc<_>)
1179 });
1180
1181 Ok(LoadedLanguage {
1182 config,
1183 queries,
1184 context_provider,
1185 toolchain_provider: None,
1186 })
1187 }),
1188 );
1189 }
1190
1191 let fs = self.fs.clone();
1192 let wasm_host = self.wasm_host.clone();
1193 let root_dir = self.installed_dir.clone();
1194 let api = self.registration_hooks.clone();
1195 let extension_entries = extensions_to_load
1196 .iter()
1197 .filter_map(|name| new_index.extensions.get(name).cloned())
1198 .collect::<Vec<_>>();
1199
1200 self.extension_index = new_index;
1201 cx.notify();
1202 cx.emit(Event::ExtensionsUpdated);
1203
1204 cx.spawn(|this, mut cx| async move {
1205 cx.background_executor()
1206 .spawn({
1207 let fs = fs.clone();
1208 async move {
1209 for theme_path in themes_to_add.into_iter() {
1210 api.load_user_theme(theme_path, fs.clone()).await.log_err();
1211 }
1212
1213 for snippets_path in &snippets_to_add {
1214 if let Some(snippets_contents) = fs.load(snippets_path).await.log_err()
1215 {
1216 api.register_snippets(snippets_path, &snippets_contents)
1217 .log_err();
1218 }
1219 }
1220 }
1221 })
1222 .await;
1223
1224 let mut wasm_extensions = Vec::new();
1225 for extension in extension_entries {
1226 if extension.manifest.lib.kind.is_none() {
1227 continue;
1228 };
1229
1230 let extension_path = root_dir.join(extension.manifest.id.as_ref());
1231 let wasm_extension = WasmExtension::load(
1232 extension_path,
1233 &extension.manifest,
1234 wasm_host.clone(),
1235 &cx,
1236 )
1237 .await;
1238
1239 if let Some(wasm_extension) = wasm_extension.log_err() {
1240 wasm_extensions.push((extension.manifest.clone(), wasm_extension));
1241 } else {
1242 this.update(&mut cx, |_, cx| {
1243 cx.emit(Event::ExtensionFailedToLoad(extension.manifest.id.clone()))
1244 })
1245 .ok();
1246 }
1247 }
1248
1249 this.update(&mut cx, |this, cx| {
1250 this.reload_complete_senders.clear();
1251
1252 for (manifest, wasm_extension) in &wasm_extensions {
1253 let extension = Arc::new(wasm_extension.clone());
1254
1255 for (language_server_id, language_server_config) in &manifest.language_servers {
1256 for language in language_server_config.languages() {
1257 this.registration_hooks.register_lsp_adapter(
1258 language.clone(),
1259 ExtensionLspAdapter {
1260 extension: extension.clone(),
1261 language_server_id: language_server_id.clone(),
1262 language_name: language.clone(),
1263 },
1264 );
1265 }
1266 }
1267
1268 for (slash_command_name, slash_command) in &manifest.slash_commands {
1269 this.registration_hooks.register_slash_command(
1270 extension.clone(),
1271 extension::SlashCommand {
1272 name: slash_command_name.to_string(),
1273 description: slash_command.description.to_string(),
1274 // We don't currently expose this as a configurable option, as it currently drives
1275 // the `menu_text` on the `SlashCommand` trait, which is not used for slash commands
1276 // defined in extensions, as they are not able to be added to the menu.
1277 tooltip_text: String::new(),
1278 requires_argument: slash_command.requires_argument,
1279 },
1280 );
1281 }
1282
1283 for (id, _context_server_entry) in &manifest.context_servers {
1284 this.registration_hooks.register_context_server(
1285 id.clone(),
1286 wasm_extension.clone(),
1287 cx,
1288 );
1289 }
1290
1291 for (provider_id, _provider) in &manifest.indexed_docs_providers {
1292 this.registration_hooks
1293 .register_docs_provider(extension.clone(), provider_id.clone());
1294 }
1295 }
1296
1297 this.wasm_extensions.extend(wasm_extensions);
1298 this.registration_hooks.reload_current_theme(cx);
1299 })
1300 .ok();
1301 })
1302 }
1303
1304 fn rebuild_extension_index(&self, cx: &mut ModelContext<Self>) -> Task<ExtensionIndex> {
1305 let fs = self.fs.clone();
1306 let work_dir = self.wasm_host.work_dir.clone();
1307 let extensions_dir = self.installed_dir.clone();
1308 let index_path = self.index_path.clone();
1309 let extension_api = self.registration_hooks.clone();
1310 cx.background_executor().spawn(async move {
1311 let start_time = Instant::now();
1312 let mut index = ExtensionIndex::default();
1313
1314 fs.create_dir(&work_dir).await.log_err();
1315 fs.create_dir(&extensions_dir).await.log_err();
1316
1317 let extension_paths = fs.read_dir(&extensions_dir).await;
1318 if let Ok(mut extension_paths) = extension_paths {
1319 while let Some(extension_dir) = extension_paths.next().await {
1320 let Ok(extension_dir) = extension_dir else {
1321 continue;
1322 };
1323
1324 if extension_dir
1325 .file_name()
1326 .map_or(false, |file_name| file_name == ".DS_Store")
1327 {
1328 continue;
1329 }
1330
1331 Self::add_extension_to_index(
1332 fs.clone(),
1333 extension_dir,
1334 &mut index,
1335 extension_api.clone(),
1336 )
1337 .await
1338 .log_err();
1339 }
1340 }
1341
1342 if let Ok(index_json) = serde_json::to_string_pretty(&index) {
1343 fs.save(&index_path, &index_json.as_str().into(), Default::default())
1344 .await
1345 .context("failed to save extension index")
1346 .log_err();
1347 }
1348
1349 log::info!("rebuilt extension index in {:?}", start_time.elapsed());
1350 index
1351 })
1352 }
1353
1354 async fn add_extension_to_index(
1355 fs: Arc<dyn Fs>,
1356 extension_dir: PathBuf,
1357 index: &mut ExtensionIndex,
1358 extension_api: Arc<dyn ExtensionRegistrationHooks>,
1359 ) -> Result<()> {
1360 let mut extension_manifest = ExtensionManifest::load(fs.clone(), &extension_dir).await?;
1361 let extension_id = extension_manifest.id.clone();
1362
1363 // TODO: distinguish dev extensions more explicitly, by the absence
1364 // of a checksum file that we'll create when downloading normal extensions.
1365 let is_dev = fs
1366 .metadata(&extension_dir)
1367 .await?
1368 .ok_or_else(|| anyhow!("directory does not exist"))?
1369 .is_symlink;
1370
1371 if let Ok(mut language_paths) = fs.read_dir(&extension_dir.join("languages")).await {
1372 while let Some(language_path) = language_paths.next().await {
1373 let language_path = language_path?;
1374 let Ok(relative_path) = language_path.strip_prefix(&extension_dir) else {
1375 continue;
1376 };
1377 let Ok(Some(fs_metadata)) = fs.metadata(&language_path).await else {
1378 continue;
1379 };
1380 if !fs_metadata.is_dir {
1381 continue;
1382 }
1383 let config = fs.load(&language_path.join("config.toml")).await?;
1384 let config = ::toml::from_str::<LanguageConfig>(&config)?;
1385
1386 let relative_path = relative_path.to_path_buf();
1387 if !extension_manifest.languages.contains(&relative_path) {
1388 extension_manifest.languages.push(relative_path.clone());
1389 }
1390
1391 index.languages.insert(
1392 config.name.clone(),
1393 ExtensionIndexLanguageEntry {
1394 extension: extension_id.clone(),
1395 path: relative_path,
1396 matcher: config.matcher,
1397 grammar: config.grammar,
1398 },
1399 );
1400 }
1401 }
1402
1403 if let Ok(mut theme_paths) = fs.read_dir(&extension_dir.join("themes")).await {
1404 while let Some(theme_path) = theme_paths.next().await {
1405 let theme_path = theme_path?;
1406 let Ok(relative_path) = theme_path.strip_prefix(&extension_dir) else {
1407 continue;
1408 };
1409
1410 let Some(theme_families) = extension_api
1411 .list_theme_names(theme_path.clone(), fs.clone())
1412 .await
1413 .log_err()
1414 else {
1415 continue;
1416 };
1417
1418 let relative_path = relative_path.to_path_buf();
1419 if !extension_manifest.themes.contains(&relative_path) {
1420 extension_manifest.themes.push(relative_path.clone());
1421 }
1422
1423 for theme_name in theme_families {
1424 index.themes.insert(
1425 theme_name.into(),
1426 ExtensionIndexThemeEntry {
1427 extension: extension_id.clone(),
1428 path: relative_path.clone(),
1429 },
1430 );
1431 }
1432 }
1433 }
1434
1435 let extension_wasm_path = extension_dir.join("extension.wasm");
1436 if fs.is_file(&extension_wasm_path).await {
1437 extension_manifest
1438 .lib
1439 .kind
1440 .get_or_insert(ExtensionLibraryKind::Rust);
1441 }
1442
1443 index.extensions.insert(
1444 extension_id.clone(),
1445 ExtensionIndexEntry {
1446 dev: is_dev,
1447 manifest: Arc::new(extension_manifest),
1448 },
1449 );
1450
1451 Ok(())
1452 }
1453
1454 fn prepare_remote_extension(
1455 &mut self,
1456 extension_id: Arc<str>,
1457 tmp_dir: PathBuf,
1458 cx: &mut ModelContext<Self>,
1459 ) -> Task<Result<()>> {
1460 let src_dir = self.extensions_dir().join(extension_id.as_ref());
1461 let Some(loaded_extension) = self.extension_index.extensions.get(&extension_id).cloned()
1462 else {
1463 return Task::ready(Err(anyhow!("extension no longer installed")));
1464 };
1465 let fs = self.fs.clone();
1466 cx.background_executor().spawn(async move {
1467 for well_known_path in ["extension.toml", "extension.json", "extension.wasm"] {
1468 if fs.is_file(&src_dir.join(well_known_path)).await {
1469 fs.copy_file(
1470 &src_dir.join(well_known_path),
1471 &tmp_dir.join(well_known_path),
1472 fs::CopyOptions::default(),
1473 )
1474 .await?
1475 }
1476 }
1477
1478 for language_path in loaded_extension.manifest.languages.iter() {
1479 if fs
1480 .is_file(&src_dir.join(language_path).join("config.toml"))
1481 .await
1482 {
1483 fs.create_dir(&tmp_dir.join(language_path)).await?;
1484 fs.copy_file(
1485 &src_dir.join(language_path).join("config.toml"),
1486 &tmp_dir.join(language_path).join("config.toml"),
1487 fs::CopyOptions::default(),
1488 )
1489 .await?
1490 }
1491 }
1492
1493 Ok(())
1494 })
1495 }
1496
1497 async fn sync_extensions_over_ssh(
1498 this: &WeakModel<Self>,
1499 client: WeakModel<SshRemoteClient>,
1500 cx: &mut AsyncAppContext,
1501 ) -> Result<()> {
1502 let extensions = this.update(cx, |this, _cx| {
1503 this.extension_index
1504 .extensions
1505 .iter()
1506 .filter_map(|(id, entry)| {
1507 if entry.manifest.language_servers.is_empty() {
1508 return None;
1509 }
1510 Some(proto::Extension {
1511 id: id.to_string(),
1512 version: entry.manifest.version.to_string(),
1513 dev: entry.dev,
1514 })
1515 })
1516 .collect()
1517 })?;
1518
1519 let response = client
1520 .update(cx, |client, _cx| {
1521 client
1522 .proto_client()
1523 .request(proto::SyncExtensions { extensions })
1524 })?
1525 .await?;
1526
1527 for missing_extension in response.missing_extensions.into_iter() {
1528 let tmp_dir = tempfile::tempdir()?;
1529 this.update(cx, |this, cx| {
1530 this.prepare_remote_extension(
1531 missing_extension.id.clone().into(),
1532 tmp_dir.path().to_owned(),
1533 cx,
1534 )
1535 })?
1536 .await?;
1537 let dest_dir = PathBuf::from(&response.tmp_dir).join(missing_extension.clone().id);
1538 log::info!("Uploading extension {}", missing_extension.clone().id);
1539
1540 client
1541 .update(cx, |client, cx| {
1542 client.upload_directory(tmp_dir.path().to_owned(), dest_dir.clone(), cx)
1543 })?
1544 .await?;
1545
1546 client
1547 .update(cx, |client, _cx| {
1548 client.proto_client().request(proto::InstallExtension {
1549 tmp_dir: dest_dir.to_string_lossy().to_string(),
1550 extension: Some(missing_extension),
1551 })
1552 })?
1553 .await?;
1554 }
1555
1556 anyhow::Ok(())
1557 }
1558
1559 pub async fn update_ssh_clients(
1560 this: &WeakModel<Self>,
1561 cx: &mut AsyncAppContext,
1562 ) -> Result<()> {
1563 let clients = this.update(cx, |this, _cx| {
1564 this.ssh_clients.retain(|_k, v| v.upgrade().is_some());
1565 this.ssh_clients.values().cloned().collect::<Vec<_>>()
1566 })?;
1567
1568 for client in clients {
1569 Self::sync_extensions_over_ssh(&this, client, cx)
1570 .await
1571 .log_err();
1572 }
1573
1574 anyhow::Ok(())
1575 }
1576
1577 pub fn register_ssh_client(
1578 &mut self,
1579 client: Model<SshRemoteClient>,
1580 cx: &mut ModelContext<Self>,
1581 ) {
1582 let connection_options = client.read(cx).connection_options();
1583 if self.ssh_clients.contains_key(&connection_options.ssh_url()) {
1584 return;
1585 }
1586
1587 self.ssh_clients
1588 .insert(connection_options.ssh_url(), client.downgrade());
1589 self.ssh_registered_tx.unbounded_send(()).ok();
1590 }
1591}
1592
1593fn load_plugin_queries(root_path: &Path) -> LanguageQueries {
1594 let mut result = LanguageQueries::default();
1595 if let Some(entries) = std::fs::read_dir(root_path).log_err() {
1596 for entry in entries {
1597 let Some(entry) = entry.log_err() else {
1598 continue;
1599 };
1600 let path = entry.path();
1601 if let Some(remainder) = path.strip_prefix(root_path).ok().and_then(|p| p.to_str()) {
1602 if !remainder.ends_with(".scm") {
1603 continue;
1604 }
1605 for (name, query) in QUERY_FILENAME_PREFIXES {
1606 if remainder.starts_with(name) {
1607 if let Some(contents) = std::fs::read_to_string(&path).log_err() {
1608 match query(&mut result) {
1609 None => *query(&mut result) = Some(contents.into()),
1610 Some(r) => r.to_mut().push_str(contents.as_ref()),
1611 }
1612 }
1613 break;
1614 }
1615 }
1616 }
1617 }
1618 }
1619 result
1620}