Repair missing unpacked Effect modules after desktop packaging

- Add an electron-builder afterPack hook to restore the Effect runtime closure
- Add focused tests for hook behavior and build configuration
This commit is contained in:
Julius Marminge committed 2026-07-20 17:42:35 +02:00
1 parent 5d34f9ff23
commit 2892699bfb
4 files changed
+205

No files matched your search

+4
View File
@@ -14,6 +14,7 @@ import {
createStagePatchedDependencies,
createBuildConfig,
DESKTOP_ASAR_UNPACK,
DESKTOP_AFTER_PACK_HOOK_FILE,
InvalidMacPasskeyRpDomainError,
InvalidMacPasskeyPublishableKeyError,
InvalidMockUpdateServerPortError,
@@ -305,6 +306,7 @@ it.layer(NodeServices.layer)("build-desktop-artifact", (it) => {
it("unpacks the fff shared library for filesystem and FFI access", () => {
assert.deepStrictEqual(DESKTOP_ASAR_UNPACK, ["node_modules/@ff-labs/fff-bin-*/**/*"]);
assert.equal(DESKTOP_AFTER_PACK_HOOK_FILE, "desktop-after-pack.ts");
});
it.effect("preserves both Linux icon resize failures with structural context", () => {
@@ -489,9 +491,11 @@ it.layer(NodeServices.layer)("build-desktop-artifact", (it) => {
false,
undefined,
undefined,
"C:\\stage\\desktop-after-pack.ts",
);
const win = config.win as Record<string, unknown>;
assert.equal(config.afterPack, "C:\\stage\\desktop-after-pack.ts");
assert.equal(win.icon, "icon.ico");
assert.equal(win.signAndEditExecutable, true);
assert.notProperty(win, "azureSignOptions");
+8
View File
@@ -580,6 +580,7 @@ interface StagePackageJson {
export const STAGE_INSTALL_ARGS = ["install", "--prod"] as const;
export const DESKTOP_ASAR_UNPACK = ["node_modules/@ff-labs/fff-bin-*/**/*"] as const;
export const DESKTOP_AFTER_PACK_HOOK_FILE = "desktop-after-pack.ts";
export interface MacPasskeySigningConfiguration {
readonly appId: string;
@@ -1385,6 +1386,7 @@ export const createBuildConfig = Effect.fn("createBuildConfig")(function* (
readonly provisioningProfilePath: string;
}
| undefined,
afterPackHookPath?: string,
) {
const buildConfig: Record<string, unknown> = {
appId: DESKTOP_APP_ID,
@@ -1407,6 +1409,7 @@ export const createBuildConfig = Effect.fn("createBuildConfig")(function* (
// files through the asar (transparently redirected to the unpacked copy), so
// there's no duplication.
asarUnpack: [...DESKTOP_ASAR_UNPACK, "apps/server/dist/**", "**/node_modules/**"],
...(afterPackHookPath ? { afterPack: afterPackHookPath } : {}),
};
const updateChannel = resolveDesktopUpdateChannel(version);
const publishConfig = yield* resolveGitHubPublishConfig(updateChannel);
@@ -1776,6 +1779,7 @@ const buildDesktopArtifact = Effect.fn("buildDesktopArtifact")(function* (
provisioningProfilePath: macPasskeySigning.provisioningProfilePath,
}
: undefined,
path.join(stageAppDir, DESKTOP_AFTER_PACK_HOOK_FILE),
),
dependencies: stageDependencies,
devDependencies: {
@@ -1784,6 +1788,10 @@ const buildDesktopArtifact = Effect.fn("buildDesktopArtifact")(function* (
};
const stagePackageJsonString = yield* encodeJsonString(stagePackageJson);
yield* fs.copyFile(
path.join(repoRoot, "scripts", DESKTOP_AFTER_PACK_HOOK_FILE),
path.join(stageAppDir, DESKTOP_AFTER_PACK_HOOK_FILE),
);
yield* fs.writeFileString(path.join(stageAppDir, "package.json"), `${stagePackageJsonString}\n`);
const stageWorkspaceConfig = createStageWorkspaceConfig({
platform: options.platform,
+96
View File
@@ -0,0 +1,96 @@
// @effect-diagnostics nodeBuiltinImport:off - This fixture verifies the filesystem repair boundary.
import * as NodeFSP from "node:fs/promises";
import * as NodeOS from "node:os";
import * as NodePath from "node:path";
import { assert } from "@effect/vitest";
import { afterEach, describe, it } from "vite-plus/test";
import {
repairUnpackedEffectModules,
REQUIRED_UNPACKED_EFFECT_MODULES,
} from "./desktop-after-pack.ts";
const temporaryDirectories: string[] = [];
async function writePackage(
nodeModulesDir: string,
packageName: string,
marker: string,
): Promise<string> {
const packageDir = NodePath.join(nodeModulesDir, ...packageName.split("/"));
await NodeFSP.mkdir(packageDir, { recursive: true });
await NodeFSP.writeFile(
NodePath.join(packageDir, "package.json"),
`${JSON.stringify({ name: packageName, main: "index.js" })}\n`,
);
await NodeFSP.writeFile(NodePath.join(packageDir, "index.js"), `export default ${marker};\n`);
return packageDir;
}
afterEach(async () => {
await Promise.all(
temporaryDirectories
.splice(0)
.map((directory) => NodeFSP.rm(directory, { recursive: true, force: true })),
);
});
describe("desktop afterPack", () => {
it("restores the Effect runtime closure when electron-builder omits unpacked payloads", async () => {
const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3code-after-pack-"));
temporaryDirectories.push(root);
const stageAppDir = NodePath.join(root, "stage");
const appOutDir = NodePath.join(root, "win-unpacked");
const stageNodeModules = NodePath.join(stageAppDir, "node_modules");
await NodeFSP.mkdir(stageAppDir, { recursive: true });
await NodeFSP.writeFile(NodePath.join(stageAppDir, "package.json"), '{"name":"t3code"}\n');
await writePackage(stageNodeModules, "effect", '"effect"');
const platformNodeDir = await writePackage(
stageNodeModules,
"@effect/platform-node",
'"platform-node"',
);
const platformNodeModules = NodePath.join(platformNodeDir, "node_modules");
await writePackage(platformNodeModules, "@effect/platform-node-shared", '"shared"');
await writePackage(platformNodeModules, "mime", '"mime"');
await writePackage(platformNodeModules, "undici", '"undici"');
const staleEffectDir = NodePath.join(
appOutDir,
"resources/app.asar.unpacked/node_modules/effect",
);
await NodeFSP.mkdir(staleEffectDir, { recursive: true });
await NodeFSP.writeFile(NodePath.join(staleEffectDir, "stale.js"), "stale\n");
await repairUnpackedEffectModules({ stageAppDir, appOutDir });
const expectedMarkers: Record<(typeof REQUIRED_UNPACKED_EFFECT_MODULES)[number], string> = {
effect: "effect",
"@effect/platform-node": "platform-node",
"@effect/platform-node-shared": "shared",
mime: "mime",
undici: "undici",
};
for (const packageName of REQUIRED_UNPACKED_EFFECT_MODULES) {
const packageDir = NodePath.join(
appOutDir,
"resources/app.asar.unpacked/node_modules",
...packageName.split("/"),
);
const manifest = JSON.parse(
await NodeFSP.readFile(NodePath.join(packageDir, "package.json"), "utf8"),
) as { readonly name: string };
assert.equal(manifest.name, packageName);
assert.equal(
await NodeFSP.readFile(NodePath.join(packageDir, "index.js"), "utf8"),
`export default "${expectedMarkers[packageName]}";\n`,
);
}
await NodeFSP.access(NodePath.join(staleEffectDir, "stale.js")).then(
() => assert.fail("The stale unpacked payload should have been replaced."),
(error: NodeJS.ErrnoException) => assert.equal(error.code, "ENOENT"),
);
});
});
+97
View File
@@ -0,0 +1,97 @@
// @effect-diagnostics nodeBuiltinImport:off - electron-builder hooks run outside an Effect runtime.
import * as NodeFSP from "node:fs/promises";
import * as NodeModule from "node:module";
import * as NodePath from "node:path";
import * as NodeURL from "node:url";
export const REQUIRED_UNPACKED_EFFECT_MODULES = [
"effect",
"@effect/platform-node",
"@effect/platform-node-shared",
"mime",
"undici",
] as const;
interface DesktopAfterPackContext {
readonly appOutDir: string;
}
async function findPackageRoot(resolvedEntry: string, packageName: string): Promise<string> {
let current = NodePath.dirname(resolvedEntry);
const root = NodePath.parse(current).root;
while (current !== root) {
try {
const manifest = JSON.parse(
await NodeFSP.readFile(NodePath.join(current, "package.json"), "utf8"),
) as { readonly name?: unknown };
if (manifest.name === packageName) {
return current;
}
} catch (error) {
const code = (error as NodeJS.ErrnoException).code;
if (code !== "ENOENT" && !(error instanceof SyntaxError)) {
throw error;
}
}
current = NodePath.dirname(current);
}
throw new Error(`Could not locate the package root for ${packageName} from ${resolvedEntry}.`);
}
async function resolvePackageRoot(
resolver: ReturnType<typeof NodeModule.createRequire>,
packageName: string,
): Promise<string> {
return findPackageRoot(resolver.resolve(packageName), packageName);
}
export async function repairUnpackedEffectModules(input: {
readonly stageAppDir: string;
readonly appOutDir: string;
}): Promise<void> {
const stageResolver = NodeModule.createRequire(NodePath.join(input.stageAppDir, "package.json"));
const platformNodeRoot = await resolvePackageRoot(stageResolver, "@effect/platform-node");
const platformNodeResolver = NodeModule.createRequire(
NodePath.join(platformNodeRoot, "package.json"),
);
const unpackedNodeModules = NodePath.join(
input.appOutDir,
"resources",
"app.asar.unpacked",
"node_modules",
);
for (const packageName of REQUIRED_UNPACKED_EFFECT_MODULES) {
const resolver =
packageName === "effect" || packageName === "@effect/platform-node"
? stageResolver
: platformNodeResolver;
const source = await resolvePackageRoot(resolver, packageName);
const destination = NodePath.join(unpackedNodeModules, ...packageName.split("/"));
// electron-builder can mark a module as unpacked in the ASAR header but omit
// some or all of its payload on Windows. Replace this small runtime closure
// from the staged pnpm install after ASAR creation and before signing/NSIS.
await NodeFSP.rm(destination, { recursive: true, force: true });
await NodeFSP.cp(source, destination, {
recursive: true,
dereference: true,
force: true,
preserveTimestamps: true,
});
const copiedManifest = JSON.parse(
await NodeFSP.readFile(NodePath.join(destination, "package.json"), "utf8"),
) as { readonly name?: unknown };
if (copiedManifest.name !== packageName) {
throw new Error(`Packaged runtime module verification failed for ${packageName}.`);
}
}
}
export async function afterPack(context: DesktopAfterPackContext): Promise<void> {
const stageAppDir = NodePath.dirname(NodeURL.fileURLToPath(import.meta.url));
await repairUnpackedEffectModules({ stageAppDir, appOutDir: context.appOutDir });
}