Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): keep the package folder across a failed uninstall

Yudhi Armyndharis6ee1f8aeb3e8
diff --git a/src/core/plugins/plugin-uninstaller.spec.ts b/src/core/plugins/plugin-uninstaller.spec.ts
index ae7bee8c0..2f4ca7c28 100644
--- a/src/core/plugins/plugin-uninstaller.spec.ts
+++ b/src/core/plugins/plugin-uninstaller.spec.ts
@@ -212,6 +212,30 @@ describe('PluginUninstaller — uninstall', () => {
expect(fs.existsSync(dir)).toBe(false);
expect(storage.getPluginEntry('relay')).toBeUndefined();
});
+
+ // The boot scan loads a folder by its manifest id, whatever the folder is called. The repeat of a
+ // failed uninstall has no runtime record left to read the folder from.
+ it('removes the package folder on the repeat of a failed uninstall when the folder is not the plugin id', async () => {
+ const dir = path.join(pluginsDir, 'relay-v2-copy');
+ fs.mkdirSync(dir, { recursive: true });
+ fs.writeFileSync(path.join(dir, 'manifest.json'), JSON.stringify({ id: 'relay' }));
+ seedEntry(storage, 'relay');
+ const config = { get: (k: string) => (k === 'plugins.dir' ? pluginsDir : tmpDir) } as unknown as ConfigService;
+ let storeUp = false;
+ const flaky = makeUninstaller(config, storage, pluginsDir, () =>
+ storeUp ? Promise.resolve() : Promise.reject(new Error('mapping store unavailable')),
+ );
+ flaky.plugins.set('relay', loadedUserPlugin('relay', dir));
+
+ await expect(flaky.uninstaller.uninstallPlugin('relay')).rejects.toThrow('mapping store unavailable');
+ expect(fs.existsSync(dir)).toBe(true);
+
+ storeUp = true;
+ await flaky.uninstaller.uninstallPlugin('relay');
+
+ expect(fs.existsSync(dir)).toBe(false);
+ expect(storage.getPluginEntry('relay')).toBeUndefined();
+ });
});
describe('PluginUninstaller — uninstall cleans up split-dir plugin storage', () => {
diff --git a/src/core/plugins/plugin-uninstaller.ts b/src/core/plugins/plugin-uninstaller.ts
index 055bac43a..ef5d84030 100644
--- a/src/core/plugins/plugin-uninstaller.ts
+++ b/src/core/plugins/plugin-uninstaller.ts
@@ -35,6 +35,13 @@ export class PluginUninstaller {
private readonly releaseTakeovers?: (pluginId: string) => Promise<void>,
) {}
+ /**
+ * Package folders of plugins whose uninstall stopped after the unload dropped their runtime record,
+ * until a repeat completes: without it the repeat only has the id to go by, and a package whose folder
+ * is not its manifest id would be left on disk to load again on the next boot.
+ */
+ private readonly unloadedDirs = new Map<string, string>();
+
/** <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);
@@ -56,7 +63,8 @@ export class PluginUninstaller {
// it, and for a plugin loaded from the legacy directory the configured root is the wrong tree —
// deleting there removes the ctx.storage directory and leaves the code, so the plugin comes
// back on the next boot having reported a successful uninstall.
- const recordedDir = this.plugins.get(pluginId)?.packageDir ?? null;
+ const recordedDir = this.plugins.get(pluginId)?.packageDir ?? this.unloadedDirs.get(pluginId) ?? null;
+ if (recordedDir) this.unloadedDirs.set(pluginId, recordedDir);
if (this.plugins.has(pluginId)) {
await this.unload(pluginId);
@@ -93,6 +101,7 @@ export class PluginUninstaller {
// Best-effort, and strictly that one plugin's directory.
this.pluginStorage.deletePluginData(pluginId);
+ this.unloadedDirs.delete(pluginId);
this.logger.log(`Plugin uninstalled: ${pluginId}`, { pluginId, action: 'plugin_uninstalled' });
}
}