Skip to content

Commit fbf0b1b

Browse files
committed
fix(plugins): clean legacy retained npm roots
1 parent 1232090 commit fbf0b1b

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

‎src/plugins/managed-npm-retention.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,28 @@ describe("managed npm retention", () => {
4848
fs.rmSync(stateDir, { recursive: true, force: true });
4949
}
5050
});
51+
52+
it("cleans retained packages from the legacy shared npm root", async () => {
53+
const stateDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-retention-"));
54+
const npmDir = path.join(stateDir, "npm");
55+
const packageDir = path.join(npmDir, "node_modules", "@openclaw", "codex");
56+
fs.mkdirSync(packageDir, { recursive: true });
57+
await markRetainedManagedNpmInstall({
58+
packageDir,
59+
pluginId: "codex",
60+
reason: "test-legacy-generation",
61+
});
62+
63+
try {
64+
await expect(
65+
cleanupRetainedManagedNpmInstallGenerations({
66+
npmDir,
67+
}),
68+
).resolves.toBe(1);
69+
expect(fs.existsSync(packageDir)).toBe(false);
70+
expect(hasRetainedManagedNpmInstallMarker(packageDir)).toBe(false);
71+
} finally {
72+
fs.rmSync(stateDir, { recursive: true, force: true });
73+
}
74+
});
5175
});

‎src/plugins/managed-npm-retention.ts‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from "node:fs";
33
import path from "node:path";
44
import { safePathSegmentHashed } from "../infra/install-safe-path.js";
55
import { resolveDefaultPluginNpmDir, resolvePluginNpmProjectsDir } from "./install-paths.js";
6-
import { listManagedPluginNpmProjectRootsSync } from "./npm-project-roots.js";
6+
import { listManagedPluginNpmRootsSync } from "./npm-project-roots.js";
77

88
export const RETAINED_MANAGED_NPM_INSTALL_MARKER = ".openclaw-retained-npm-install.json";
99
const RETAINED_MANAGED_NPM_INSTALL_MARKER_DIR = ".openclaw-retained-npm-installs";
@@ -117,6 +117,55 @@ function isPathEqualOrInside(parentPath: string, childPath: string): boolean {
117117
return relative === "" || (relative !== ".." && !relative.startsWith(`..${path.sep}`));
118118
}
119119

120+
function listManagedNpmPackageDirs(npmRoot: string): string[] {
121+
const nodeModulesDir = path.join(npmRoot, "node_modules");
122+
let entries: fs.Dirent[];
123+
try {
124+
entries = fs.readdirSync(nodeModulesDir, { withFileTypes: true });
125+
} catch (error) {
126+
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
127+
return [];
128+
}
129+
throw error;
130+
}
131+
return entries.flatMap((entry) => {
132+
if (!entry.isDirectory()) {
133+
return [];
134+
}
135+
if (!entry.name.startsWith("@")) {
136+
return [path.join(nodeModulesDir, entry.name)];
137+
}
138+
return fs
139+
.readdirSync(path.join(nodeModulesDir, entry.name), { withFileTypes: true })
140+
.filter((scopedEntry) => scopedEntry.isDirectory())
141+
.map((scopedEntry) => path.join(nodeModulesDir, entry.name, scopedEntry.name));
142+
});
143+
}
144+
145+
async function cleanupRetainedLegacyNpmPackages(params: {
146+
npmRoot: string;
147+
activeInstallPaths: string[];
148+
onError?: (error: unknown, projectRoot: string) => void;
149+
}): Promise<number> {
150+
let removed = 0;
151+
for (const packageDir of listManagedNpmPackageDirs(params.npmRoot)) {
152+
if (
153+
!hasRetainedManagedNpmInstallMarker(packageDir) ||
154+
params.activeInstallPaths.some((installPath) => isPathEqualOrInside(packageDir, installPath))
155+
) {
156+
continue;
157+
}
158+
try {
159+
await fs.promises.rm(packageDir, { recursive: true, force: true });
160+
await clearRetainedManagedNpmInstallMarker(packageDir);
161+
removed += 1;
162+
} catch (error) {
163+
params.onError?.(error, packageDir);
164+
}
165+
}
166+
return removed;
167+
}
168+
120169
export async function cleanupRetainedManagedNpmInstallGenerations(
121170
params: {
122171
activeInstallPaths?: Iterable<string>;
@@ -133,7 +182,15 @@ export async function cleanupRetainedManagedNpmInstallGenerations(
133182
path.resolve(installPath),
134183
);
135184
let removed = 0;
136-
for (const projectRoot of listManagedPluginNpmProjectRootsSync(npmDir)) {
185+
for (const projectRoot of listManagedPluginNpmRootsSync(npmDir)) {
186+
if (path.resolve(projectRoot) === path.resolve(npmDir)) {
187+
removed += await cleanupRetainedLegacyNpmPackages({
188+
npmRoot: projectRoot,
189+
activeInstallPaths,
190+
onError: params.onError,
191+
});
192+
continue;
193+
}
137194
const markerDir = path.join(projectRoot, RETAINED_MANAGED_NPM_INSTALL_MARKER_DIR);
138195
let markerEntries: fs.Dirent[];
139196
try {

0 commit comments

Comments
 (0)