diff --git a/builtin-extensions/serverkit-walkthrough-studio/frontend/index.jsx b/builtin-extensions/serverkit-walkthrough-studio/frontend/index.jsx index 2c47298f..b11b9300 100644 --- a/builtin-extensions/serverkit-walkthrough-studio/frontend/index.jsx +++ b/builtin-extensions/serverkit-walkthrough-studio/frontend/index.jsx @@ -551,4 +551,7 @@ export function WalkthroughStudioPage() { ); } -export default WalkthroughStudioPage; +// No default export on purpose: the route contribution resolves the named +// WalkthroughStudioPage export, and a default export would make the legacy +// auto-render in PluginLoader mount this whole page globally on panels +// where the extension is not installed. diff --git a/frontend/src/plugins/PluginLoader.jsx b/frontend/src/plugins/PluginLoader.jsx index 60500dbe..59e9c69b 100644 --- a/frontend/src/plugins/PluginLoader.jsx +++ b/frontend/src/plugins/PluginLoader.jsx @@ -25,7 +25,12 @@ * stacking several pages into one view. */ import { useMemo } from 'react'; -import { useContributions, resolveComponent, getPluginModule } from './contributions'; +import { + useContributions, + resolveComponent, + getPluginModule, + declaresBakedContributions, +} from './contributions'; const pluginModules = import.meta.glob('./*/index.{js,jsx}', { eager: true }); @@ -84,8 +89,15 @@ const PluginLoader = ({ api }) => { }) .filter(Boolean); + // A plugin qualifies for the legacy auto-render only when neither the + // runtime envelope NOR its baked manifest declares contributions. The + // baked check matters for extensions bundled into the build but not + // installed on this panel: they contribute nothing at runtime, yet + // their module (and default export) is still in the bundle. const legacyWidgets = legacyPlugins - .filter(({ slug, Component }) => Component && !slugsWithDeclared.has(slug)) + .filter(({ slug, Component }) => Component + && !slugsWithDeclared.has(slug) + && !declaresBakedContributions(slug)) .map(({ slug, Component, Provider }) => { const node = ; return Provider diff --git a/frontend/src/plugins/__tests__/bakedPluginRendering.test.mjs b/frontend/src/plugins/__tests__/bakedPluginRendering.test.mjs new file mode 100644 index 00000000..53300b5d --- /dev/null +++ b/frontend/src/plugins/__tests__/bakedPluginRendering.test.mjs @@ -0,0 +1,104 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { readdirSync, readFileSync, existsSync, statSync } from 'node:fs'; +import { join, dirname } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +// Baked plugin rendering contract. +// +// PluginLoader has a legacy auto-render: any baked plugin module with a +// default export and NO contributions gets that export rendered globally +// on every route. A baked extension whose manifest declares contributions +// owns its rendering through the contribution model instead — installed +// or not — because its module ships in the bundle either way. +// +// Regression pinned here: serverkit-walkthrough-studio was baked with a +// `export default WalkthroughStudioPage` while not installed on a panel, +// so the legacy auto-render stacked the whole Studio page on top of the +// dashboard. Two independent guards prevent a recurrence: +// +// 1. PluginLoader excludes plugins whose BAKED manifest declares +// contributions (declaresBakedContributions). +// 2. This test: a baked plugin that declares route contributions must +// not also carry a default export in its index module — routes +// resolve named exports, and the default only feeds the legacy +// auto-render footgun. + +const HERE = dirname(fileURLToPath(import.meta.url)); +const PLUGINS_DIR = join(HERE, '..'); + +function bakedPluginDirs() { + return readdirSync(PLUGINS_DIR).filter((name) => { + const dir = join(PLUGINS_DIR, name); + if (!statSync(dir).isDirectory()) return false; + if (name === 'sdk' || name === 'runtime' || name === '__tests__') return false; + return existsSync(join(dir, 'plugin.json')); + }); +} + +function indexSource(slug) { + for (const file of ['index.jsx', 'index.js']) { + const path = join(PLUGINS_DIR, slug, file); + if (existsSync(path)) return readFileSync(path, 'utf8'); + } + return null; +} + +function declaresContributions(manifest) { + const contrib = manifest?.contributions; + if (!contrib || typeof contrib !== 'object') return false; + return Object.values(contrib).some((value) => ( + Array.isArray(value) + ? value.length > 0 + : !!value && typeof value === 'object' && Object.keys(value).length > 0 + )); +} + +test('baked plugins are discoverable', () => { + const dirs = bakedPluginDirs(); + assert.ok(dirs.length > 0, 'expected at least one baked plugin directory'); + assert.ok( + dirs.includes('serverkit-walkthrough-studio'), + 'expected the walkthrough studio to be among baked plugins ' + + '(if it was unbaked on purpose, update this test)', + ); +}); + +test('a baked plugin with route contributions has no default export', () => { + for (const slug of bakedPluginDirs()) { + const manifest = JSON.parse( + readFileSync(join(PLUGINS_DIR, slug, 'plugin.json'), 'utf8'), + ); + const routes = manifest?.contributions?.routes || []; + if (!routes.length) continue; + + const source = indexSource(slug); + assert.ok(source, `${slug}: baked plugin has no index module`); + assert.ok( + !/^\s*export\s+default\s/m.test(source), + `${slug}: declares route contributions AND a default export — ` + + 'the legacy auto-render in PluginLoader would mount that export ' + + 'globally on panels where the extension is not installed. ' + + 'Export the page under a name and point the route at it.', + ); + } +}); + +test('PluginLoader gates the legacy auto-render on baked manifests', () => { + const loader = readFileSync(join(PLUGINS_DIR, 'PluginLoader.jsx'), 'utf8'); + assert.match( + loader, + /declaresBakedContributions\(slug\)/, + 'PluginLoader must exclude plugins whose baked manifest declares ' + + 'contributions from the legacy auto-render', + ); +}); + +test('every baked plugin declaring contributions keeps them out of the legacy path', () => { + // The studio specifically: manifest declares contributions, so guard 1 + // (declaresBakedContributions) must classify it as contribution-owned. + const manifest = JSON.parse(readFileSync( + join(PLUGINS_DIR, 'serverkit-walkthrough-studio', 'plugin.json'), 'utf8', + )); + assert.equal(declaresContributions(manifest), true); +}); diff --git a/frontend/src/plugins/contributions.js b/frontend/src/plugins/contributions.js index 1b5ad4a1..0208b11d 100644 --- a/frontend/src/plugins/contributions.js +++ b/frontend/src/plugins/contributions.js @@ -58,6 +58,23 @@ export function getPluginModule(slug) { return moduleBySlug[slug] || getRuntimeModule(slug) || null; } +// True iff the BAKED manifest for this slug declares any contribution. +// Such a plugin owns its rendering through the contribution model even +// when it is not installed on this panel — its module is still in the +// bundle (eager glob), so the legacy auto-render must never pick it up. +// Without this gate, a baked-but-uninstalled extension whose index module +// had a default export mounted its whole page globally on every route +// (the Walkthrough Studio page stacked on top of the dashboard). +export function declaresBakedContributions(slug) { + const contrib = localManifestBySlug[slug]?.contributions; + if (!contrib || typeof contrib !== 'object') return false; + return Object.values(contrib).some((value) => ( + Array.isArray(value) + ? value.length > 0 + : !!value && typeof value === 'object' && Object.keys(value).length > 0 + )); +} + // Resolve a contribution's `component` string to an actual React component. // `component` may be: // - "default" → mod.default diff --git a/frontend/src/plugins/serverkit-walkthrough-studio/index.jsx b/frontend/src/plugins/serverkit-walkthrough-studio/index.jsx index 2c47298f..b11b9300 100644 --- a/frontend/src/plugins/serverkit-walkthrough-studio/index.jsx +++ b/frontend/src/plugins/serverkit-walkthrough-studio/index.jsx @@ -551,4 +551,7 @@ export function WalkthroughStudioPage() { ); } -export default WalkthroughStudioPage; +// No default export on purpose: the route contribution resolves the named +// WalkthroughStudioPage export, and a default export would make the legacy +// auto-render in PluginLoader mount this whole page globally on panels +// where the extension is not installed.