extension_store.rs

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