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
 831            if !this.update(&mut cx, |this, cx| {
 832                match this.outstanding_operations.entry(extension_id.clone()) {
 833                    btree_map::Entry::Occupied(_) => return false,
 834                    btree_map::Entry::Vacant(e) => e.insert(ExtensionOperation::Remove),
 835                };
 836                cx.notify();
 837                true
 838            })? {
 839                return Ok(());
 840            }
 841
 842            let _finish = util::defer({
 843                let this = this.clone();
 844                let mut cx = cx.clone();
 845                let extension_id = extension_id.clone();
 846                move || {
 847                    this.update(&mut cx, |this, cx| {
 848                        this.outstanding_operations.remove(extension_id.as_ref());
 849                        cx.notify();
 850                    })
 851                    .ok();
 852                }
 853            });
 854
 855            cx.background_executor()
 856                .spawn({
 857                    let extension_source_path = extension_source_path.clone();
 858                    async move {
 859                        builder
 860                            .compile_extension(
 861                                &extension_source_path,
 862                                &mut extension_manifest,
 863                                CompileExtensionOptions { release: false },
 864                            )
 865                            .await
 866                    }
 867                })
 868                .await?;
 869
 870            let output_path = &extensions_dir.join(extension_id.as_ref());
 871            if let Some(metadata) = fs.metadata(output_path).await? {
 872                if metadata.is_symlink {
 873                    fs.remove_file(
 874                        output_path,
 875                        RemoveOptions {
 876                            recursive: false,
 877                            ignore_if_not_exists: true,
 878                        },
 879                    )
 880                    .await?;
 881                } else {
 882                    bail!("extension {extension_id} is already installed");
 883                }
 884            }
 885
 886            fs.create_symlink(output_path, extension_source_path)
 887                .await?;
 888
 889            this.update(&mut cx, |this, cx| this.reload(None, cx))?
 890                .await;
 891            Ok(())
 892        })
 893    }
 894
 895    pub fn rebuild_dev_extension(&mut self, extension_id: Arc<str>, cx: &mut ModelContext<Self>) {
 896        let path = self.installed_dir.join(extension_id.as_ref());
 897        let builder = self.builder.clone();
 898        let fs = self.fs.clone();
 899
 900        match self.outstanding_operations.entry(extension_id.clone()) {
 901            btree_map::Entry::Occupied(_) => return,
 902            btree_map::Entry::Vacant(e) => e.insert(ExtensionOperation::Upgrade),
 903        };
 904
 905        cx.notify();
 906        let compile = cx.background_executor().spawn(async move {
 907            let mut manifest = ExtensionManifest::load(fs, &path).await?;
 908            builder
 909                .compile_extension(
 910                    &path,
 911                    &mut manifest,
 912                    CompileExtensionOptions { release: true },
 913                )
 914                .await
 915        });
 916
 917        cx.spawn(|this, mut cx| async move {
 918            let result = compile.await;
 919
 920            this.update(&mut cx, |this, cx| {
 921                this.outstanding_operations.remove(&extension_id);
 922                cx.notify();
 923            })?;
 924
 925            if result.is_ok() {
 926                this.update(&mut cx, |this, cx| this.reload(Some(extension_id), cx))?
 927                    .await;
 928            }
 929
 930            result
 931        })
 932        .detach_and_log_err(cx)
 933    }
 934
 935    /// Updates the set of installed extensions.
 936    ///
 937    /// First, this unloads any themes, languages, or grammars that are
 938    /// no longer in the manifest, or whose files have changed on disk.
 939    /// Then it loads any themes, languages, or grammars that are newly
 940    /// added to the manifest, or whose files have changed on disk.
 941    fn extensions_updated(
 942        &mut self,
 943        new_index: ExtensionIndex,
 944        cx: &mut ModelContext<Self>,
 945    ) -> Task<()> {
 946        let old_index = &self.extension_index;
 947
 948        // Determine which extensions need to be loaded and unloaded, based
 949        // on the changes to the manifest and the extensions that we know have been
 950        // modified.
 951        let mut extensions_to_unload = Vec::default();
 952        let mut extensions_to_load = Vec::default();
 953        {
 954            let mut old_keys = old_index.extensions.iter().peekable();
 955            let mut new_keys = new_index.extensions.iter().peekable();
 956            loop {
 957                match (old_keys.peek(), new_keys.peek()) {
 958                    (None, None) => break,
 959                    (None, Some(_)) => {
 960                        extensions_to_load.push(new_keys.next().unwrap().0.clone());
 961                    }
 962                    (Some(_), None) => {
 963                        extensions_to_unload.push(old_keys.next().unwrap().0.clone());
 964                    }
 965                    (Some((old_key, _)), Some((new_key, _))) => match old_key.cmp(new_key) {
 966                        Ordering::Equal => {
 967                            let (old_key, old_value) = old_keys.next().unwrap();
 968                            let (new_key, new_value) = new_keys.next().unwrap();
 969                            if old_value != new_value || self.modified_extensions.contains(old_key)
 970                            {
 971                                extensions_to_unload.push(old_key.clone());
 972                                extensions_to_load.push(new_key.clone());
 973                            }
 974                        }
 975                        Ordering::Less => {
 976                            extensions_to_unload.push(old_keys.next().unwrap().0.clone());
 977                        }
 978                        Ordering::Greater => {
 979                            extensions_to_load.push(new_keys.next().unwrap().0.clone());
 980                        }
 981                    },
 982                }
 983            }
 984            self.modified_extensions.clear();
 985        }
 986
 987        if extensions_to_load.is_empty() && extensions_to_unload.is_empty() {
 988            return Task::ready(());
 989        }
 990
 991        let reload_count = extensions_to_unload
 992            .iter()
 993            .filter(|id| extensions_to_load.contains(id))
 994            .count();
 995
 996        log::info!(
 997            "extensions updated. loading {}, reloading {}, unloading {}",
 998            extensions_to_load.len() - reload_count,
 999            reload_count,
1000            extensions_to_unload.len() - reload_count
1001        );
1002
1003        if let Some(telemetry) = &self.telemetry {
1004            for extension_id in &extensions_to_load {
1005                if let Some(extension) = new_index.extensions.get(extension_id) {
1006                    telemetry.report_extension_event(
1007                        extension_id.clone(),
1008                        extension.manifest.version.clone(),
1009                    );
1010                }
1011            }
1012        }
1013
1014        let themes_to_remove = old_index
1015            .themes
1016            .iter()
1017            .filter_map(|(name, entry)| {
1018                if extensions_to_unload.contains(&entry.extension) {
1019                    Some(name.clone().into())
1020                } else {
1021                    None
1022                }
1023            })
1024            .collect::<Vec<_>>();
1025        let languages_to_remove = old_index
1026            .languages
1027            .iter()
1028            .filter_map(|(name, entry)| {
1029                if extensions_to_unload.contains(&entry.extension) {
1030                    Some(name.clone())
1031                } else {
1032                    None
1033                }
1034            })
1035            .collect::<Vec<_>>();
1036        let mut grammars_to_remove = Vec::new();
1037        for extension_id in &extensions_to_unload {
1038            let Some(extension) = old_index.extensions.get(extension_id) else {
1039                continue;
1040            };
1041            grammars_to_remove.extend(extension.manifest.grammars.keys().cloned());
1042            for (language_server_name, config) in extension.manifest.language_servers.iter() {
1043                for language in config.languages() {
1044                    self.language_registry
1045                        .remove_lsp_adapter(&language, language_server_name);
1046                }
1047            }
1048        }
1049
1050        self.wasm_extensions
1051            .retain(|(extension, _)| !extensions_to_unload.contains(&extension.id));
1052        self.theme_registry.remove_user_themes(&themes_to_remove);
1053        self.language_registry
1054            .remove_languages(&languages_to_remove, &grammars_to_remove);
1055
1056        let languages_to_add = new_index
1057            .languages
1058            .iter()
1059            .filter(|(_, entry)| extensions_to_load.contains(&entry.extension))
1060            .collect::<Vec<_>>();
1061        let mut grammars_to_add = Vec::new();
1062        let mut themes_to_add = Vec::new();
1063        let mut snippets_to_add = Vec::new();
1064        for extension_id in &extensions_to_load {
1065            let Some(extension) = new_index.extensions.get(extension_id) else {
1066                continue;
1067            };
1068
1069            grammars_to_add.extend(extension.manifest.grammars.keys().map(|grammar_name| {
1070                let mut grammar_path = self.installed_dir.clone();
1071                grammar_path.extend([extension_id.as_ref(), "grammars"]);
1072                grammar_path.push(grammar_name.as_ref());
1073                grammar_path.set_extension("wasm");
1074                (grammar_name.clone(), grammar_path)
1075            }));
1076            themes_to_add.extend(extension.manifest.themes.iter().map(|theme_path| {
1077                let mut path = self.installed_dir.clone();
1078                path.extend([Path::new(extension_id.as_ref()), theme_path.as_path()]);
1079                path
1080            }));
1081            snippets_to_add.extend(extension.manifest.snippets.iter().map(|snippets_path| {
1082                let mut path = self.installed_dir.clone();
1083                path.extend([Path::new(extension_id.as_ref()), snippets_path.as_path()]);
1084                path
1085            }));
1086        }
1087
1088        self.language_registry
1089            .register_wasm_grammars(grammars_to_add);
1090
1091        for (language_name, language) in languages_to_add {
1092            let mut language_path = self.installed_dir.clone();
1093            language_path.extend([
1094                Path::new(language.extension.as_ref()),
1095                language.path.as_path(),
1096            ]);
1097            self.language_registry.register_language(
1098                language_name.clone(),
1099                language.grammar.clone(),
1100                language.matcher.clone(),
1101                move || {
1102                    let config = std::fs::read_to_string(language_path.join("config.toml"))?;
1103                    let config: LanguageConfig = ::toml::from_str(&config)?;
1104                    let queries = load_plugin_queries(&language_path);
1105                    let tasks = std::fs::read_to_string(language_path.join("tasks.json"))
1106                        .ok()
1107                        .and_then(|contents| {
1108                            let definitions = serde_json_lenient::from_str(&contents).log_err()?;
1109                            Some(Arc::new(ContextProviderWithTasks::new(definitions)) as Arc<_>)
1110                        });
1111
1112                    Ok((config, queries, tasks))
1113                },
1114            );
1115        }
1116
1117        let fs = self.fs.clone();
1118        let wasm_host = self.wasm_host.clone();
1119        let root_dir = self.installed_dir.clone();
1120        let theme_registry = self.theme_registry.clone();
1121        let snippet_registry = self.snippet_registry.clone();
1122        let extension_entries = extensions_to_load
1123            .iter()
1124            .filter_map(|name| new_index.extensions.get(name).cloned())
1125            .collect::<Vec<_>>();
1126
1127        self.extension_index = new_index;
1128        cx.notify();
1129        cx.emit(Event::ExtensionsUpdated);
1130
1131        cx.spawn(|this, mut cx| async move {
1132            cx.background_executor()
1133                .spawn({
1134                    let fs = fs.clone();
1135                    async move {
1136                        for theme_path in &themes_to_add {
1137                            theme_registry
1138                                .load_user_theme(theme_path, fs.clone())
1139                                .await
1140                                .log_err();
1141                        }
1142
1143                        for snippets_path in &snippets_to_add {
1144                            if let Some(snippets_contents) = fs.load(snippets_path).await.log_err()
1145                            {
1146                                snippet_registry
1147                                    .register_snippets(snippets_path, &snippets_contents)
1148                                    .log_err();
1149                            }
1150                        }
1151                    }
1152                })
1153                .await;
1154
1155            let mut wasm_extensions = Vec::new();
1156            for extension in extension_entries {
1157                if extension.manifest.lib.kind.is_none() {
1158                    continue;
1159                };
1160
1161                let wasm_extension = maybe!(async {
1162                    let mut path = root_dir.clone();
1163                    path.extend([extension.manifest.clone().id.as_ref(), "extension.wasm"]);
1164                    let mut wasm_file = fs
1165                        .open_sync(&path)
1166                        .await
1167                        .context("failed to open wasm file")?;
1168
1169                    let mut wasm_bytes = Vec::new();
1170                    wasm_file
1171                        .read_to_end(&mut wasm_bytes)
1172                        .context("failed to read wasm")?;
1173
1174                    wasm_host
1175                        .load_extension(
1176                            wasm_bytes,
1177                            extension.manifest.clone().clone(),
1178                            cx.background_executor().clone(),
1179                        )
1180                        .await
1181                        .with_context(|| {
1182                            format!("failed to load wasm extension {}", extension.manifest.id)
1183                        })
1184                })
1185                .await;
1186
1187                if let Some(wasm_extension) = wasm_extension.log_err() {
1188                    wasm_extensions.push((extension.manifest.clone(), wasm_extension));
1189                } else {
1190                    this.update(&mut cx, |_, cx| {
1191                        cx.emit(Event::ExtensionFailedToLoad(extension.manifest.id.clone()))
1192                    })
1193                    .ok();
1194                }
1195            }
1196
1197            this.update(&mut cx, |this, cx| {
1198                this.reload_complete_senders.clear();
1199
1200                for (manifest, wasm_extension) in &wasm_extensions {
1201                    for (language_server_id, language_server_config) in &manifest.language_servers {
1202                        for language in language_server_config.languages() {
1203                            this.language_registry.register_lsp_adapter(
1204                                language.clone(),
1205                                Arc::new(ExtensionLspAdapter {
1206                                    extension: wasm_extension.clone(),
1207                                    host: this.wasm_host.clone(),
1208                                    language_server_id: language_server_id.clone(),
1209                                    config: wit::LanguageServerConfig {
1210                                        name: language_server_id.0.to_string(),
1211                                        language_name: language.to_string(),
1212                                    },
1213                                }),
1214                            );
1215                        }
1216                    }
1217
1218                    for (slash_command_name, slash_command) in &manifest.slash_commands {
1219                        this.slash_command_registry.register_command(
1220                            ExtensionSlashCommand {
1221                                command: crate::wit::SlashCommand {
1222                                    name: slash_command_name.to_string(),
1223                                    description: slash_command.description.to_string(),
1224                                    // We don't currently expose this as a configurable option, as it currently drives
1225                                    // the `menu_text` on the `SlashCommand` trait, which is not used for slash commands
1226                                    // defined in extensions, as they are not able to be added to the menu.
1227                                    tooltip_text: String::new(),
1228                                    requires_argument: slash_command.requires_argument,
1229                                },
1230                                extension: wasm_extension.clone(),
1231                                host: this.wasm_host.clone(),
1232                            },
1233                            false,
1234                        );
1235                    }
1236
1237                    for (provider_id, _provider) in &manifest.indexed_docs_providers {
1238                        this.indexed_docs_registry.register_provider(Box::new(
1239                            ExtensionIndexedDocsProvider {
1240                                extension: wasm_extension.clone(),
1241                                host: this.wasm_host.clone(),
1242                                id: ProviderId(provider_id.clone()),
1243                            },
1244                        ));
1245                    }
1246                }
1247
1248                this.wasm_extensions.extend(wasm_extensions);
1249                ThemeSettings::reload_current_theme(cx)
1250            })
1251            .ok();
1252        })
1253    }
1254
1255    fn rebuild_extension_index(&self, cx: &mut ModelContext<Self>) -> Task<ExtensionIndex> {
1256        let fs = self.fs.clone();
1257        let work_dir = self.wasm_host.work_dir.clone();
1258        let extensions_dir = self.installed_dir.clone();
1259        let index_path = self.index_path.clone();
1260        cx.background_executor().spawn(async move {
1261            let start_time = Instant::now();
1262            let mut index = ExtensionIndex::default();
1263
1264            fs.create_dir(&work_dir).await.log_err();
1265            fs.create_dir(&extensions_dir).await.log_err();
1266
1267            let extension_paths = fs.read_dir(&extensions_dir).await;
1268            if let Ok(mut extension_paths) = extension_paths {
1269                while let Some(extension_dir) = extension_paths.next().await {
1270                    let Ok(extension_dir) = extension_dir else {
1271                        continue;
1272                    };
1273
1274                    if extension_dir
1275                        .file_name()
1276                        .map_or(false, |file_name| file_name == ".DS_Store")
1277                    {
1278                        continue;
1279                    }
1280
1281                    Self::add_extension_to_index(fs.clone(), extension_dir, &mut index)
1282                        .await
1283                        .log_err();
1284                }
1285            }
1286
1287            if let Ok(index_json) = serde_json::to_string_pretty(&index) {
1288                fs.save(&index_path, &index_json.as_str().into(), Default::default())
1289                    .await
1290                    .context("failed to save extension index")
1291                    .log_err();
1292            }
1293
1294            log::info!("rebuilt extension index in {:?}", start_time.elapsed());
1295            index
1296        })
1297    }
1298
1299    async fn add_extension_to_index(
1300        fs: Arc<dyn Fs>,
1301        extension_dir: PathBuf,
1302        index: &mut ExtensionIndex,
1303    ) -> Result<()> {
1304        let mut extension_manifest = ExtensionManifest::load(fs.clone(), &extension_dir).await?;
1305        let extension_id = extension_manifest.id.clone();
1306
1307        // TODO: distinguish dev extensions more explicitly, by the absence
1308        // of a checksum file that we'll create when downloading normal extensions.
1309        let is_dev = fs
1310            .metadata(&extension_dir)
1311            .await?
1312            .ok_or_else(|| anyhow!("directory does not exist"))?
1313            .is_symlink;
1314
1315        if let Ok(mut language_paths) = fs.read_dir(&extension_dir.join("languages")).await {
1316            while let Some(language_path) = language_paths.next().await {
1317                let language_path = language_path?;
1318                let Ok(relative_path) = language_path.strip_prefix(&extension_dir) else {
1319                    continue;
1320                };
1321                let Ok(Some(fs_metadata)) = fs.metadata(&language_path).await else {
1322                    continue;
1323                };
1324                if !fs_metadata.is_dir {
1325                    continue;
1326                }
1327                let config = fs.load(&language_path.join("config.toml")).await?;
1328                let config = ::toml::from_str::<LanguageConfig>(&config)?;
1329
1330                let relative_path = relative_path.to_path_buf();
1331                if !extension_manifest.languages.contains(&relative_path) {
1332                    extension_manifest.languages.push(relative_path.clone());
1333                }
1334
1335                index.languages.insert(
1336                    config.name.clone(),
1337                    ExtensionIndexLanguageEntry {
1338                        extension: extension_id.clone(),
1339                        path: relative_path,
1340                        matcher: config.matcher,
1341                        grammar: config.grammar,
1342                    },
1343                );
1344            }
1345        }
1346
1347        if let Ok(mut theme_paths) = fs.read_dir(&extension_dir.join("themes")).await {
1348            while let Some(theme_path) = theme_paths.next().await {
1349                let theme_path = theme_path?;
1350                let Ok(relative_path) = theme_path.strip_prefix(&extension_dir) else {
1351                    continue;
1352                };
1353
1354                let Some(theme_family) = ThemeRegistry::read_user_theme(&theme_path, fs.clone())
1355                    .await
1356                    .log_err()
1357                else {
1358                    continue;
1359                };
1360
1361                let relative_path = relative_path.to_path_buf();
1362                if !extension_manifest.themes.contains(&relative_path) {
1363                    extension_manifest.themes.push(relative_path.clone());
1364                }
1365
1366                for theme in theme_family.themes {
1367                    index.themes.insert(
1368                        theme.name.into(),
1369                        ExtensionIndexThemeEntry {
1370                            extension: extension_id.clone(),
1371                            path: relative_path.clone(),
1372                        },
1373                    );
1374                }
1375            }
1376        }
1377
1378        let extension_wasm_path = extension_dir.join("extension.wasm");
1379        if fs.is_file(&extension_wasm_path).await {
1380            extension_manifest
1381                .lib
1382                .kind
1383                .get_or_insert(ExtensionLibraryKind::Rust);
1384        }
1385
1386        index.extensions.insert(
1387            extension_id.clone(),
1388            ExtensionIndexEntry {
1389                dev: is_dev,
1390                manifest: Arc::new(extension_manifest),
1391            },
1392        );
1393
1394        Ok(())
1395    }
1396}
1397
1398fn load_plugin_queries(root_path: &Path) -> LanguageQueries {
1399    let mut result = LanguageQueries::default();
1400    if let Some(entries) = std::fs::read_dir(root_path).log_err() {
1401        for entry in entries {
1402            let Some(entry) = entry.log_err() else {
1403                continue;
1404            };
1405            let path = entry.path();
1406            if let Some(remainder) = path.strip_prefix(root_path).ok().and_then(|p| p.to_str()) {
1407                if !remainder.ends_with(".scm") {
1408                    continue;
1409                }
1410                for (name, query) in QUERY_FILENAME_PREFIXES {
1411                    if remainder.starts_with(name) {
1412                        if let Some(contents) = std::fs::read_to_string(&path).log_err() {
1413                            match query(&mut result) {
1414                                None => *query(&mut result) = Some(contents.into()),
1415                                Some(r) => r.to_mut().push_str(contents.as_ref()),
1416                            }
1417                        }
1418                        break;
1419                    }
1420                }
1421            }
1422        }
1423    }
1424    result
1425}