Skip to content

Commit 58b8aee

Browse files
committed
fix(plugins): bind npm cleanup to planned root kind
1 parent 68e9404 commit 58b8aee

4 files changed

Lines changed: 36 additions & 12 deletions

File tree

‎src/plugins/install-persistence.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ describe("persistPluginInstall", () => {
403403
kind: "npm",
404404
npmRoot: previousProjectRoot,
405405
packageName: "@openclaw/codex",
406+
rootKind: "isolated-project",
406407
},
407408
},
408409
});

‎src/plugins/management-service.install-compensation.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe("managed plugin install compensation", () => {
162162

163163
expect(mocks.applyUninstall).toHaveBeenCalledWith({
164164
target: npmRoot,
165-
cleanup: { kind: "npm", npmRoot, packageName },
165+
cleanup: { kind: "npm", npmRoot, packageName, rootKind: "isolated-project" },
166166
});
167167
await expect(fs.access(npmRoot)).rejects.toMatchObject({ code: "ENOENT" });
168168
},

‎src/plugins/uninstall.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,7 @@ describe("uninstallPlugin", () => {
12301230
kind: "npm",
12311231
npmRoot,
12321232
packageName: "@openclaw/kitchen-sink",
1233+
rootKind: "legacy-shared",
12331234
},
12341235
});
12351236

@@ -1309,6 +1310,7 @@ describe("uninstallPlugin", () => {
13091310
kind: "npm",
13101311
npmRoot,
13111312
packageName: "@openclaw/kitchen-sink",
1313+
rootKind: "isolated-project",
13121314
},
13131315
});
13141316

@@ -1406,12 +1408,14 @@ describe("uninstallPlugin", () => {
14061408
kind: "npm",
14071409
npmRoot,
14081410
packageName: "@openclaw/kitchen-sink",
1411+
rootKind: "isolated-project",
14091412
},
14101413
});
14111414

14121415
const applied = await applyPluginUninstallDirectoryRemoval(plan.directoryRemoval);
14131416

14141417
expect(applied).toEqual({ directoryRemoved: true, warnings: [] });
1418+
expect(runCommandWithTimeoutMock).not.toHaveBeenCalled();
14151419
await expectPathAccessState(pluginDir, "missing");
14161420
await expect(fs.readFile(siblingFile, "utf8")).resolves.toBe("preserve me");
14171421
});
@@ -1736,6 +1740,7 @@ describe("uninstallPlugin", () => {
17361740
kind: "npm",
17371741
npmRoot,
17381742
packageName: "removed-plugin",
1743+
rootKind: "legacy-shared",
17391744
},
17401745
});
17411746

@@ -1844,6 +1849,7 @@ describe("uninstallPlugin", () => {
18441849
kind: "npm",
18451850
npmRoot,
18461851
packageName: "removed-plugin",
1852+
rootKind: "legacy-shared",
18471853
},
18481854
});
18491855

@@ -2095,6 +2101,7 @@ describe("uninstallPlugin", () => {
20952101
kind: "npm",
20962102
npmRoot,
20972103
packageName: "missing-plugin",
2104+
rootKind: "legacy-shared",
20982105
},
20992106
});
21002107

