fix(plugins): reuse unchanged runtime mirrors

This commit is contained in:
Peter Steinberger
2026-04-27 23:44:57 +01:00
parent 323030594e
commit 6f09039b0c
6 changed files with 407 additions and 94 deletions

View File

@@ -43,6 +43,10 @@ import {
withBundledRuntimeDepsFilesystemLock,
type BundledRuntimeDepsInstallParams,
} from "./bundled-runtime-deps.js";
import {
copyBundledPluginRuntimeRoot,
refreshBundledPluginRuntimeMirrorRoot,
} from "./bundled-runtime-mirror.js";
import {
clearPluginCommands,
listRegisteredPluginCommands,
@@ -703,15 +707,12 @@ function mirrorBundledPluginRuntimeRoot(params: {
if (path.resolve(mirrorRoot) === path.resolve(params.pluginRoot)) {
return mirrorRoot;
}
const tempDir = fs.mkdtempSync(path.join(mirrorParent, `.plugin-${params.pluginId}-`));
const stagedRoot = path.join(tempDir, "plugin");
try {
copyBundledPluginRuntimeRoot(params.pluginRoot, stagedRoot);
fs.rmSync(mirrorRoot, { recursive: true, force: true });
fs.renameSync(stagedRoot, mirrorRoot);
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
refreshBundledPluginRuntimeMirrorRoot({
pluginId: params.pluginId,
sourceRoot: params.pluginRoot,
targetRoot: mirrorRoot,
tempDirParent: mirrorParent,
});
return mirrorRoot;
},
);
@@ -819,17 +820,12 @@ function mirrorCanonicalBundledRuntimeDistRoot(params: {
return;
}
const targetCanonicalPluginRoot = path.join(targetCanonicalDistRoot, "extensions", pluginId);
const tempDir = fs.mkdtempSync(
path.join(path.dirname(targetCanonicalPluginRoot), `.plugin-${pluginId}-`),
);
const stagedRoot = path.join(tempDir, "plugin");
try {
copyBundledPluginRuntimeRoot(sourceCanonicalPluginRoot, stagedRoot);
fs.rmSync(targetCanonicalPluginRoot, { recursive: true, force: true });
fs.renameSync(stagedRoot, targetCanonicalPluginRoot);
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
refreshBundledPluginRuntimeMirrorRoot({
pluginId,
sourceRoot: sourceCanonicalPluginRoot,
targetRoot: targetCanonicalPluginRoot,
tempDirParent: path.dirname(targetCanonicalPluginRoot),
});
}
function ensureBundledRuntimeDistPackageJson(mirrorDistRoot: string): void {
@@ -840,38 +836,6 @@ function ensureBundledRuntimeDistPackageJson(mirrorDistRoot: string): void {
writeRuntimeJsonFile(packageJsonPath, { type: "module" });
}
function copyBundledPluginRuntimeRoot(sourceRoot: string, targetRoot: string): void {
if (path.resolve(sourceRoot) === path.resolve(targetRoot)) {
return;
}
fs.mkdirSync(targetRoot, { recursive: true, mode: 0o755 });
for (const entry of fs.readdirSync(sourceRoot, { withFileTypes: true })) {
if (entry.name === "node_modules") {
continue;
}
const sourcePath = path.join(sourceRoot, entry.name);
const targetPath = path.join(targetRoot, entry.name);
if (entry.isDirectory()) {
copyBundledPluginRuntimeRoot(sourcePath, targetPath);
continue;
}
if (entry.isSymbolicLink()) {
fs.symlinkSync(fs.readlinkSync(sourcePath), targetPath);
continue;
}
if (!entry.isFile()) {
continue;
}
fs.copyFileSync(sourcePath, targetPath);
try {
const sourceMode = fs.statSync(sourcePath).mode;
fs.chmodSync(targetPath, sourceMode | 0o600);
} catch {
// Readable copied files are enough for plugin loading.
}
}
}
function writeRuntimeJsonFile(targetPath: string, value: unknown): void {
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
fs.writeFileSync(targetPath, `${JSON.stringify(value, null, 2)}\n`, "utf8");