Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(plugins): remove the legacy copy of a plugin on uninstall
Yudhi Armyndharis3715311ec7ea
diff --git a/src/core/plugins/plugin-uninstaller.spec.ts b/src/core/plugins/plugin-uninstaller.spec.tsindex 69f1cb0c9..add302653 100644--- a/src/core/plugins/plugin-uninstaller.spec.ts+++ b/src/core/plugins/plugin-uninstaller.spec.ts@@ -126,6 +126,34 @@ describe('PluginUninstaller — uninstall', () => {expect(fs.existsSync(stray)).toBe(true);});+ it('also removes a legacy copy of a plugin that loaded from the configured tree', async () => {+ // The boot scan skips the legacy copy as a duplicate, so the runtime record points at the configured+ // copy. Left behind, the legacy copy would load again on the next boot as a new plugin.+ const dir = writeUserPlugin('dup-plg');+ const legacyDir = path.join(tmpDir, 'legacy-plugins');+ const legacyPkg = path.join(legacyDir, 'dup-plg');+ fs.mkdirSync(legacyPkg, { recursive: true });+ fs.writeFileSync(path.join(legacyPkg, 'manifest.json'), '{}');+ plugins.set('dup-plg', loadedUserPlugin('dup-plg', dir));+ seedEntry(storage, 'dup-plg');+ const withLegacy = new PluginUninstaller(+ createLogger('PluginLoaderService'),+ plugins,+ storage,+ pluginsDir,+ pluginId => {+ plugins.delete(pluginId);+ return Promise.resolve();+ },+ legacyDir,+ );++ await withLegacy.uninstallPlugin('dup-plg');++ expect(fs.existsSync(dir)).toBe(false);+ expect(fs.existsSync(legacyPkg)).toBe(false);+ });+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 5917d4f23..12a6c13bc 100644--- a/src/core/plugins/plugin-uninstaller.ts+++ b/src/core/plugins/plugin-uninstaller.ts@@ -67,10 +67,12 @@ export class PluginUninstaller {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) {+ // The legacy tree can still hold a copy the scan would load again on the next boot (as a new+ // plugin, config lost): the package of a plugin that failed to load (its registry entry survives+ // the boot scan but no runtime record does), or a copy the scan skipped as a duplicate because+ // the same id loaded from the configured tree. When the recorded dir was the legacy copy it is+ // already gone above, and this is a no-op.+ if (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 });