Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 81 additions & 14 deletions apps/desktop/scripts/verify-electron.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { spawnSync } from "node:child_process";
import { existsSync, readFileSync } from "node:fs";
import { createRequire } from "node:module";
import path from "node:path";
Expand All @@ -19,6 +20,76 @@ function fail(message) {
process.exit(1);
}

function inspectElectronRuntime(electronPackageDir) {
const pathFile = path.join(electronPackageDir, "path.txt");

if (!existsSync(pathFile)) {
return {
message:
"Electron package metadata exists, but the downloaded binary marker file path.txt is missing.",
ok: false,
};
}

const executableRelativePath = readFileSync(pathFile, "utf8").trim();

if (!executableRelativePath) {
return {
message: "Electron path.txt is empty, so the runtime executable cannot be located.",
ok: false,
};
}

const executablePath = path.join(electronPackageDir, "dist", executableRelativePath);

if (!existsSync(executablePath)) {
return {
message: `Electron expected a runtime executable at ${executablePath}, but it was not found.`,
ok: false,
};
}

return { ok: true };
}

function attemptRepair(electronPackageDir, reason) {
const installScript = path.join(electronPackageDir, "install.js");

if (!existsSync(installScript)) {
return {
message: `Automatic repair could not start because ${installScript} does not exist.`,
ok: false,
};
}

console.warn("");
console.warn("[Corexa desktop] Electron runtime is incomplete.");
console.warn(`[Corexa desktop] ${reason}`);
console.warn("[Corexa desktop] Attempting an automatic Electron repair...");
console.warn("");

const result = spawnSync(process.execPath, [installScript], {
cwd: electronPackageDir,
stdio: "inherit",
});

if (result.error) {
return {
message: `Automatic Electron repair failed to start: ${result.error.message}`,
ok: false,
};
}

if (result.status !== 0) {
return {
message: `Automatic Electron repair exited with status ${result.status ?? "unknown"}.`,
ok: false,
};
}

return { ok: true };
}

let electronPackageDir;

try {
Expand All @@ -27,22 +98,18 @@ try {
fail("The electron package could not be resolved from apps/desktop/node_modules.");
}

const pathFile = path.join(electronPackageDir, "path.txt");
let runtimeState = inspectElectronRuntime(electronPackageDir);

if (!existsSync(pathFile)) {
fail(
"Electron package metadata exists, but the downloaded binary marker file path.txt is missing.",
);
}
if (!runtimeState.ok) {
const repairResult = attemptRepair(electronPackageDir, runtimeState.message);

const executableRelativePath = readFileSync(pathFile, "utf8").trim();

if (!executableRelativePath) {
fail("Electron path.txt is empty, so the runtime executable cannot be located.");
}
if (!repairResult.ok) {
fail(repairResult.message);
}

const executablePath = path.join(electronPackageDir, "dist", executableRelativePath);
runtimeState = inspectElectronRuntime(electronPackageDir);

if (!existsSync(executablePath)) {
fail(`Electron expected a runtime executable at ${executablePath}, but it was not found.`);
if (!runtimeState.ok) {
fail(`Automatic repair completed, but Electron is still incomplete. ${runtimeState.message}`);
}
}
2 changes: 1 addition & 1 deletion docs/development/local-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ cargo test --manifest-path native/tree-sitter-indexer/Cargo.toml

## Desktop Troubleshooting

If Electron is installed but the binary payload is missing:
If Electron is installed but the binary payload is missing, the desktop preflight now attempts an automatic repair before it exits. If you still need to repair it manually:

```bash
pnpm --filter @corexa/desktop run repair:electron
Expand Down
Loading