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