‎src/plugins/uninstall.ts‎

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export type PluginUninstallDirectoryRemoval = {
8282
kind: "npm";
8383
npmRoot: string;
8484
packageName: string;
85+
rootKind: "legacy-shared" | "isolated-project";
8586
}
8687
| {
8788
kind: "git";
@@ -164,7 +165,12 @@ function resolveUninstallDirectoryTarget(params: {
164165
function resolveNpmManagedInstall(params: {
165166
installRecord?: PluginInstallRecord;
166167
extensionsDir?: string;
167-
}): { installPath: string; npmRoot: string; packageName: string } | null {
168+
}): {
169+
installPath: string;
170+
npmRoot: string;
171+
packageName: string;
172+
rootKind: "legacy-shared" | "isolated-project";
173+
} | null {
168174
const installPath = params.installRecord?.installPath?.trim();
169175
if (params.installRecord?.source !== "npm" || !installPath) {
170176
return null;
@@ -184,7 +190,7 @@ function resolveNpmManagedInstall(params: {
184190
resolveComparableUninstallPath(installPath)
185191
) {
186192
const packageName = resolveNpmPackageNameFromInstallPath({ installPath, nodeModulesRoot });
187-
return packageName ? { installPath, npmRoot, packageName } : null;
193+
return packageName ? { installPath, npmRoot, packageName, rootKind: "legacy-shared" } : null;
188194
}
189195
const projectMatch = resolveNpmManagedProjectInstall({
190196
installPath,
@@ -197,10 +203,12 @@ function resolveNpmManagedInstall(params: {
197203
return null;
198204
}
199205

200-
function resolveNpmManagedProjectInstall(params: {
206+
function resolveNpmManagedProjectInstall(params: { installPath: string; projectsDir: string }): {
201207
installPath: string;
202-
projectsDir: string;
203-
}): { installPath: string; npmRoot: string; packageName: string } | null {
208+
npmRoot: string;
209+
packageName: string;
210+
rootKind: "isolated-project";
211+
} | null {
204212
if (
205213
!isUninstallPathInsideOrEqual(params.projectsDir, params.installPath) ||
206214
resolveComparableUninstallPath(params.projectsDir) ===
@@ -238,7 +246,12 @@ function resolveNpmManagedProjectInstall(params: {
238246
projectDir: npmRoot,
239247
npmDir,
240248
});
241-
return { installPath: ownsProjectRoot ? npmRoot : params.installPath, npmRoot, packageName };
249+
return {
250+
installPath: ownsProjectRoot ? npmRoot : params.installPath,
251+
npmRoot,
252+
packageName,
253+
rootKind: "isolated-project",
254+
};
242255
}
243256

244257
function resolveNpmPackageNameFromInstallPath(params: {
@@ -423,6 +436,7 @@ export function planPluginUninstall(params: UninstallPluginParams): PluginUninst
423436
kind: "npm",
424437
npmRoot: npmManagedInstall.npmRoot,
425438
packageName: npmManagedInstall.packageName,
439+
rootKind: npmManagedInstall.rootKind,
426440
},
427441
}
428442
: gitManagedInstall && deleteTarget === gitManagedInstall.installPath
@@ -453,7 +467,10 @@ function isOwnedNpmRemoval(removal: PluginUninstallDirectoryRemoval): boolean {
453467
return true;
454468
}
455469
const projectsDir = path.dirname(cleanup.npmRoot);
456-
const projectRoot = path.basename(projectsDir) === "projects";
470+
const projectRoot = cleanup.rootKind === "isolated-project";
471+
if (projectRoot !== (path.basename(projectsDir) === "projects")) {
472+
return false;
473+
}
457474
const npmDir = projectRoot ? path.dirname(projectsDir) : cleanup.npmRoot;
458475
if (
459476
projectRoot
@@ -510,9 +527,8 @@ export async function applyPluginUninstallDirectoryRemoval(
510527
return { directoryRemoved: false, warnings };
511528
}
512529

513-
const removesNpmProject =
514-
removal.cleanup?.kind === "npm" &&
515-
path.resolve(removal.target) === path.resolve(removal.cleanup.npmRoot);
530+
const usesLegacySharedNpmRoot =
531+
removal.cleanup?.kind === "npm" && removal.cleanup.rootKind === "legacy-shared";
516532
const npmCleanupManifestPath =
517533
removal.cleanup?.kind === "npm" ? path.join(removal.cleanup.npmRoot, "package.json") : "";
518534
const npmCleanupManifestExists =
@@ -531,7 +547,7 @@ export async function applyPluginUninstallDirectoryRemoval(
531547
if (!isOwnedNpmRemoval(removal)) {
532548
return { directoryRemoved: false, warnings: [ownershipWarning] };
533549
}
534-
if (removal.cleanup?.kind === "npm" && npmCleanupManifestExists && !removesNpmProject) {
550+
if (removal.cleanup?.kind === "npm" && npmCleanupManifestExists && usesLegacySharedNpmRoot) {
535551
const uninstall = await runCommandWithTimeout(
536552
[
537553
"npm",

0 commit comments

Comments
 (0)