Files
app__bitwarden-clients/apps/desktop/scripts/debug-start.js
Bernd Schoolmann da69481f74 feat(desktop): Add an isolated debug run script for desktop (#22912)
* feat(desktop): Add an isolated debug run script

Running the desktop client from source shares app data, the SSH agent
socket and the IPC socket with an installed client, so a debug session
corrupts the real profile and the two fight over the same sockets.

debug-start.js runs the same watch + Electron pipeline as start.js but
redirects every piece of host state into a repo-root .debug/ directory,
and reaps the whole pipeline on Ctrl+C so no orphan client survives.

* add vscode config

* address feedback around unrelated electron apps being stopped
2026-09-11 16:01:33 +00:00

108 lines
3.8 KiB
JavaScript

/* eslint-disable @typescript-eslint/no-require-imports */
////
// Same watch + Electron pipeline as start.js, but state is isolated in `.debug`
// so it does not interfere with the host system's bitwarden desktop installation.
//
// .debug/desktop-profile/ app data (vault, settings, logs)
// .debug/.bitwarden-ssh-agent.sock SSH agent socket
// .debug/s.<name> IPC sockets
////
const { execFileSync } = require("child_process");
const path = require("path");
const concurrently = require("concurrently");
// Absolute path to the Electron binary, so it can be `exec`d directly (see the Elec command).
const electronBinary = require("electron");
const rimraf = require("rimraf");
const args = process.argv.splice(2);
const DEBUG_DIR = path.resolve(__dirname, "../../..", ".debug");
process.env.BITWARDEN_APPDATA_DIR = path.join(DEBUG_DIR, "desktop-profile");
process.env.BITWARDEN_SSH_AUTH_SOCK = path.join(DEBUG_DIR, ".bitwarden-ssh-agent.sock");
process.env.BITWARDEN_IPC_SOCKET_DIR = DEBUG_DIR;
process.env.NODE_ENV = "development";
const INSPECT_FLAG = "--inspect=5858";
// `npm run electron` in this checkout runs the very same Electron binary (electron/cli.js just
// spawns `require("electron")`) with the same debug ports, so neither the binary path nor a port
// can tell the two sessions apart. This marker is what killStrayClients matches on. The main
// process only ever scans argv for known flags, so an extra one is inert.
const DEBUG_MARKER = "--bitwarden-debug-run";
const WEBPACK = path.resolve(__dirname, "../../../node_modules/.bin/webpack");
// `exec` replaces the shell with the child, so kill signals reach the child itself instead of a
// wrapper that leaves an orphan behind. cmd.exe has no equivalent, but concurrently reaps the
// whole tree there via `taskkill /F /T`.
const EXEC = process.platform === "win32" ? "" : "exec ";
function watchCommand(configName) {
return `${EXEC}"${WEBPACK}" --config webpack.config.js --config-name ${configName} --watch`;
}
function killStrayClients() {
try {
// Match on the marker, not just the binary: a plain `npm run electron` session runs the
// same binary against the real app data dir and must be left alone.
execFileSync("pkill", ["-9", "-f", `${electronBinary}.*${DEBUG_MARKER}`]);
} catch {
// pkill exits non-zero when nothing matched, and does not exist on Windows.
}
}
killStrayClients();
process.on("exit", killStrayClients);
rimraf.sync("build");
const { commands } = concurrently(
[
{
name: "Main",
command: `npm run build-native && ${watchCommand("main")}`,
prefixColor: "yellow",
},
{
name: "Prel",
command: watchCommand("preload"),
prefixColor: "magenta",
},
{
name: "Rend",
command: watchCommand("renderer"),
prefixColor: "cyan",
},
{
name: "Elec",
command: `npx wait-on ./build/main.js ./build/index.html ./build/app/main.js && ${EXEC}"${electronBinary}" --no-sandbox ${INSPECT_FLAG} --remote-debugging-port=9222 ${DEBUG_MARKER} ${args.join(
" ",
)} ./build`,
prefixColor: "green",
},
],
{
prefix: "name",
outputStream: process.stdout,
killOthersOn: ["success", "failure"],
// Electron ignores SIGINT/SIGTERM here (tray + quit handlers keep it alive), which left an
// orphan client running after Ctrl+C. Nothing in a debug run needs a graceful shutdown.
killSignal: "SIGKILL",
},
);
// Ctrl+C: reap the whole pipeline before leaving. Electron ignores the terminal's SIGINT, and the
// watchers only see it when they share the terminal's process group.
for (const signal of ["SIGINT", "SIGTERM"]) {
process.on(signal, () => {
commands.forEach((command) => command.kill("SIGKILL"));
killStrayClients();
process.exit(0);
});
}