Skip to content

Commit dc98770

Browse files
fix(desktop): invalidate partial legacy trees
Co-authored-by: Utkarsh Patil <[email protected]>
1 parent cee8a4a commit dc98770

2 files changed

Lines changed: 69 additions & 1 deletion

File tree

‎apps/desktop/src/wsl/DesktopWslServerTree.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,67 @@ describe("DesktopWslServerTree", () => {
323323
).pipe(Effect.provide(NodeServices.layer)),
324324
);
325325

326+
it.effect("re-extracts after legacy cleanup partially deletes the completed tree", () =>
327+
withTempDir((tempDir) =>
328+
Effect.gen(function* () {
329+
const fileSystem = yield* FileSystem.FileSystem;
330+
const path = yield* Path.Path;
331+
const resourcesPath = path.join(tempDir, "resources");
332+
const serverRoot = path.join(resourcesPath, "server.asar");
333+
const sourceEntryPath = path.join(serverRoot, "apps/server/dist/bin.mjs");
334+
yield* fileSystem.makeDirectory(path.dirname(sourceEntryPath), { recursive: true });
335+
yield* fileSystem.writeFileString(sourceEntryPath, "fresh-server-entry");
336+
337+
const initial = yield* ensureWith({ baseDir: tempDir, resourcesPath });
338+
assert.isTrue(initial.ok);
339+
const versionDir = initial.ok ? initial.root : "";
340+
const treeRoot = path.dirname(versionDir);
341+
const extractedEntryPath = path.join(versionDir, "apps/server/dist/bin.mjs");
342+
let cleanupFailed = false;
343+
const partialCleanupFileSystem = Layer.effect(
344+
FileSystem.FileSystem,
345+
Effect.gen(function* () {
346+
const realFileSystem = yield* FileSystem.FileSystem;
347+
return {
348+
...realFileSystem,
349+
remove: (target, options) =>
350+
String(target) === treeRoot && options?.recursive === true && !cleanupFailed
351+
? Effect.gen(function* () {
352+
cleanupFailed = true;
353+
yield* realFileSystem.remove(extractedEntryPath);
354+
return yield* PlatformError.systemError({
355+
_tag: "PermissionDenied",
356+
module: "FileSystem",
357+
method: "remove",
358+
pathOrDescriptor: treeRoot,
359+
description: "simulated partial legacy cleanup",
360+
});
361+
})
362+
: realFileSystem.remove(target, options),
363+
} satisfies FileSystem.FileSystem;
364+
}),
365+
).pipe(Layer.provide(NodeServices.layer));
366+
367+
const result = yield* Effect.gen(function* () {
368+
const tree = yield* DesktopWslServerTree.DesktopWslServerTree;
369+
yield* tree.cleanupLegacy;
370+
return yield* tree.ensure;
371+
}).pipe(
372+
Effect.provide(
373+
DesktopWslServerTree.layer.pipe(
374+
Layer.provideMerge(environmentLayer({ baseDir: tempDir, resourcesPath })),
375+
Layer.provideMerge(partialCleanupFileSystem),
376+
),
377+
),
378+
);
379+
380+
assert.isTrue(cleanupFailed);
381+
assert.isTrue(result.ok);
382+
assert.equal(yield* fileSystem.readFileString(extractedEntryPath), "fresh-server-entry");
383+
}),
384+
).pipe(Effect.provide(NodeServices.layer)),
385+
);
386+
326387
it.effect("reports a retryable failure when the archive cannot be read", () =>
327388
withTempDir((tempDir) =>
328389
Effect.gen(function* () {

‎apps/desktop/src/wsl/DesktopWslServerTree.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,14 @@ export const make = Effect.gen(function* () {
179179
const cleanupLegacy = gate
180180
.withPermits(1)(
181181
needsExtraction
182-
? fs.remove(treeRoot, { recursive: true, force: true }).pipe(
182+
? Effect.gen(function* () {
183+
// Invalidate completeness before recursive deletion. Windows can
184+
// remove part of a tree and then fail on a locked file; without
185+
// this ordering, a surviving marker makes ensure reuse that
186+
// half-deleted fallback instead of extracting it again.
187+
yield* fs.remove(join(versionDir, MARKER_FILE_NAME), { force: true });
188+
yield* fs.remove(treeRoot, { recursive: true, force: true });
189+
}).pipe(
183190
Effect.catch((cause) =>
184191
Effect.logWarning("[wsl-server-tree] Could not remove the legacy extraction cache.", {
185192
treeRoot,

0 commit comments

Comments
 (0)