From 7878ba01fd3091fa634587580541fb6147630f49 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Mon, 30 Nov 2026 21:16:19 +0800 Subject: [PATCH] fix(plugins): activate plugins whose activation event fired before they loaded A remote project's server receives the desktop's plugin packages after the window has opened its files, so the on-language:twig event reached the server before the Twig plugin was installed there and was lost: the plugin stayed inactive and Twig files got no highlighting or completion. The same happened locally when a plugin was installed with a matching file already open. The host now remembers the language, file and view activation events fired so far (bounded) and activates a plugin that loads later if one of them matches. The window also reports on-language again when the open file's language changes (the server redetects it once it has the plugin's language). Fixes #7014 --- crates/ide-host/src/plugins/mod.rs | 26 ++++++++++++++++-- crates/ide-host/tests/plugins.rs | 22 +++++++++++++++ .../__tests__/activationEvents.test.ts | 27 +++++++++++++++++++ src/features/plugins/bridge.ts | 7 +++-- 4 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 src/features/plugins/__tests__/activationEvents.test.ts diff --git a/crates/ide-host/src/plugins/mod.rs b/crates/ide-host/src/plugins/mod.rs index 3b32ca3ca7..01fdec4646 100644 --- a/crates/ide-host/src/plugins/mod.rs +++ b/crates/ide-host/src/plugins/mod.rs @@ -422,6 +422,10 @@ pub struct PluginHost { initial_load: (Mutex, Condvar), /// `on-startup-finished` has fired. startup_finished: AtomicBool, + /// Language, file and view activation events fired so far (with their project), so a plugin + /// installed or synced afterwards (a remote server gets its packages after the window opened + /// files) still activates for a Twig file that is already open. + fired_activations: Mutex)>>, /// The theme last announced with `ThemeChanged`. announced_theme: Mutex>, /// [`PluginLanguages::revision`] last announced with `LanguagesChanged`. @@ -541,6 +545,7 @@ impl PluginHost { lifecycle: Mutex::new(()), initial_load: (Mutex::new(false), Condvar::new()), startup_finished: AtomicBool::new(false), + fired_activations: Mutex::new(Vec::new()), announced_theme: Mutex::new(None), announced_languages: AtomicU64::new(0), announced_skills: AtomicU64::new(0), @@ -699,6 +704,16 @@ impl PluginHost { /// Fires an activation event from the UI (`on-language:rust`, `on-file:…`, `on-view:…`). pub fn activation_event(&self, event: &ActivationEvent, project: Option<&Path>) { + { + let mut fired = self.fired_activations.lock(); + let entry = (event.clone(), project.map(Path::to_path_buf)); + if !fired.contains(&entry) { + if fired.len() >= MAX_FIRED_ACTIVATIONS { + fired.remove(0); + } + fired.push(entry); + } + } let targets: Vec<_> = self .plugins .read() @@ -3096,8 +3111,12 @@ impl PluginHost { meta.pending = pending; meta.status = if plugin.manifest().wasm.is_some() { PluginStatus::Inactive } else { PluginStatus::Static }; } - let eager = plugin.manifest().activation.contains(&ActivationEvent::StartupFinished) - && self.startup_finished.load(Ordering::Acquire); + let eager = (plugin.manifest().activation.contains(&ActivationEvent::StartupFinished) + && self.startup_finished.load(Ordering::Acquire)) + || self.fired_activations.lock().iter().any(|(event, project)| { + (plugin.dev_root.is_none() || plugin.dev_root == *project) + && plugin.manifest().activation.iter().any(|a| activation_matches(a, event)) + }); if eager { let _ = self.ensure_active(plugin, None); } @@ -3725,6 +3744,9 @@ fn counts(m: &Manifest) -> Vec { m.declaration_counts().into_iter().map(|(point, count)| ContributionCount { point, count }).collect() } +/// How many fired activation events [`PluginHost::activation_event`] remembers (oldest dropped first). +const MAX_FIRED_ACTIVATIONS: usize = 512; + fn activation_matches(declared: &ActivationEvent, fired: &ActivationEvent) -> bool { match (declared, fired) { (ActivationEvent::Language(a), ActivationEvent::Language(b)) => a == b || a == "*", diff --git a/crates/ide-host/tests/plugins.rs b/crates/ide-host/tests/plugins.rs index e18e600aa7..62dfea0642 100644 --- a/crates/ide-host/tests/plugins.rs +++ b/crates/ide-host/tests/plugins.rs @@ -1590,6 +1590,28 @@ fn misbehave_with_language(dir: &Path) -> PathBuf { pkg } +/// A language activation fired before the plugin was there (a remote server gets the desktop's +/// packages after the window opened its files; a plugin installed with such a file open) +/// activates it once it loads; one that never fired doesn't. +#[test] +fn plugins_loaded_after_their_activation_event_fired_activate() { + use ide_extension::ActivationEvent; + let tmp = tempfile::tempdir().unwrap(); + let host = Arc::new( + PluginHost::new(tmp.path().join("plugins"), Arc::new(|_: Option<&str>, _| {}), Limits::default()).unwrap(), + ); + host.load_all(); + host.startup_finished(); + host.activation_event(&ActivationEvent::Language("other".into()), None); + host.install_from_dir(&misbehave_with_language(tmp.path()), &Consent::all()).unwrap(); + assert_eq!(host.info_of("misbehave").unwrap().status, PluginStatus::Inactive); + host.uninstall("misbehave").unwrap(); + + host.activation_event(&ActivationEvent::Language("mis".into()), None); + host.install_from_dir(&misbehave_with_language(tmp.path()), &Consent::all()).unwrap(); + assert!(eventually(|| host.info_of("misbehave").is_ok_and(|i| i.status == PluginStatus::Active))); +} + /// Edit-path calls stop waiting at the soft deadline (the result is /// dropped, the instance kept) and are interrupted at the hard one. #[test] diff --git a/src/features/plugins/__tests__/activationEvents.test.ts b/src/features/plugins/__tests__/activationEvents.test.ts new file mode 100644 index 0000000000..08b5c4414f --- /dev/null +++ b/src/features/plugins/__tests__/activationEvents.test.ts @@ -0,0 +1,27 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { useEditorRegistry } from "@/editor/api"; +import { createMemoryPluginsBackend, setPluginsBackend } from "../backend"; +import { trackActivationEvents } from "../bridge"; + +afterEach(() => { + setPluginsBackend(null); + useEditorRegistry.setState({ active: null }); +}); + +describe("activation events", () => { + it("fires on-language when the open file's language is recognized later", () => { + const backend = createMemoryPluginsBackend(); + const activationEvent = vi.spyOn(backend, "activationEvent"); + setPluginsBackend(backend); + const ctx = (language: string) => ({ active: { ctx: { path: "/p/page.twig", language } } }) as never; + useEditorRegistry.setState(ctx("plaintext")); + const stop = trackActivationEvents(); + // A remote server redetects the file once it has the plugin's language. + useEditorRegistry.setState(ctx("twig")); + stop(); + expect(activationEvent.mock.calls.filter(([kind]) => kind === "language").map(([, arg]) => arg)).toEqual([ + "plaintext", + "twig", + ]); + }); +}); diff --git a/src/features/plugins/bridge.ts b/src/features/plugins/bridge.ts index adab500d9a..53e842a217 100644 --- a/src/features/plugins/bridge.ts +++ b/src/features/plugins/bridge.ts @@ -130,7 +130,7 @@ function scheduleRefresh() { * Fires `on-language:` and `on-file:` for files * opened in the editor; the backend activates the plugins waiting for them. */ -function trackActivationEvents(): () => void { +export function trackActivationEvents(): () => void { const seenLanguages = new Set(); const report = (ctx: { path: string | null; language: string } | undefined) => { if (!ctx?.path) return; @@ -148,8 +148,11 @@ function trackActivationEvents(): () => void { } }; report(useEditorRegistry.getState().active?.ctx); + // Also when the open file's language changes: a remote project learns plugin languages after the + // window restored its tabs, so a `.twig` file is plain text until the server redetects it. return useEditorRegistry.subscribe((s, prev) => { - if (s.active?.ctx.path !== prev.active?.ctx.path) report(s.active?.ctx); + if (s.active?.ctx.path !== prev.active?.ctx.path || s.active?.ctx.language !== prev.active?.ctx.language) + report(s.active?.ctx); }); }