extension_host.rs

   1mod anthropic_migration;
   2mod capability_granter;
   3mod copilot_migration;
   4pub mod extension_settings;
   5mod google_ai_migration;
   6pub mod headless_host;
   7mod open_router_migration;
   8mod openai_migration;
   9pub mod wasm_host;
  10
  11#[cfg(test)]
  12mod extension_store_test;
  13
  14use anyhow::{Context as _, Result, anyhow, bail};
  15use async_compression::futures::bufread::GzipDecoder;
  16use async_tar::Archive;
  17use client::ExtensionProvides;
  18use client::{Client, ExtensionMetadata, GetExtensionsResponse, proto, telemetry::Telemetry};
  19use collections::{BTreeMap, BTreeSet, HashSet, btree_map};
  20
  21pub use extension::ExtensionManifest;
  22use extension::extension_builder::{CompileExtensionOptions, ExtensionBuilder};
  23use extension::{
  24    ExtensionContextServerProxy, ExtensionDebugAdapterProviderProxy, ExtensionEvents,
  25    ExtensionGrammarProxy, ExtensionHostProxy, ExtensionLanguageModelProviderProxy,
  26    ExtensionLanguageProxy, ExtensionLanguageServerProxy, ExtensionSlashCommandProxy,
  27    ExtensionSnippetProxy, ExtensionThemeProxy,
  28};
  29use fs::{Fs, RemoveOptions};
  30use futures::future::join_all;
  31use futures::{
  32    AsyncReadExt as _, Future, FutureExt as _, StreamExt as _,
  33    channel::{
  34        mpsc::{UnboundedSender, unbounded},
  35        oneshot,
  36    },
  37    io::BufReader,
  38    select_biased,
  39};
  40use gpui::{
  41    App, AppContext as _, AsyncApp, Context, Entity, EventEmitter, Global, SharedString, Task,
  42    WeakEntity, actions,
  43};
  44use http_client::{AsyncBody, HttpClient, HttpClientWithUrl};
  45use language::{
  46    LanguageConfig, LanguageMatcher, LanguageName, LanguageQueries, LoadedLanguage,
  47    QUERY_FILENAME_PREFIXES, Rope,
  48};
  49use node_runtime::NodeRuntime;
  50use project::ContextProviderWithTasks;
  51use release_channel::ReleaseChannel;
  52use remote::RemoteClient;
  53use semver::Version;
  54use serde::{Deserialize, Serialize};
  55use settings::Settings;
  56use std::ops::RangeInclusive;
  57use std::str::FromStr;
  58use std::{
  59    cmp::Ordering,
  60    path::{self, Path, PathBuf},
  61    sync::Arc,
  62    time::Duration,
  63};
  64use url::Url;
  65use util::{ResultExt, paths::RemotePathBuf};
  66use wasm_host::llm_provider::ExtensionLanguageModelProvider;
  67use wasm_host::{
  68    WasmExtension, WasmHost,
  69    wit::{LlmModelInfo, LlmProviderInfo, is_supported_wasm_api_version, wasm_api_version_range},
  70};
  71
  72struct LlmProviderWithModels {
  73    provider_info: LlmProviderInfo,
  74    models: Vec<LlmModelInfo>,
  75    is_authenticated: bool,
  76    icon_path: Option<SharedString>,
  77    auth_config: Option<extension::LanguageModelAuthConfig>,
  78}
  79
  80pub use extension::{
  81    ExtensionLibraryKind, GrammarManifestEntry, OldExtensionManifest, SchemaVersion,
  82};
  83pub use extension_settings::ExtensionSettings;
  84
  85pub const RELOAD_DEBOUNCE_DURATION: Duration = Duration::from_millis(200);
  86const FS_WATCH_LATENCY: Duration = Duration::from_millis(100);
  87
  88/// Extension IDs that are being migrated from hardcoded LLM providers.
  89/// For backwards compatibility, if the user has the corresponding env var set,
  90/// we automatically enable env var reading for these extensions on first install.
  91const LEGACY_LLM_EXTENSION_IDS: &[&str] = &[
  92    "anthropic",
  93    "copilot-chat",
  94    "google-ai",
  95    "openrouter",
  96    "openai",
  97];
  98
  99/// Migrates legacy LLM provider extensions by auto-enabling env var reading
 100/// if the env var is currently present in the environment.
 101///
 102/// This is idempotent: if the env var is already in `allowed_env_vars`,
 103/// we skip. This means if a user explicitly removes it, it will be re-added on
 104/// next launch if the env var is still set - but that's predictable behavior.
 105fn migrate_legacy_llm_provider_env_var(manifest: &ExtensionManifest, cx: &mut App) {
 106    // Only apply migration to known legacy LLM extensions
 107    if !LEGACY_LLM_EXTENSION_IDS.contains(&manifest.id.as_ref()) {
 108        return;
 109    }
 110
 111    // Check each provider in the manifest
 112    for (provider_id, provider_entry) in &manifest.language_model_providers {
 113        let Some(auth_config) = &provider_entry.auth else {
 114            continue;
 115        };
 116        let Some(env_vars) = &auth_config.env_vars else {
 117            continue;
 118        };
 119
 120        let full_provider_id = format!("{}:{}", manifest.id, provider_id);
 121
 122        // For each env var, check if it's set and enable it if so
 123        for env_var_name in env_vars {
 124            let env_var_is_set = std::env::var(env_var_name)
 125                .map(|v| !v.is_empty())
 126                .unwrap_or(false);
 127
 128            if !env_var_is_set {
 129                continue;
 130            }
 131
 132            let settings_key: Arc<str> = format!("{}:{}", full_provider_id, env_var_name).into();
 133
 134            // Check if already enabled in settings
 135            let already_enabled = ExtensionSettings::get_global(cx)
 136                .allowed_env_vars
 137                .contains(settings_key.as_ref());
 138
 139            if already_enabled {
 140                continue;
 141            }
 142
 143            // Enable env var reading since the env var is set
 144            settings::update_settings_file(<dyn fs::Fs>::global(cx), cx, {
 145                let settings_key = settings_key.clone();
 146                move |settings, _| {
 147                    let allowed = settings
 148                        .extension
 149                        .allowed_env_vars
 150                        .get_or_insert_with(Vec::new);
 151
 152                    if !allowed
 153                        .iter()
 154                        .any(|id| id.as_ref() == settings_key.as_ref())
 155                    {
 156                        allowed.push(settings_key);
 157                    }
 158                }
 159            });
 160        }
 161    }
 162}
 163
 164/// The current extension [`SchemaVersion`] supported by Zed.
 165const CURRENT_SCHEMA_VERSION: SchemaVersion = SchemaVersion(1);
 166
 167/// Extensions that should no longer be loaded or downloaded.
 168///
 169/// These snippets should no longer be downloaded or loaded, because their
 170/// functionality has been integrated into the core editor.
 171const SUPPRESSED_EXTENSIONS: &[&str] = &["snippets", "ruff", "ty", "basedpyright"];
 172
 173/// Returns the [`SchemaVersion`] range that is compatible with this version of Zed.
 174pub fn schema_version_range() -> RangeInclusive<SchemaVersion> {
 175    SchemaVersion::ZERO..=CURRENT_SCHEMA_VERSION
 176}
 177
 178/// Returns whether the given extension version is compatible with this version of Zed.
 179pub fn is_version_compatible(
 180    release_channel: ReleaseChannel,
 181    extension_version: &ExtensionMetadata,
 182) -> bool {
 183    let schema_version = extension_version.manifest.schema_version.unwrap_or(0);
 184    if CURRENT_SCHEMA_VERSION.0 < schema_version {
 185        return false;
 186    }
 187
 188    if let Some(wasm_api_version) = extension_version
 189        .manifest
 190        .wasm_api_version
 191        .as_ref()
 192        .and_then(|wasm_api_version| Version::from_str(wasm_api_version).ok())
 193        && !is_supported_wasm_api_version(release_channel, wasm_api_version)
 194    {
 195        return false;
 196    }
 197
 198    true
 199}
 200
 201pub struct ExtensionStore {
 202    pub proxy: Arc<ExtensionHostProxy>,
 203    pub builder: Arc<ExtensionBuilder>,
 204    pub extension_index: ExtensionIndex,
 205    pub fs: Arc<dyn Fs>,
 206    pub http_client: Arc<HttpClientWithUrl>,
 207    pub telemetry: Option<Arc<Telemetry>>,
 208    pub reload_tx: UnboundedSender<Option<Arc<str>>>,
 209    pub reload_complete_senders: Vec<oneshot::Sender<()>>,
 210    pub installed_dir: PathBuf,
 211    pub outstanding_operations: BTreeMap<Arc<str>, ExtensionOperation>,
 212    pub index_path: PathBuf,
 213    pub modified_extensions: HashSet<Arc<str>>,
 214    pub wasm_host: Arc<WasmHost>,
 215    pub wasm_extensions: Vec<(Arc<ExtensionManifest>, WasmExtension)>,
 216    pub tasks: Vec<Task<()>>,
 217    pub remote_clients: Vec<WeakEntity<RemoteClient>>,
 218    pub ssh_registered_tx: UnboundedSender<()>,
 219}
 220
 221#[derive(Clone, Copy)]
 222pub enum ExtensionOperation {
 223    Upgrade,
 224    Install,
 225    /// Auto-install from settings - triggers legacy LLM provider migrations
 226    AutoInstall,
 227    Remove,
 228}
 229
 230#[derive(Clone)]
 231pub enum Event {
 232    ExtensionsUpdated,
 233    StartedReloading,
 234    ExtensionInstalled(Arc<str>),
 235    ExtensionUninstalled(Arc<str>),
 236    ExtensionFailedToLoad(Arc<str>),
 237}
 238
 239impl EventEmitter<Event> for ExtensionStore {}
 240
 241struct GlobalExtensionStore(Entity<ExtensionStore>);
 242
 243impl Global for GlobalExtensionStore {}
 244
 245#[derive(Debug, Deserialize, Serialize, Default, PartialEq, Eq)]
 246pub struct ExtensionIndex {
 247    pub extensions: BTreeMap<Arc<str>, ExtensionIndexEntry>,
 248    pub themes: BTreeMap<Arc<str>, ExtensionIndexThemeEntry>,
 249    #[serde(default)]
 250    pub icon_themes: BTreeMap<Arc<str>, ExtensionIndexIconThemeEntry>,
 251    pub languages: BTreeMap<LanguageName, ExtensionIndexLanguageEntry>,
 252}
 253
 254#[derive(Clone, PartialEq, Eq, Debug, Deserialize, Serialize)]
 255pub struct ExtensionIndexEntry {
 256    pub manifest: Arc<ExtensionManifest>,
 257    pub dev: bool,
 258}
 259
 260#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Deserialize, Serialize)]
 261pub struct ExtensionIndexThemeEntry {
 262    pub extension: Arc<str>,
 263    pub path: PathBuf,
 264}
 265
 266#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Deserialize, Serialize)]
 267pub struct ExtensionIndexIconThemeEntry {
 268    pub extension: Arc<str>,
 269    pub path: PathBuf,
 270}
 271
 272#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Deserialize, Serialize)]
 273pub struct ExtensionIndexLanguageEntry {
 274    pub extension: Arc<str>,
 275    pub path: PathBuf,
 276    pub matcher: LanguageMatcher,
 277    pub hidden: bool,
 278    pub grammar: Option<Arc<str>>,
 279}
 280
 281actions!(
 282    zed,
 283    [
 284        /// Reloads all installed extensions.
 285        ReloadExtensions
 286    ]
 287);
 288
 289pub fn init(
 290    extension_host_proxy: Arc<ExtensionHostProxy>,
 291    fs: Arc<dyn Fs>,
 292    client: Arc<Client>,
 293    node_runtime: NodeRuntime,
 294    cx: &mut App,
 295) {
 296    let store = cx.new(move |cx| {
 297        ExtensionStore::new(
 298            paths::extensions_dir().clone(),
 299            None,
 300            extension_host_proxy,
 301            fs,
 302            client.http_client(),
 303            client.http_client(),
 304            Some(client.telemetry().clone()),
 305            node_runtime,
 306            cx,
 307        )
 308    });
 309
 310    cx.on_action(|_: &ReloadExtensions, cx| {
 311        let store = cx.global::<GlobalExtensionStore>().0.clone();
 312        store.update(cx, |store, cx| drop(store.reload(None, cx)));
 313    });
 314
 315    cx.set_global(GlobalExtensionStore(store));
 316}
 317
 318impl ExtensionStore {
 319    pub fn try_global(cx: &App) -> Option<Entity<Self>> {
 320        cx.try_global::<GlobalExtensionStore>()
 321            .map(|store| store.0.clone())
 322    }
 323
 324    pub fn global(cx: &App) -> Entity<Self> {
 325        cx.global::<GlobalExtensionStore>().0.clone()
 326    }
 327
 328    pub fn new(
 329        extensions_dir: PathBuf,
 330        build_dir: Option<PathBuf>,
 331        extension_host_proxy: Arc<ExtensionHostProxy>,
 332        fs: Arc<dyn Fs>,
 333        http_client: Arc<HttpClientWithUrl>,
 334        builder_client: Arc<dyn HttpClient>,
 335        telemetry: Option<Arc<Telemetry>>,
 336        node_runtime: NodeRuntime,
 337        cx: &mut Context<Self>,
 338    ) -> Self {
 339        let work_dir = extensions_dir.join("work");
 340        let build_dir = build_dir.unwrap_or_else(|| extensions_dir.join("build"));
 341        let installed_dir = extensions_dir.join("installed");
 342        let index_path = extensions_dir.join("index.json");
 343
 344        let (reload_tx, mut reload_rx) = unbounded();
 345        let (connection_registered_tx, mut connection_registered_rx) = unbounded();
 346        let mut this = Self {
 347            proxy: extension_host_proxy.clone(),
 348            extension_index: Default::default(),
 349            installed_dir,
 350            index_path,
 351            builder: Arc::new(ExtensionBuilder::new(builder_client, build_dir)),
 352            outstanding_operations: Default::default(),
 353            modified_extensions: Default::default(),
 354            reload_complete_senders: Vec::new(),
 355            wasm_host: WasmHost::new(
 356                fs.clone(),
 357                http_client.clone(),
 358                node_runtime,
 359                extension_host_proxy,
 360                work_dir,
 361                cx,
 362            ),
 363            wasm_extensions: Vec::new(),
 364            fs,
 365            http_client,
 366            telemetry,
 367            reload_tx,
 368            tasks: Vec::new(),
 369
 370            remote_clients: Default::default(),
 371            ssh_registered_tx: connection_registered_tx,
 372        };
 373
 374        // The extensions store maintains an index file, which contains a complete
 375        // list of the installed extensions and the resources that they provide.
 376        // This index is loaded synchronously on startup.
 377        let (index_content, index_metadata, extensions_metadata) =
 378            cx.background_executor().block(async {
 379                futures::join!(
 380                    this.fs.load(&this.index_path),
 381                    this.fs.metadata(&this.index_path),
 382                    this.fs.metadata(&this.installed_dir),
 383                )
 384            });
 385
 386        // Normally, there is no need to rebuild the index. But if the index file
 387        // is invalid or is out-of-date according to the filesystem mtimes, then
 388        // it must be asynchronously rebuilt.
 389        let mut extension_index = ExtensionIndex::default();
 390        let mut extension_index_needs_rebuild = true;
 391        if let Ok(index_content) = index_content
 392            && let Some(index) = serde_json::from_str(&index_content).log_err()
 393        {
 394            extension_index = index;
 395            if let (Ok(Some(index_metadata)), Ok(Some(extensions_metadata))) =
 396                (index_metadata, extensions_metadata)
 397                && index_metadata
 398                    .mtime
 399                    .bad_is_greater_than(extensions_metadata.mtime)
 400            {
 401                extension_index_needs_rebuild = false;
 402            }
 403        }
 404
 405        // Immediately load all of the extensions in the initial manifest. If the
 406        // index needs to be rebuild, then enqueue
 407        let load_initial_extensions = this.extensions_updated(extension_index, cx);
 408        let mut reload_future = None;
 409        if extension_index_needs_rebuild {
 410            reload_future = Some(this.reload(None, cx));
 411        }
 412
 413        cx.spawn(async move |this, cx| {
 414            if let Some(future) = reload_future {
 415                future.await;
 416            }
 417            this.update(cx, |this, cx| this.auto_install_extensions(cx))
 418                .ok();
 419            this.update(cx, |this, cx| this.check_for_updates(cx)).ok();
 420        })
 421        .detach();
 422
 423        // Perform all extension loading in a single task to ensure that we
 424        // never attempt to simultaneously load/unload extensions from multiple
 425        // parallel tasks.
 426        this.tasks.push(cx.spawn(async move |this, cx| {
 427            async move {
 428                load_initial_extensions.await;
 429
 430                let mut index_changed = false;
 431                let mut debounce_timer = cx.background_spawn(futures::future::pending()).fuse();
 432                loop {
 433                    select_biased! {
 434                        _ = debounce_timer => {
 435                            if index_changed {
 436                                let index = this
 437                                    .update(cx, |this, cx| this.rebuild_extension_index(cx))?
 438                                    .await;
 439                                this.update(cx, |this, cx| this.extensions_updated(index, cx))?
 440                                    .await;
 441                                index_changed = false;
 442                            }
 443
 444                            Self::update_remote_clients(&this, cx).await?;
 445                        }
 446                        _ = connection_registered_rx.next() => {
 447                            debounce_timer = cx
 448                                .background_executor()
 449                                .timer(RELOAD_DEBOUNCE_DURATION)
 450                                .fuse();
 451                        }
 452                        extension_id = reload_rx.next() => {
 453                            let Some(extension_id) = extension_id else { break; };
 454                            this.update(cx, |this, _| {
 455                                this.modified_extensions.extend(extension_id);
 456                            })?;
 457                            index_changed = true;
 458                            debounce_timer = cx
 459                                .background_executor()
 460                                .timer(RELOAD_DEBOUNCE_DURATION)
 461                                .fuse();
 462                        }
 463                    }
 464                }
 465
 466                anyhow::Ok(())
 467            }
 468            .map(drop)
 469            .await;
 470        }));
 471
 472        // Watch the installed extensions directory for changes. Whenever changes are
 473        // detected, rebuild the extension index, and load/unload any extensions that
 474        // have been added, removed, or modified.
 475        this.tasks.push(cx.background_spawn({
 476            let fs = this.fs.clone();
 477            let reload_tx = this.reload_tx.clone();
 478            let installed_dir = this.installed_dir.clone();
 479            async move {
 480                let (mut paths, _) = fs.watch(&installed_dir, FS_WATCH_LATENCY).await;
 481                while let Some(events) = paths.next().await {
 482                    for event in events {
 483                        let Ok(event_path) = event.path.strip_prefix(&installed_dir) else {
 484                            continue;
 485                        };
 486
 487                        if let Some(path::Component::Normal(extension_dir_name)) =
 488                            event_path.components().next()
 489                            && let Some(extension_id) = extension_dir_name.to_str()
 490                        {
 491                            reload_tx.unbounded_send(Some(extension_id.into())).ok();
 492                        }
 493                    }
 494                }
 495            }
 496        }));
 497
 498        this
 499    }
 500
 501    pub fn reload(
 502        &mut self,
 503        modified_extension: Option<Arc<str>>,
 504        cx: &mut Context<Self>,
 505    ) -> impl Future<Output = ()> + use<> {
 506        let (tx, rx) = oneshot::channel();
 507        self.reload_complete_senders.push(tx);
 508        self.reload_tx
 509            .unbounded_send(modified_extension)
 510            .expect("reload task exited");
 511        cx.emit(Event::StartedReloading);
 512
 513        async move {
 514            rx.await.ok();
 515        }
 516    }
 517
 518    fn extensions_dir(&self) -> PathBuf {
 519        self.installed_dir.clone()
 520    }
 521
 522    pub fn outstanding_operations(&self) -> &BTreeMap<Arc<str>, ExtensionOperation> {
 523        &self.outstanding_operations
 524    }
 525
 526    pub fn installed_extensions(&self) -> &BTreeMap<Arc<str>, ExtensionIndexEntry> {
 527        &self.extension_index.extensions
 528    }
 529
 530    pub fn dev_extensions(&self) -> impl Iterator<Item = &Arc<ExtensionManifest>> {
 531        self.extension_index
 532            .extensions
 533            .values()
 534            .filter_map(|extension| extension.dev.then_some(&extension.manifest))
 535    }
 536
 537    pub fn extension_manifest_for_id(&self, extension_id: &str) -> Option<&Arc<ExtensionManifest>> {
 538        self.extension_index
 539            .extensions
 540            .get(extension_id)
 541            .map(|extension| &extension.manifest)
 542    }
 543
 544    /// Returns the names of themes provided by extensions.
 545    pub fn extension_themes<'a>(
 546        &'a self,
 547        extension_id: &'a str,
 548    ) -> impl Iterator<Item = &'a Arc<str>> {
 549        self.extension_index
 550            .themes
 551            .iter()
 552            .filter_map(|(name, theme)| theme.extension.as_ref().eq(extension_id).then_some(name))
 553    }
 554
 555    /// Returns the path to the theme file within an extension, if there is an
 556    /// extension that provides the theme.
 557    pub fn path_to_extension_theme(&self, theme_name: &str) -> Option<PathBuf> {
 558        let entry = self.extension_index.themes.get(theme_name)?;
 559
 560        Some(
 561            self.extensions_dir()
 562                .join(entry.extension.as_ref())
 563                .join(&entry.path),
 564        )
 565    }
 566
 567    /// Returns the names of icon themes provided by extensions.
 568    pub fn extension_icon_themes<'a>(
 569        &'a self,
 570        extension_id: &'a str,
 571    ) -> impl Iterator<Item = &'a Arc<str>> {
 572        self.extension_index
 573            .icon_themes
 574            .iter()
 575            .filter_map(|(name, icon_theme)| {
 576                icon_theme
 577                    .extension
 578                    .as_ref()
 579                    .eq(extension_id)
 580                    .then_some(name)
 581            })
 582    }
 583
 584    /// Returns the path to the icon theme file within an extension, if there is
 585    /// an extension that provides the icon theme.
 586    pub fn path_to_extension_icon_theme(
 587        &self,
 588        icon_theme_name: &str,
 589    ) -> Option<(PathBuf, PathBuf)> {
 590        let entry = self.extension_index.icon_themes.get(icon_theme_name)?;
 591
 592        let icon_theme_path = self
 593            .extensions_dir()
 594            .join(entry.extension.as_ref())
 595            .join(&entry.path);
 596        let icons_root_path = self.extensions_dir().join(entry.extension.as_ref());
 597
 598        Some((icon_theme_path, icons_root_path))
 599    }
 600
 601    pub fn fetch_extensions(
 602        &self,
 603        search: Option<&str>,
 604        provides_filter: Option<&BTreeSet<ExtensionProvides>>,
 605        cx: &mut Context<Self>,
 606    ) -> Task<Result<Vec<ExtensionMetadata>>> {
 607        let version = CURRENT_SCHEMA_VERSION.to_string();
 608        let mut query = vec![("max_schema_version", version.as_str())];
 609        if let Some(search) = search {
 610            query.push(("filter", search));
 611        }
 612
 613        let provides_filter = provides_filter.map(|provides_filter| {
 614            provides_filter
 615                .iter()
 616                .map(|provides| provides.to_string())
 617                .collect::<Vec<_>>()
 618                .join(",")
 619        });
 620        if let Some(provides_filter) = provides_filter.as_deref() {
 621            query.push(("provides", provides_filter));
 622        }
 623
 624        self.fetch_extensions_from_api("/extensions", &query, cx)
 625    }
 626
 627    pub fn fetch_extensions_with_update_available(
 628        &mut self,
 629        cx: &mut Context<Self>,
 630    ) -> Task<Result<Vec<ExtensionMetadata>>> {
 631        let schema_versions = schema_version_range();
 632        let wasm_api_versions = wasm_api_version_range(ReleaseChannel::global(cx));
 633        let extension_settings = ExtensionSettings::get_global(cx);
 634        let extension_ids = self
 635            .extension_index
 636            .extensions
 637            .iter()
 638            .filter(|(id, entry)| !entry.dev && extension_settings.should_auto_update(id))
 639            .map(|(id, _)| id.as_ref())
 640            .collect::<Vec<_>>()
 641            .join(",");
 642        let task = self.fetch_extensions_from_api(
 643            "/extensions/updates",
 644            &[
 645                ("min_schema_version", &schema_versions.start().to_string()),
 646                ("max_schema_version", &schema_versions.end().to_string()),
 647                (
 648                    "min_wasm_api_version",
 649                    &wasm_api_versions.start().to_string(),
 650                ),
 651                ("max_wasm_api_version", &wasm_api_versions.end().to_string()),
 652                ("ids", &extension_ids),
 653            ],
 654            cx,
 655        );
 656        cx.spawn(async move |this, cx| {
 657            let extensions = task.await?;
 658            this.update(cx, |this, _cx| {
 659                extensions
 660                    .into_iter()
 661                    .filter(|extension| {
 662                        this.extension_index
 663                            .extensions
 664                            .get(&extension.id)
 665                            .is_none_or(|installed_extension| {
 666                                installed_extension.manifest.version != extension.manifest.version
 667                            })
 668                    })
 669                    .collect()
 670            })
 671        })
 672    }
 673
 674    pub fn fetch_extension_versions(
 675        &self,
 676        extension_id: &str,
 677        cx: &mut Context<Self>,
 678    ) -> Task<Result<Vec<ExtensionMetadata>>> {
 679        self.fetch_extensions_from_api(&format!("/extensions/{extension_id}"), &[], cx)
 680    }
 681
 682    /// Installs any extensions that should be included with Zed by default.
 683    ///
 684    /// This can be used to make certain functionality provided by extensions
 685    /// available out-of-the-box.
 686    pub fn auto_install_extensions(&mut self, cx: &mut Context<Self>) {
 687        if cfg!(test) {
 688            return;
 689        }
 690
 691        let extension_settings = ExtensionSettings::get_global(cx);
 692
 693        let extensions_to_install = extension_settings
 694            .auto_install_extensions
 695            .keys()
 696            .filter(|extension_id| extension_settings.should_auto_install(extension_id))
 697            .filter(|extension_id| {
 698                let is_already_installed = self
 699                    .extension_index
 700                    .extensions
 701                    .contains_key(extension_id.as_ref());
 702                !is_already_installed && !SUPPRESSED_EXTENSIONS.contains(&extension_id.as_ref())
 703            })
 704            .cloned()
 705            .collect::<Vec<_>>();
 706
 707        cx.spawn(async move |this, cx| {
 708            for extension_id in extensions_to_install {
 709                // When enabled, this checks if an extension exists locally in the repo's extensions/
 710                // directory and installs it as a dev extension instead of fetching from the registry.
 711                // This is useful for testing auto-installed extensions before they've been published.
 712                // Set to `true` only during local development/testing of new auto-install extensions.
 713                #[cfg(debug_assertions)]
 714                const DEBUG_ALLOW_UNPUBLISHED_AUTO_EXTENSIONS: bool = false;
 715
 716                #[cfg(debug_assertions)]
 717                if DEBUG_ALLOW_UNPUBLISHED_AUTO_EXTENSIONS {
 718                    let local_extension_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
 719                        .parent()
 720                        .unwrap()
 721                        .parent()
 722                        .unwrap()
 723                        .join("extensions")
 724                        .join(extension_id.as_ref());
 725
 726                    if local_extension_path.exists() {
 727                        // Force-remove existing extension directory if it exists and isn't a symlink
 728                        // This handles the case where the extension was previously installed from the registry
 729                        if let Some(installed_dir) = this
 730                            .update(cx, |this, _cx| this.installed_dir.clone())
 731                            .ok()
 732                        {
 733                            let existing_path = installed_dir.join(extension_id.as_ref());
 734                            if existing_path.exists() {
 735                                let metadata = std::fs::symlink_metadata(&existing_path);
 736                                let is_symlink = metadata.map(|m| m.is_symlink()).unwrap_or(false);
 737                                if !is_symlink {
 738                                    if let Err(e) = std::fs::remove_dir_all(&existing_path) {
 739                                        log::error!(
 740                                            "Failed to remove existing extension directory {:?}: {}",
 741                                            existing_path,
 742                                            e
 743                                        );
 744                                    }
 745                                }
 746                            }
 747                        }
 748
 749                        if let Some(task) = this
 750                            .update(cx, |this, cx| {
 751                                this.install_dev_extension(local_extension_path, cx)
 752                            })
 753                            .ok()
 754                        {
 755                            task.await.log_err();
 756                        }
 757                        continue;
 758                    }
 759                }
 760
 761                this.update(cx, |this, cx| {
 762                    this.auto_install_latest_extension(extension_id.clone(), cx);
 763                })
 764                .ok();
 765            }
 766        })
 767        .detach();
 768    }
 769
 770    pub fn check_for_updates(&mut self, cx: &mut Context<Self>) {
 771        let task = self.fetch_extensions_with_update_available(cx);
 772        cx.spawn(async move |this, cx| Self::upgrade_extensions(this, task.await?, cx).await)
 773            .detach();
 774    }
 775
 776    async fn upgrade_extensions(
 777        this: WeakEntity<Self>,
 778        extensions: Vec<ExtensionMetadata>,
 779        cx: &mut AsyncApp,
 780    ) -> Result<()> {
 781        for extension in extensions {
 782            let task = this.update(cx, |this, cx| {
 783                if let Some(installed_extension) =
 784                    this.extension_index.extensions.get(&extension.id)
 785                {
 786                    let installed_version =
 787                        Version::from_str(&installed_extension.manifest.version).ok()?;
 788                    let latest_version = Version::from_str(&extension.manifest.version).ok()?;
 789
 790                    if installed_version >= latest_version {
 791                        return None;
 792                    }
 793                }
 794
 795                Some(this.upgrade_extension(extension.id, extension.manifest.version, cx))
 796            })?;
 797
 798            if let Some(task) = task {
 799                task.await.log_err();
 800            }
 801        }
 802        anyhow::Ok(())
 803    }
 804
 805    fn fetch_extensions_from_api(
 806        &self,
 807        path: &str,
 808        query: &[(&str, &str)],
 809        cx: &mut Context<ExtensionStore>,
 810    ) -> Task<Result<Vec<ExtensionMetadata>>> {
 811        let url = self.http_client.build_zed_api_url(path, query);
 812        let http_client = self.http_client.clone();
 813        cx.spawn(async move |_, _| {
 814            let mut response = http_client
 815                .get(url?.as_ref(), AsyncBody::empty(), true)
 816                .await?;
 817
 818            let mut body = Vec::new();
 819            response
 820                .body_mut()
 821                .read_to_end(&mut body)
 822                .await
 823                .context("error reading extensions")?;
 824
 825            if response.status().is_client_error() {
 826                let text = String::from_utf8_lossy(body.as_slice());
 827                bail!(
 828                    "status error {}, response: {text:?}",
 829                    response.status().as_u16()
 830                );
 831            }
 832
 833            let mut response: GetExtensionsResponse = serde_json::from_slice(&body)?;
 834
 835            response
 836                .data
 837                .retain(|extension| !SUPPRESSED_EXTENSIONS.contains(&extension.id.as_ref()));
 838
 839            Ok(response.data)
 840        })
 841    }
 842
 843    pub fn install_extension(
 844        &mut self,
 845        extension_id: Arc<str>,
 846        version: Arc<str>,
 847        cx: &mut Context<Self>,
 848    ) {
 849        self.install_or_upgrade_extension(extension_id, version, ExtensionOperation::Install, cx)
 850            .detach_and_log_err(cx);
 851    }
 852
 853    fn install_or_upgrade_extension_at_endpoint(
 854        &mut self,
 855        extension_id: Arc<str>,
 856        url: Url,
 857        operation: ExtensionOperation,
 858        cx: &mut Context<Self>,
 859    ) -> Task<Result<()>> {
 860        let extension_dir = self.installed_dir.join(extension_id.as_ref());
 861        let http_client = self.http_client.clone();
 862        let fs = self.fs.clone();
 863
 864        match self.outstanding_operations.entry(extension_id.clone()) {
 865            btree_map::Entry::Occupied(_) => return Task::ready(Ok(())),
 866            btree_map::Entry::Vacant(e) => e.insert(operation),
 867        };
 868        cx.notify();
 869
 870        cx.spawn(async move |this, cx| {
 871            let _finish = cx.on_drop(&this, {
 872                let extension_id = extension_id.clone();
 873                move |this, cx| {
 874                    this.outstanding_operations.remove(extension_id.as_ref());
 875                    cx.notify();
 876                }
 877            });
 878
 879            let mut response = http_client
 880                .get(url.as_ref(), Default::default(), true)
 881                .await
 882                .context("downloading extension")?;
 883
 884            fs.remove_dir(
 885                &extension_dir,
 886                RemoveOptions {
 887                    recursive: true,
 888                    ignore_if_not_exists: true,
 889                },
 890            )
 891            .await?;
 892
 893            let content_length = response
 894                .headers()
 895                .get(http_client::http::header::CONTENT_LENGTH)
 896                .and_then(|value| value.to_str().ok()?.parse::<usize>().ok());
 897
 898            let mut body = BufReader::new(response.body_mut());
 899            let mut tar_gz_bytes = Vec::new();
 900            body.read_to_end(&mut tar_gz_bytes).await?;
 901
 902            if let Some(content_length) = content_length {
 903                let actual_len = tar_gz_bytes.len();
 904                if content_length != actual_len {
 905                    bail!(concat!(
 906                        "downloaded extension size {actual_len} ",
 907                        "does not match content length {content_length}"
 908                    ));
 909                }
 910            }
 911            let decompressed_bytes = GzipDecoder::new(BufReader::new(tar_gz_bytes.as_slice()));
 912            let archive = Archive::new(decompressed_bytes);
 913            archive.unpack(extension_dir).await?;
 914            this.update(cx, |this, cx| this.reload(Some(extension_id.clone()), cx))?
 915                .await;
 916
 917            if matches!(
 918                operation,
 919                ExtensionOperation::Install | ExtensionOperation::AutoInstall
 920            ) {
 921                this.update(cx, |this, cx| {
 922                    cx.emit(Event::ExtensionInstalled(extension_id.clone()));
 923                    if let Some(events) = ExtensionEvents::try_global(cx)
 924                        && let Some(manifest) = this.extension_manifest_for_id(&extension_id)
 925                    {
 926                        events.update(cx, |this, cx| {
 927                            this.emit(extension::Event::ExtensionInstalled(manifest.clone()), cx)
 928                        });
 929                    }
 930
 931                    // Run legacy LLM provider migrations only for auto-installed extensions
 932                    if matches!(operation, ExtensionOperation::AutoInstall) {
 933                        if let Some(manifest) = this.extension_manifest_for_id(&extension_id) {
 934                            migrate_legacy_llm_provider_env_var(&manifest, cx);
 935                        }
 936                        copilot_migration::migrate_copilot_credentials_if_needed(&extension_id, cx);
 937                        anthropic_migration::migrate_anthropic_credentials_if_needed(
 938                            &extension_id,
 939                            cx,
 940                        );
 941                        google_ai_migration::migrate_google_ai_credentials_if_needed(
 942                            &extension_id,
 943                            cx,
 944                        );
 945                        openai_migration::migrate_openai_credentials_if_needed(&extension_id, cx);
 946                        open_router_migration::migrate_open_router_credentials_if_needed(
 947                            &extension_id,
 948                            cx,
 949                        );
 950                    }
 951                })
 952                .ok();
 953            }
 954
 955            anyhow::Ok(())
 956        })
 957    }
 958
 959    pub fn install_latest_extension(&mut self, extension_id: Arc<str>, cx: &mut Context<Self>) {
 960        self.install_latest_extension_with_operation(extension_id, ExtensionOperation::Install, cx);
 961    }
 962
 963    /// Auto-install an extension, triggering legacy LLM provider migrations.
 964    fn auto_install_latest_extension(&mut self, extension_id: Arc<str>, cx: &mut Context<Self>) {
 965        self.install_latest_extension_with_operation(
 966            extension_id,
 967            ExtensionOperation::AutoInstall,
 968            cx,
 969        );
 970    }
 971
 972    fn install_latest_extension_with_operation(
 973        &mut self,
 974        extension_id: Arc<str>,
 975        operation: ExtensionOperation,
 976        cx: &mut Context<Self>,
 977    ) {
 978        let schema_versions = schema_version_range();
 979        let wasm_api_versions = wasm_api_version_range(ReleaseChannel::global(cx));
 980
 981        let Some(url) = self
 982            .http_client
 983            .build_zed_api_url(
 984                &format!("/extensions/{extension_id}/download"),
 985                &[
 986                    ("min_schema_version", &schema_versions.start().to_string()),
 987                    ("max_schema_version", &schema_versions.end().to_string()),
 988                    (
 989                        "min_wasm_api_version",
 990                        &wasm_api_versions.start().to_string(),
 991                    ),
 992                    ("max_wasm_api_version", &wasm_api_versions.end().to_string()),
 993                ],
 994            )
 995            .log_err()
 996        else {
 997            return;
 998        };
 999
1000        self.install_or_upgrade_extension_at_endpoint(extension_id, url, operation, cx)
1001            .detach_and_log_err(cx);
1002    }
1003
1004    pub fn upgrade_extension(
1005        &mut self,
1006        extension_id: Arc<str>,
1007        version: Arc<str>,
1008        cx: &mut Context<Self>,
1009    ) -> Task<Result<()>> {
1010        self.install_or_upgrade_extension(extension_id, version, ExtensionOperation::Upgrade, cx)
1011    }
1012
1013    fn install_or_upgrade_extension(
1014        &mut self,
1015        extension_id: Arc<str>,
1016        version: Arc<str>,
1017        operation: ExtensionOperation,
1018        cx: &mut Context<Self>,
1019    ) -> Task<Result<()>> {
1020        let Some(url) = self
1021            .http_client
1022            .build_zed_api_url(
1023                &format!("/extensions/{extension_id}/{version}/download"),
1024                &[],
1025            )
1026            .log_err()
1027        else {
1028            return Task::ready(Ok(()));
1029        };
1030
1031        self.install_or_upgrade_extension_at_endpoint(extension_id, url, operation, cx)
1032    }
1033
1034    pub fn uninstall_extension(
1035        &mut self,
1036        extension_id: Arc<str>,
1037        cx: &mut Context<Self>,
1038    ) -> Task<Result<()>> {
1039        let extension_dir = self.installed_dir.join(extension_id.as_ref());
1040        let work_dir = self.wasm_host.work_dir.join(extension_id.as_ref());
1041        let fs = self.fs.clone();
1042
1043        let extension_manifest = self.extension_manifest_for_id(&extension_id).cloned();
1044
1045        match self.outstanding_operations.entry(extension_id.clone()) {
1046            btree_map::Entry::Occupied(_) => return Task::ready(Ok(())),
1047            btree_map::Entry::Vacant(e) => e.insert(ExtensionOperation::Remove),
1048        };
1049
1050        cx.spawn(async move |extension_store, cx| {
1051            let _finish = cx.on_drop(&extension_store, {
1052                let extension_id = extension_id.clone();
1053                move |this, cx| {
1054                    this.outstanding_operations.remove(extension_id.as_ref());
1055                    cx.notify();
1056                }
1057            });
1058
1059            fs.remove_dir(
1060                &extension_dir,
1061                RemoveOptions {
1062                    recursive: true,
1063                    ignore_if_not_exists: true,
1064                },
1065            )
1066            .await
1067            .with_context(|| format!("Removing extension dir {extension_dir:?}"))?;
1068
1069            extension_store
1070                .update(cx, |extension_store, cx| extension_store.reload(None, cx))?
1071                .await;
1072
1073            // There's a race between wasm extension fully stopping and the directory removal.
1074            // On Windows, it's impossible to remove a directory that has a process running in it.
1075            for i in 0..3 {
1076                cx.background_executor()
1077                    .timer(Duration::from_millis(i * 100))
1078                    .await;
1079                let removal_result = fs
1080                    .remove_dir(
1081                        &work_dir,
1082                        RemoveOptions {
1083                            recursive: true,
1084                            ignore_if_not_exists: true,
1085                        },
1086                    )
1087                    .await;
1088                match removal_result {
1089                    Ok(()) => break,
1090                    Err(e) => {
1091                        if i == 2 {
1092                            log::error!("Failed to remove extension work dir {work_dir:?} : {e}");
1093                        }
1094                    }
1095                }
1096            }
1097
1098            extension_store.update(cx, |_, cx| {
1099                cx.emit(Event::ExtensionUninstalled(extension_id.clone()));
1100                if let Some(events) = ExtensionEvents::try_global(cx)
1101                    && let Some(manifest) = extension_manifest
1102                {
1103                    events.update(cx, |this, cx| {
1104                        this.emit(extension::Event::ExtensionUninstalled(manifest.clone()), cx)
1105                    });
1106                }
1107            })?;
1108
1109            anyhow::Ok(())
1110        })
1111    }
1112
1113    pub fn install_dev_extension(
1114        &mut self,
1115        extension_source_path: PathBuf,
1116        cx: &mut Context<Self>,
1117    ) -> Task<Result<()>> {
1118        let extensions_dir = self.extensions_dir();
1119        let fs = self.fs.clone();
1120        let builder = self.builder.clone();
1121
1122        cx.spawn(async move |this, cx| {
1123            let mut extension_manifest =
1124                ExtensionManifest::load(fs.clone(), &extension_source_path).await?;
1125            let extension_id = extension_manifest.id.clone();
1126
1127            if let Some(uninstall_task) = this
1128                .update(cx, |this, cx| {
1129                    this.extension_index
1130                        .extensions
1131                        .get(extension_id.as_ref())
1132                        .is_some_and(|index_entry| !index_entry.dev)
1133                        .then(|| this.uninstall_extension(extension_id.clone(), cx))
1134                })
1135                .ok()
1136                .flatten()
1137            {
1138                uninstall_task.await.log_err();
1139            }
1140
1141            if !this.update(cx, |this, cx| {
1142                match this.outstanding_operations.entry(extension_id.clone()) {
1143                    btree_map::Entry::Occupied(_) => return false,
1144                    btree_map::Entry::Vacant(e) => e.insert(ExtensionOperation::Install),
1145                };
1146                cx.notify();
1147                true
1148            })? {
1149                return Ok(());
1150            }
1151
1152            let _finish = cx.on_drop(&this, {
1153                let extension_id = extension_id.clone();
1154                move |this, cx| {
1155                    this.outstanding_operations.remove(extension_id.as_ref());
1156                    cx.notify();
1157                }
1158            });
1159
1160            cx.background_spawn({
1161                let extension_source_path = extension_source_path.clone();
1162                let fs = fs.clone();
1163                async move {
1164                    builder
1165                        .compile_extension(
1166                            &extension_source_path,
1167                            &mut extension_manifest,
1168                            CompileExtensionOptions { release: false },
1169                            fs,
1170                        )
1171                        .await
1172                }
1173            })
1174            .await
1175            .inspect_err(|error| {
1176                util::log_err(error);
1177            })?;
1178
1179            let output_path = &extensions_dir.join(extension_id.as_ref());
1180            if let Some(metadata) = fs.metadata(output_path).await? {
1181                if metadata.is_symlink {
1182                    fs.remove_file(
1183                        output_path,
1184                        RemoveOptions {
1185                            recursive: false,
1186                            ignore_if_not_exists: true,
1187                        },
1188                    )
1189                    .await?;
1190                } else {
1191                    bail!("extension {extension_id} is still installed");
1192                }
1193            }
1194
1195            fs.create_symlink(output_path, extension_source_path)
1196                .await?;
1197
1198            this.update(cx, |this, cx| this.reload(None, cx))?.await;
1199            this.update(cx, |this, cx| {
1200                cx.emit(Event::ExtensionInstalled(extension_id.clone()));
1201                if let Some(events) = ExtensionEvents::try_global(cx)
1202                    && let Some(manifest) = this.extension_manifest_for_id(&extension_id)
1203                {
1204                    events.update(cx, |this, cx| {
1205                        this.emit(extension::Event::ExtensionInstalled(manifest.clone()), cx)
1206                    });
1207                }
1208            })?;
1209
1210            Ok(())
1211        })
1212    }
1213
1214    pub fn rebuild_dev_extension(&mut self, extension_id: Arc<str>, cx: &mut Context<Self>) {
1215        let path = self.installed_dir.join(extension_id.as_ref());
1216        let builder = self.builder.clone();
1217        let fs = self.fs.clone();
1218
1219        match self.outstanding_operations.entry(extension_id.clone()) {
1220            btree_map::Entry::Occupied(_) => return,
1221            btree_map::Entry::Vacant(e) => e.insert(ExtensionOperation::Upgrade),
1222        };
1223
1224        cx.notify();
1225        let compile = cx.background_spawn(async move {
1226            let mut manifest = ExtensionManifest::load(fs.clone(), &path).await?;
1227            builder
1228                .compile_extension(
1229                    &path,
1230                    &mut manifest,
1231                    CompileExtensionOptions { release: true },
1232                    fs,
1233                )
1234                .await
1235        });
1236
1237        cx.spawn(async move |this, cx| {
1238            let result = compile.await;
1239
1240            this.update(cx, |this, cx| {
1241                this.outstanding_operations.remove(&extension_id);
1242                cx.notify();
1243            })?;
1244
1245            if result.is_ok() {
1246                this.update(cx, |this, cx| this.reload(Some(extension_id), cx))?
1247                    .await;
1248            }
1249
1250            result
1251        })
1252        .detach_and_log_err(cx)
1253    }
1254
1255    /// Updates the set of installed extensions.
1256    ///
1257    /// First, this unloads any themes, languages, or grammars that are
1258    /// no longer in the manifest, or whose files have changed on disk.
1259    /// Then it loads any themes, languages, or grammars that are newly
1260    /// added to the manifest, or whose files have changed on disk.
1261    fn extensions_updated(
1262        &mut self,
1263        mut new_index: ExtensionIndex,
1264        cx: &mut Context<Self>,
1265    ) -> Task<()> {
1266        let old_index = &self.extension_index;
1267
1268        new_index
1269            .extensions
1270            .retain(|extension_id, _| !SUPPRESSED_EXTENSIONS.contains(&extension_id.as_ref()));
1271
1272        // Determine which extensions need to be loaded and unloaded, based
1273        // on the changes to the manifest and the extensions that we know have been
1274        // modified.
1275        let mut extensions_to_unload = Vec::default();
1276        let mut extensions_to_load = Vec::default();
1277        {
1278            let mut old_keys = old_index.extensions.iter().peekable();
1279            let mut new_keys = new_index.extensions.iter().peekable();
1280            loop {
1281                match (old_keys.peek(), new_keys.peek()) {
1282                    (None, None) => break,
1283                    (None, Some(_)) => {
1284                        extensions_to_load.push(new_keys.next().unwrap().0.clone());
1285                    }
1286                    (Some(_), None) => {
1287                        extensions_to_unload.push(old_keys.next().unwrap().0.clone());
1288                    }
1289                    (Some((old_key, _)), Some((new_key, _))) => match old_key.cmp(new_key) {
1290                        Ordering::Equal => {
1291                            let (old_key, old_value) = old_keys.next().unwrap();
1292                            let (new_key, new_value) = new_keys.next().unwrap();
1293                            if old_value != new_value || self.modified_extensions.contains(old_key)
1294                            {
1295                                extensions_to_unload.push(old_key.clone());
1296                                extensions_to_load.push(new_key.clone());
1297                            }
1298                        }
1299                        Ordering::Less => {
1300                            extensions_to_unload.push(old_keys.next().unwrap().0.clone());
1301                        }
1302                        Ordering::Greater => {
1303                            extensions_to_load.push(new_keys.next().unwrap().0.clone());
1304                        }
1305                    },
1306                }
1307            }
1308            self.modified_extensions.clear();
1309        }
1310
1311        if extensions_to_load.is_empty() && extensions_to_unload.is_empty() {
1312            self.reload_complete_senders.clear();
1313            return Task::ready(());
1314        }
1315
1316        let extension_ids = extensions_to_load
1317            .iter()
1318            .filter_map(|id| {
1319                Some((
1320                    id.clone(),
1321                    new_index.extensions.get(id)?.manifest.version.clone(),
1322                ))
1323            })
1324            .collect::<Vec<_>>();
1325
1326        telemetry::event!("Extensions Loaded", id_and_versions = extension_ids);
1327
1328        let themes_to_remove = old_index
1329            .themes
1330            .iter()
1331            .filter_map(|(name, entry)| {
1332                if extensions_to_unload.contains(&entry.extension) {
1333                    Some(name.clone().into())
1334                } else {
1335                    None
1336                }
1337            })
1338            .collect::<Vec<_>>();
1339        let icon_themes_to_remove = old_index
1340            .icon_themes
1341            .iter()
1342            .filter_map(|(name, entry)| {
1343                if extensions_to_unload.contains(&entry.extension) {
1344                    Some(name.clone().into())
1345                } else {
1346                    None
1347                }
1348            })
1349            .collect::<Vec<_>>();
1350        let languages_to_remove = old_index
1351            .languages
1352            .iter()
1353            .filter_map(|(name, entry)| {
1354                if extensions_to_unload.contains(&entry.extension) {
1355                    Some(name.clone())
1356                } else {
1357                    None
1358                }
1359            })
1360            .collect::<Vec<_>>();
1361        let mut grammars_to_remove = Vec::new();
1362        let mut server_removal_tasks = Vec::with_capacity(extensions_to_unload.len());
1363        for extension_id in &extensions_to_unload {
1364            let Some(extension) = old_index.extensions.get(extension_id) else {
1365                continue;
1366            };
1367            grammars_to_remove.extend(extension.manifest.grammars.keys().cloned());
1368            for (language_server_name, config) in &extension.manifest.language_servers {
1369                for language in config.languages() {
1370                    server_removal_tasks.push(self.proxy.remove_language_server(
1371                        &language,
1372                        language_server_name,
1373                        cx,
1374                    ));
1375                }
1376            }
1377
1378            for server_id in extension.manifest.context_servers.keys() {
1379                self.proxy.unregister_context_server(server_id.clone(), cx);
1380            }
1381            for adapter in extension.manifest.debug_adapters.keys() {
1382                self.proxy.unregister_debug_adapter(adapter.clone());
1383            }
1384            for locator in extension.manifest.debug_locators.keys() {
1385                self.proxy.unregister_debug_locator(locator.clone());
1386            }
1387            for command_name in extension.manifest.slash_commands.keys() {
1388                self.proxy.unregister_slash_command(command_name.clone());
1389            }
1390            for provider_id in extension.manifest.language_model_providers.keys() {
1391                let full_provider_id: Arc<str> = format!("{}:{}", extension_id, provider_id).into();
1392                self.proxy
1393                    .unregister_language_model_provider(full_provider_id, cx);
1394            }
1395        }
1396
1397        self.wasm_extensions
1398            .retain(|(extension, _)| !extensions_to_unload.contains(&extension.id));
1399        self.proxy.remove_user_themes(themes_to_remove);
1400        self.proxy.remove_icon_themes(icon_themes_to_remove);
1401        self.proxy
1402            .remove_languages(&languages_to_remove, &grammars_to_remove);
1403
1404        let mut grammars_to_add = Vec::new();
1405        let mut themes_to_add = Vec::new();
1406        let mut icon_themes_to_add = Vec::new();
1407        let mut snippets_to_add = Vec::new();
1408        for extension_id in &extensions_to_load {
1409            let Some(extension) = new_index.extensions.get(extension_id) else {
1410                continue;
1411            };
1412
1413            grammars_to_add.extend(extension.manifest.grammars.keys().map(|grammar_name| {
1414                let mut grammar_path = self.installed_dir.clone();
1415                grammar_path.extend([extension_id.as_ref(), "grammars"]);
1416                grammar_path.push(grammar_name.as_ref());
1417                grammar_path.set_extension("wasm");
1418                (grammar_name.clone(), grammar_path)
1419            }));
1420            themes_to_add.extend(extension.manifest.themes.iter().map(|theme_path| {
1421                let mut path = self.installed_dir.clone();
1422                path.extend([Path::new(extension_id.as_ref()), theme_path.as_path()]);
1423                path
1424            }));
1425            icon_themes_to_add.extend(extension.manifest.icon_themes.iter().map(
1426                |icon_theme_path| {
1427                    let mut path = self.installed_dir.clone();
1428                    path.extend([Path::new(extension_id.as_ref()), icon_theme_path.as_path()]);
1429
1430                    let mut icons_root_path = self.installed_dir.clone();
1431                    icons_root_path.extend([Path::new(extension_id.as_ref())]);
1432
1433                    (path, icons_root_path)
1434                },
1435            ));
1436            snippets_to_add.extend(extension.manifest.snippets.iter().map(|snippets_path| {
1437                let mut path = self.installed_dir.clone();
1438                path.extend([Path::new(extension_id.as_ref()), snippets_path.as_path()]);
1439                path
1440            }));
1441        }
1442
1443        self.proxy.register_grammars(grammars_to_add);
1444        let languages_to_add = new_index
1445            .languages
1446            .iter()
1447            .filter(|(_, entry)| extensions_to_load.contains(&entry.extension))
1448            .collect::<Vec<_>>();
1449        for (language_name, language) in languages_to_add {
1450            let mut language_path = self.installed_dir.clone();
1451            language_path.extend([
1452                Path::new(language.extension.as_ref()),
1453                language.path.as_path(),
1454            ]);
1455            self.proxy.register_language(
1456                language_name.clone(),
1457                language.grammar.clone(),
1458                language.matcher.clone(),
1459                language.hidden,
1460                Arc::new(move || {
1461                    let config = std::fs::read_to_string(language_path.join("config.toml"))?;
1462                    let config: LanguageConfig = ::toml::from_str(&config)?;
1463                    let queries = load_plugin_queries(&language_path);
1464                    let context_provider =
1465                        std::fs::read_to_string(language_path.join("tasks.json"))
1466                            .ok()
1467                            .and_then(|contents| {
1468                                let definitions =
1469                                    serde_json_lenient::from_str(&contents).log_err()?;
1470                                Some(Arc::new(ContextProviderWithTasks::new(definitions)) as Arc<_>)
1471                            });
1472
1473                    Ok(LoadedLanguage {
1474                        config,
1475                        queries,
1476                        context_provider,
1477                        toolchain_provider: None,
1478                        manifest_name: None,
1479                    })
1480                }),
1481            );
1482        }
1483
1484        let fs = self.fs.clone();
1485        let wasm_host = self.wasm_host.clone();
1486        let root_dir = self.installed_dir.clone();
1487        let proxy = self.proxy.clone();
1488        let extension_entries = extensions_to_load
1489            .iter()
1490            .filter_map(|name| new_index.extensions.get(name).cloned())
1491            .collect::<Vec<_>>();
1492        self.extension_index = new_index;
1493        cx.notify();
1494        cx.emit(Event::ExtensionsUpdated);
1495
1496        cx.spawn(async move |this, cx| {
1497            cx.background_spawn({
1498                let fs = fs.clone();
1499                async move {
1500                    let _ = join_all(server_removal_tasks).await;
1501                    for theme_path in themes_to_add {
1502                        proxy
1503                            .load_user_theme(theme_path, fs.clone())
1504                            .await
1505                            .log_err();
1506                    }
1507
1508                    for (icon_theme_path, icons_root_path) in icon_themes_to_add {
1509                        proxy
1510                            .load_icon_theme(icon_theme_path, icons_root_path, fs.clone())
1511                            .await
1512                            .log_err();
1513                    }
1514
1515                    for snippets_path in &snippets_to_add {
1516                        match fs
1517                            .load(snippets_path)
1518                            .await
1519                            .with_context(|| format!("Loading snippets from {snippets_path:?}"))
1520                        {
1521                            Ok(snippets_contents) => {
1522                                proxy
1523                                    .register_snippet(snippets_path, &snippets_contents)
1524                                    .log_err();
1525                            }
1526                            Err(e) => log::error!("Cannot load snippets: {e:#}"),
1527                        }
1528                    }
1529                }
1530            })
1531            .await;
1532
1533            let mut wasm_extensions: Vec<(
1534                Arc<ExtensionManifest>,
1535                WasmExtension,
1536                Vec<LlmProviderWithModels>,
1537            )> = Vec::new();
1538            for extension in extension_entries {
1539                if extension.manifest.lib.kind.is_none() {
1540                    continue;
1541                };
1542
1543                let extension_path = root_dir.join(extension.manifest.id.as_ref());
1544                let wasm_extension = WasmExtension::load(
1545                    &extension_path,
1546                    &extension.manifest,
1547                    wasm_host.clone(),
1548                    cx,
1549                )
1550                .await
1551                .with_context(|| format!("Loading extension from {extension_path:?}"));
1552
1553                match wasm_extension {
1554                    Ok(wasm_extension) => {
1555                        // Query for LLM providers if the manifest declares any
1556                        let mut llm_providers_with_models = Vec::new();
1557                        if !extension.manifest.language_model_providers.is_empty() {
1558                            let providers_result = wasm_extension
1559                                .call(|ext, store| {
1560                                    async move { ext.call_llm_providers(store).await }.boxed()
1561                                })
1562                                .await;
1563
1564                            if let Ok(Ok(providers)) = providers_result {
1565                                for provider_info in providers {
1566                                    let models_result = wasm_extension
1567                                        .call({
1568                                            let provider_id = provider_info.id.clone();
1569                                            |ext, store| {
1570                                                async move {
1571                                                    ext.call_llm_provider_models(store, &provider_id)
1572                                                        .await
1573                                                }
1574                                                .boxed()
1575                                            }
1576                                        })
1577                                        .await;
1578
1579                                    let models: Vec<LlmModelInfo> = match models_result {
1580                                        Ok(Ok(Ok(models))) => models,
1581                                        Ok(Ok(Err(e))) => {
1582                                            log::error!(
1583                                                "Failed to get models for LLM provider {} in extension {}: {}",
1584                                                provider_info.id,
1585                                                extension.manifest.id,
1586                                                e
1587                                            );
1588                                            Vec::new()
1589                                        }
1590                                        Ok(Err(e)) => {
1591                                            log::error!(
1592                                                "Wasm error calling llm_provider_models for {} in extension {}: {:?}",
1593                                                provider_info.id,
1594                                                extension.manifest.id,
1595                                                e
1596                                            );
1597                                            Vec::new()
1598                                        }
1599                                        Err(e) => {
1600                                            log::error!(
1601                                                "Extension call failed for llm_provider_models {} in extension {}: {:?}",
1602                                                provider_info.id,
1603                                                extension.manifest.id,
1604                                                e
1605                                            );
1606                                            Vec::new()
1607                                        }
1608                                    };
1609
1610                                    // Query initial authentication state
1611                                    let is_authenticated = wasm_extension
1612                                        .call({
1613                                            let provider_id = provider_info.id.clone();
1614                                            |ext, store| {
1615                                                async move {
1616                                                    ext.call_llm_provider_is_authenticated(
1617                                                        store,
1618                                                        &provider_id,
1619                                                    )
1620                                                    .await
1621                                                }
1622                                                .boxed()
1623                                            }
1624                                        })
1625                                        .await
1626                                        .unwrap_or(Ok(false))
1627                                        .unwrap_or(false);
1628
1629                                    // Resolve icon path if provided
1630                                    let icon_path = provider_info.icon.as_ref().map(|icon| {
1631                                        let icon_file_path = extension_path.join(icon);
1632                                        // Canonicalize to resolve symlinks (dev extensions are symlinked)
1633                                        let absolute_icon_path = icon_file_path
1634                                            .canonicalize()
1635                                            .unwrap_or(icon_file_path)
1636                                            .to_string_lossy()
1637                                            .to_string();
1638                                        SharedString::from(absolute_icon_path)
1639                                    });
1640
1641                                    let provider_id_arc: Arc<str> =
1642                                        provider_info.id.as_str().into();
1643                                    let auth_config = extension
1644                                        .manifest
1645                                        .language_model_providers
1646                                        .get(&provider_id_arc)
1647                                        .and_then(|entry| entry.auth.clone());
1648
1649                                    llm_providers_with_models.push(LlmProviderWithModels {
1650                                        provider_info,
1651                                        models,
1652                                        is_authenticated,
1653                                        icon_path,
1654                                        auth_config,
1655                                    });
1656                                }
1657                            } else {
1658                                log::error!(
1659                                    "Failed to get LLM providers from extension {}: {:?}",
1660                                    extension.manifest.id,
1661                                    providers_result
1662                                );
1663                            }
1664                        }
1665
1666                        wasm_extensions.push((
1667                            extension.manifest.clone(),
1668                            wasm_extension,
1669                            llm_providers_with_models,
1670                        ))
1671                    }
1672                    Err(e) => {
1673                        log::error!(
1674                            "Failed to load extension: {}, {:#}",
1675                            extension.manifest.id,
1676                            e
1677                        );
1678                        this.update(cx, |_, cx| {
1679                            cx.emit(Event::ExtensionFailedToLoad(extension.manifest.id.clone()))
1680                        })
1681                        .ok();
1682                    }
1683                }
1684            }
1685
1686            this.update(cx, |this, cx| {
1687                this.reload_complete_senders.clear();
1688
1689                for (manifest, wasm_extension, llm_providers_with_models) in &wasm_extensions {
1690                    let extension = Arc::new(wasm_extension.clone());
1691
1692                    for (language_server_id, language_server_config) in &manifest.language_servers {
1693                        for language in language_server_config.languages() {
1694                            this.proxy.register_language_server(
1695                                extension.clone(),
1696                                language_server_id.clone(),
1697                                language.clone(),
1698                            );
1699                        }
1700                    }
1701
1702                    for (slash_command_name, slash_command) in &manifest.slash_commands {
1703                        this.proxy.register_slash_command(
1704                            extension.clone(),
1705                            extension::SlashCommand {
1706                                name: slash_command_name.to_string(),
1707                                description: slash_command.description.to_string(),
1708                                // We don't currently expose this as a configurable option, as it currently drives
1709                                // the `menu_text` on the `SlashCommand` trait, which is not used for slash commands
1710                                // defined in extensions, as they are not able to be added to the menu.
1711                                tooltip_text: String::new(),
1712                                requires_argument: slash_command.requires_argument,
1713                            },
1714                        );
1715                    }
1716
1717                    for id in manifest.context_servers.keys() {
1718                        this.proxy
1719                            .register_context_server(extension.clone(), id.clone(), cx);
1720                    }
1721
1722                    for (debug_adapter, meta) in &manifest.debug_adapters {
1723                        let mut path = root_dir.clone();
1724                        path.push(Path::new(manifest.id.as_ref()));
1725                        if let Some(schema_path) = &meta.schema_path {
1726                            path.push(schema_path);
1727                        } else {
1728                            path.push("debug_adapter_schemas");
1729                            path.push(Path::new(debug_adapter.as_ref()).with_extension("json"));
1730                        }
1731
1732                        this.proxy.register_debug_adapter(
1733                            extension.clone(),
1734                            debug_adapter.clone(),
1735                            &path,
1736                        );
1737                    }
1738
1739                    for debug_adapter in manifest.debug_locators.keys() {
1740                        this.proxy
1741                            .register_debug_locator(extension.clone(), debug_adapter.clone());
1742                    }
1743
1744                    // Register LLM providers
1745                    for llm_provider in llm_providers_with_models {
1746                        let provider_id: Arc<str> =
1747                            format!("{}:{}", manifest.id, llm_provider.provider_info.id).into();
1748                        let wasm_ext = extension.as_ref().clone();
1749                        let pinfo = llm_provider.provider_info.clone();
1750                        let mods = llm_provider.models.clone();
1751                        let auth = llm_provider.is_authenticated;
1752                        let icon = llm_provider.icon_path.clone();
1753                        let auth_config = llm_provider.auth_config.clone();
1754
1755                        this.proxy.register_language_model_provider(
1756                            provider_id.clone(),
1757                            Box::new(move |cx: &mut App| {
1758                                let provider = Arc::new(ExtensionLanguageModelProvider::new(
1759                                    wasm_ext, pinfo, mods, auth, icon, auth_config, cx,
1760                                ));
1761                                language_model::LanguageModelRegistry::global(cx).update(
1762                                    cx,
1763                                    |registry, cx| {
1764                                        registry.register_provider(provider, cx);
1765                                    },
1766                                );
1767                            }),
1768                            cx,
1769                        );
1770                    }
1771                }
1772
1773                let wasm_extensions_without_llm: Vec<_> = wasm_extensions
1774                    .into_iter()
1775                    .map(|(manifest, ext, _)| (manifest, ext))
1776                    .collect();
1777                this.wasm_extensions.extend(wasm_extensions_without_llm);
1778                this.proxy.set_extensions_loaded();
1779                this.proxy.reload_current_theme(cx);
1780                this.proxy.reload_current_icon_theme(cx);
1781
1782                if let Some(events) = ExtensionEvents::try_global(cx) {
1783                    events.update(cx, |this, cx| {
1784                        this.emit(extension::Event::ExtensionsInstalledChanged, cx)
1785                    });
1786                }
1787            })
1788            .ok();
1789        })
1790    }
1791
1792    fn rebuild_extension_index(&self, cx: &mut Context<Self>) -> Task<ExtensionIndex> {
1793        let fs = self.fs.clone();
1794        let work_dir = self.wasm_host.work_dir.clone();
1795        let extensions_dir = self.installed_dir.clone();
1796        let index_path = self.index_path.clone();
1797        let proxy = self.proxy.clone();
1798        cx.background_spawn(async move {
1799            let mut index = ExtensionIndex::default();
1800
1801            fs.create_dir(&work_dir).await.log_err();
1802            fs.create_dir(&extensions_dir).await.log_err();
1803
1804            let extension_paths = fs.read_dir(&extensions_dir).await;
1805            if let Ok(mut extension_paths) = extension_paths {
1806                while let Some(extension_dir) = extension_paths.next().await {
1807                    let Ok(extension_dir) = extension_dir else {
1808                        continue;
1809                    };
1810
1811                    if extension_dir
1812                        .file_name()
1813                        .is_some_and(|file_name| file_name == ".DS_Store")
1814                    {
1815                        continue;
1816                    }
1817
1818                    Self::add_extension_to_index(
1819                        fs.clone(),
1820                        extension_dir,
1821                        &mut index,
1822                        proxy.clone(),
1823                    )
1824                    .await
1825                    .log_err();
1826                }
1827            }
1828
1829            if let Ok(index_json) = serde_json::to_string_pretty(&index) {
1830                fs.save(&index_path, &index_json.as_str().into(), Default::default())
1831                    .await
1832                    .context("failed to save extension index")
1833                    .log_err();
1834            }
1835
1836            index
1837        })
1838    }
1839
1840    async fn add_extension_to_index(
1841        fs: Arc<dyn Fs>,
1842        extension_dir: PathBuf,
1843        index: &mut ExtensionIndex,
1844        proxy: Arc<ExtensionHostProxy>,
1845    ) -> Result<()> {
1846        let mut extension_manifest = ExtensionManifest::load(fs.clone(), &extension_dir).await?;
1847        let extension_id = extension_manifest.id.clone();
1848
1849        if SUPPRESSED_EXTENSIONS.contains(&extension_id.as_ref()) {
1850            return Ok(());
1851        }
1852
1853        // TODO: distinguish dev extensions more explicitly, by the absence
1854        // of a checksum file that we'll create when downloading normal extensions.
1855        let is_dev = fs
1856            .metadata(&extension_dir)
1857            .await?
1858            .context("directory does not exist")?
1859            .is_symlink;
1860
1861        if let Ok(mut language_paths) = fs.read_dir(&extension_dir.join("languages")).await {
1862            while let Some(language_path) = language_paths.next().await {
1863                let language_path = language_path?;
1864                let Ok(relative_path) = language_path.strip_prefix(&extension_dir) else {
1865                    continue;
1866                };
1867                let Ok(Some(fs_metadata)) = fs.metadata(&language_path).await else {
1868                    continue;
1869                };
1870                if !fs_metadata.is_dir {
1871                    continue;
1872                }
1873                let config = fs.load(&language_path.join("config.toml")).await?;
1874                let config = ::toml::from_str::<LanguageConfig>(&config)?;
1875
1876                let relative_path = relative_path.to_path_buf();
1877                if !extension_manifest.languages.contains(&relative_path) {
1878                    extension_manifest.languages.push(relative_path.clone());
1879                }
1880
1881                index.languages.insert(
1882                    config.name.clone(),
1883                    ExtensionIndexLanguageEntry {
1884                        extension: extension_id.clone(),
1885                        path: relative_path,
1886                        matcher: config.matcher,
1887                        hidden: config.hidden,
1888                        grammar: config.grammar,
1889                    },
1890                );
1891            }
1892        }
1893
1894        if let Ok(mut theme_paths) = fs.read_dir(&extension_dir.join("themes")).await {
1895            while let Some(theme_path) = theme_paths.next().await {
1896                let theme_path = theme_path?;
1897                let Ok(relative_path) = theme_path.strip_prefix(&extension_dir) else {
1898                    continue;
1899                };
1900
1901                let Some(theme_families) = proxy
1902                    .list_theme_names(theme_path.clone(), fs.clone())
1903                    .await
1904                    .log_err()
1905                else {
1906                    continue;
1907                };
1908
1909                let relative_path = relative_path.to_path_buf();
1910                if !extension_manifest.themes.contains(&relative_path) {
1911                    extension_manifest.themes.push(relative_path.clone());
1912                }
1913
1914                for theme_name in theme_families {
1915                    index.themes.insert(
1916                        theme_name.into(),
1917                        ExtensionIndexThemeEntry {
1918                            extension: extension_id.clone(),
1919                            path: relative_path.clone(),
1920                        },
1921                    );
1922                }
1923            }
1924        }
1925
1926        if let Ok(mut icon_theme_paths) = fs.read_dir(&extension_dir.join("icon_themes")).await {
1927            while let Some(icon_theme_path) = icon_theme_paths.next().await {
1928                let icon_theme_path = icon_theme_path?;
1929                let Ok(relative_path) = icon_theme_path.strip_prefix(&extension_dir) else {
1930                    continue;
1931                };
1932
1933                let Some(icon_theme_families) = proxy
1934                    .list_icon_theme_names(icon_theme_path.clone(), fs.clone())
1935                    .await
1936                    .log_err()
1937                else {
1938                    continue;
1939                };
1940
1941                let relative_path = relative_path.to_path_buf();
1942                if !extension_manifest.icon_themes.contains(&relative_path) {
1943                    extension_manifest.icon_themes.push(relative_path.clone());
1944                }
1945
1946                for icon_theme_name in icon_theme_families {
1947                    index.icon_themes.insert(
1948                        icon_theme_name.into(),
1949                        ExtensionIndexIconThemeEntry {
1950                            extension: extension_id.clone(),
1951                            path: relative_path.clone(),
1952                        },
1953                    );
1954                }
1955            }
1956        }
1957
1958        let extension_wasm_path = extension_dir.join("extension.wasm");
1959        if fs.is_file(&extension_wasm_path).await {
1960            extension_manifest
1961                .lib
1962                .kind
1963                .get_or_insert(ExtensionLibraryKind::Rust);
1964        }
1965
1966        index.extensions.insert(
1967            extension_id.clone(),
1968            ExtensionIndexEntry {
1969                dev: is_dev,
1970                manifest: Arc::new(extension_manifest),
1971            },
1972        );
1973
1974        Ok(())
1975    }
1976
1977    fn prepare_remote_extension(
1978        &mut self,
1979        extension_id: Arc<str>,
1980        is_dev: bool,
1981        tmp_dir: PathBuf,
1982        cx: &mut Context<Self>,
1983    ) -> Task<Result<()>> {
1984        let src_dir = self.extensions_dir().join(extension_id.as_ref());
1985        let Some(loaded_extension) = self.extension_index.extensions.get(&extension_id).cloned()
1986        else {
1987            return Task::ready(Err(anyhow!("extension no longer installed")));
1988        };
1989        let fs = self.fs.clone();
1990        cx.background_spawn(async move {
1991            const EXTENSION_TOML: &str = "extension.toml";
1992            const EXTENSION_WASM: &str = "extension.wasm";
1993            const CONFIG_TOML: &str = "config.toml";
1994
1995            if is_dev {
1996                let manifest_toml = toml::to_string(&loaded_extension.manifest)?;
1997                fs.save(
1998                    &tmp_dir.join(EXTENSION_TOML),
1999                    &Rope::from(manifest_toml),
2000                    language::LineEnding::Unix,
2001                )
2002                .await?;
2003            } else {
2004                fs.copy_file(
2005                    &src_dir.join(EXTENSION_TOML),
2006                    &tmp_dir.join(EXTENSION_TOML),
2007                    fs::CopyOptions::default(),
2008                )
2009                .await?
2010            }
2011
2012            if fs.is_file(&src_dir.join(EXTENSION_WASM)).await {
2013                fs.copy_file(
2014                    &src_dir.join(EXTENSION_WASM),
2015                    &tmp_dir.join(EXTENSION_WASM),
2016                    fs::CopyOptions::default(),
2017                )
2018                .await?
2019            }
2020
2021            for language_path in loaded_extension.manifest.languages.iter() {
2022                if fs
2023                    .is_file(&src_dir.join(language_path).join(CONFIG_TOML))
2024                    .await
2025                {
2026                    fs.create_dir(&tmp_dir.join(language_path)).await?;
2027                    fs.copy_file(
2028                        &src_dir.join(language_path).join(CONFIG_TOML),
2029                        &tmp_dir.join(language_path).join(CONFIG_TOML),
2030                        fs::CopyOptions::default(),
2031                    )
2032                    .await?
2033                }
2034            }
2035
2036            for (adapter_name, meta) in loaded_extension.manifest.debug_adapters.iter() {
2037                let schema_path = &extension::build_debug_adapter_schema_path(adapter_name, meta);
2038
2039                if fs.is_file(&src_dir.join(schema_path)).await {
2040                    if let Some(parent) = schema_path.parent() {
2041                        fs.create_dir(&tmp_dir.join(parent)).await?
2042                    }
2043                    fs.copy_file(
2044                        &src_dir.join(schema_path),
2045                        &tmp_dir.join(schema_path),
2046                        fs::CopyOptions::default(),
2047                    )
2048                    .await?
2049                }
2050            }
2051
2052            Ok(())
2053        })
2054    }
2055
2056    async fn sync_extensions_to_remotes(
2057        this: &WeakEntity<Self>,
2058        client: WeakEntity<RemoteClient>,
2059        cx: &mut AsyncApp,
2060    ) -> Result<()> {
2061        let extensions = this.update(cx, |this, _cx| {
2062            this.extension_index
2063                .extensions
2064                .iter()
2065                .filter_map(|(id, entry)| {
2066                    if !entry.manifest.allow_remote_load() {
2067                        return None;
2068                    }
2069                    Some(proto::Extension {
2070                        id: id.to_string(),
2071                        version: entry.manifest.version.to_string(),
2072                        dev: entry.dev,
2073                    })
2074                })
2075                .collect()
2076        })?;
2077
2078        let response = client
2079            .update(cx, |client, _cx| {
2080                client
2081                    .proto_client()
2082                    .request(proto::SyncExtensions { extensions })
2083            })?
2084            .await?;
2085        let path_style = client.read_with(cx, |client, _| client.path_style())?;
2086
2087        for missing_extension in response.missing_extensions.into_iter() {
2088            let tmp_dir = tempfile::tempdir()?;
2089            this.update(cx, |this, cx| {
2090                this.prepare_remote_extension(
2091                    missing_extension.id.clone().into(),
2092                    missing_extension.dev,
2093                    tmp_dir.path().to_owned(),
2094                    cx,
2095                )
2096            })?
2097            .await?;
2098            let dest_dir = RemotePathBuf::new(
2099                path_style
2100                    .join(&response.tmp_dir, &missing_extension.id)
2101                    .with_context(|| {
2102                        format!(
2103                            "failed to construct destination path: {:?}, {:?}",
2104                            response.tmp_dir, missing_extension.id,
2105                        )
2106                    })?,
2107                path_style,
2108            );
2109
2110            client
2111                .update(cx, |client, cx| {
2112                    client.upload_directory(tmp_dir.path().to_owned(), dest_dir.clone(), cx)
2113                })?
2114                .await?;
2115
2116            let result = client
2117                .update(cx, |client, _cx| {
2118                    client.proto_client().request(proto::InstallExtension {
2119                        tmp_dir: dest_dir.to_proto(),
2120                        extension: Some(missing_extension.clone()),
2121                    })
2122                })?
2123                .await;
2124
2125            if let Err(e) = result {
2126                log::error!(
2127                    "Failed to install extension {}: {}",
2128                    missing_extension.id,
2129                    e
2130                );
2131            }
2132        }
2133
2134        anyhow::Ok(())
2135    }
2136
2137    pub async fn update_remote_clients(this: &WeakEntity<Self>, cx: &mut AsyncApp) -> Result<()> {
2138        let clients = this.update(cx, |this, _cx| {
2139            this.remote_clients.retain(|v| v.upgrade().is_some());
2140            this.remote_clients.clone()
2141        })?;
2142
2143        for client in clients {
2144            Self::sync_extensions_to_remotes(this, client, cx)
2145                .await
2146                .log_err();
2147        }
2148
2149        anyhow::Ok(())
2150    }
2151
2152    pub fn register_remote_client(
2153        &mut self,
2154        client: Entity<RemoteClient>,
2155        _cx: &mut Context<Self>,
2156    ) {
2157        self.remote_clients.push(client.downgrade());
2158        self.ssh_registered_tx.unbounded_send(()).ok();
2159    }
2160}
2161
2162fn load_plugin_queries(root_path: &Path) -> LanguageQueries {
2163    let mut result = LanguageQueries::default();
2164    if let Some(entries) = std::fs::read_dir(root_path).log_err() {
2165        for entry in entries {
2166            let Some(entry) = entry.log_err() else {
2167                continue;
2168            };
2169            let path = entry.path();
2170            if let Some(remainder) = path.strip_prefix(root_path).ok().and_then(|p| p.to_str()) {
2171                if !remainder.ends_with(".scm") {
2172                    continue;
2173                }
2174                for (name, query) in QUERY_FILENAME_PREFIXES {
2175                    if remainder.starts_with(name) {
2176                        if let Some(contents) = std::fs::read_to_string(&path).log_err() {
2177                            match query(&mut result) {
2178                                None => *query(&mut result) = Some(contents.into()),
2179                                Some(r) => r.to_mut().push_str(contents.as_ref()),
2180                            }
2181                        }
2182                        break;
2183                    }
2184                }
2185            }
2186        }
2187    }
2188    result
2189}