Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): remove a failed legacy-directory package on uninstall
Yudhi Armyndharis06ca3b2b68e1
diff --git a/src/core/plugins/plugin-uninstaller.spec.ts b/src/core/plugins/plugin-uninstaller.spec.tsindex f90567780..69f1cb0c9 100644--- a/src/core/plugins/plugin-uninstaller.spec.ts+++ b/src/core/plugins/plugin-uninstaller.spec.ts@@ -98,6 +98,34 @@ describe('PluginUninstaller — uninstall', () => {expect(plugins.get('user-plg')).toBeUndefined();});+ it('removes a legacy-directory package that failed to load (registry entry only)', async () => {+ // A load failure keeps the registry entry but records no runtime instance, so no packageDir either.+ const legacyDir = path.join(tmpDir, 'legacy-plugins');+ const pkg = path.join(legacyDir, 'old-plg');+ fs.mkdirSync(pkg, { recursive: true });+ fs.writeFileSync(path.join(pkg, 'manifest.json'), '{}');+ const stray = path.join(legacyDir, 'not-a-plugin');+ fs.mkdirSync(stray, { recursive: true });+ seedEntry(storage, 'old-plg');+ seedEntry(storage, 'not-a-plugin');+ const withLegacy = new PluginUninstaller(+ createLogger('PluginLoaderService'),+ plugins,+ storage,+ pluginsDir,+ () => Promise.resolve(),+ legacyDir,+ );++ await withLegacy.uninstallPlugin('old-plg');+ await withLegacy.uninstallPlugin('not-a-plugin');++ expect(fs.existsSync(pkg)).toBe(false);+ expect(storage.getPluginEntry('old-plg')).toBeUndefined();+ // Only a real package directory (one holding a manifest.json) is removed from the legacy tree.+ expect(fs.existsSync(stray)).toBe(true);+ });+it('refuses to uninstall a built-in plugin', async () => {plugins.set('core-engine', { ...loadedUserPlugin('core-engine', pluginsDir), builtIn: true });seedEntry(storage, 'core-engine', true);diff --git a/src/core/plugins/plugin-uninstaller.ts b/src/core/plugins/plugin-uninstaller.tsindex cfc74b7c5..5917d4f23 100644--- a/src/core/plugins/plugin-uninstaller.ts+++ b/src/core/plugins/plugin-uninstaller.ts@@ -29,11 +29,13 @@ export class PluginUninstaller {private readonly pluginsDir: string,/** The lifecycle's unload — passed as a callback so this file never imports the lifecycle back. */private readonly unload: (pluginId: string) => Promise<void>,+ /** The legacy plugin tree the boot scan also loads from, or null when PLUGINS_DIR is set. */+ private readonly legacyPluginsDir: string | null = null,) {}- /** <plugins.dir>/<id> for an id with no loaded package, or null if the id escapes that root. */- private resolveUninstallDir(pluginId: string): string | null {- const base = path.resolve(this.pluginsDir);+ /** <root>/<id> for an id with no loaded package, or null if the id escapes that root. */+ private resolveUninstallDir(root: string, pluginId: string): string | null {+ const base = path.resolve(root);const dir = path.resolve(base, pluginId);return dir !== base && dir.startsWith(base + path.sep) ? dir : null;}@@ -61,10 +63,19 @@ export class PluginUninstaller {// A recorded directory came from the boot scan and is already contained. Without one the id is// the only input, so it stays behind the traversal guard against the configured root.- const dir = recordedDir ?? this.resolveUninstallDir(pluginId);+ const dir = recordedDir ?? this.resolveUninstallDir(this.pluginsDir, pluginId);if (dir && fs.existsSync(dir)) {fs.rmSync(dir, { recursive: true, force: true });}+ // No recorded directory also covers a plugin that failed to load: its registry entry survives+ // the boot scan but no runtime record does. Its package may sit in the legacy tree, which the+ // scan would load again (as a new plugin, config lost) once the failure cause goes away.+ if (!recordedDir && this.legacyPluginsDir) {+ const legacyDir = this.resolveUninstallDir(this.legacyPluginsDir, pluginId);+ if (legacyDir && fs.existsSync(path.join(legacyDir, 'manifest.json'))) {+ fs.rmSync(legacyDir, { recursive: true, force: true });+ }+ }// Drop the plugin's ctx.storage data dir. Under shipped defaults it lives INSIDE the package// dir (already gone above), but a split-dir deployment (PLUGINS_DIR outside the data dir) would