Skip to content

Commit b0e596a

Browse files
committed
fix(desktop): keep sudo working in terminals after a Linux relaunch
1 parent cb9629e commit b0e596a

2 files changed

Lines changed: 127 additions & 7 deletions

File tree

‎apps/desktop/src/electron/ElectronApp.test.ts‎

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,25 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
33
import * as Effect from "effect/Effect";
44
import { beforeEach, vi } from "vite-plus/test";
55

6-
const { getSystemLocaleMock, getUserDefaultMock } = vi.hoisted(() => ({
7-
getSystemLocaleMock: vi.fn(() => "en-GB"),
8-
getUserDefaultMock: vi.fn((_key: string) => false),
9-
}));
6+
const { getSystemLocaleMock, getUserDefaultMock, relaunchMock, spawnMock, unrefMock } = vi.hoisted(
7+
() => {
8+
const unrefMock = vi.fn();
9+
return {
10+
getSystemLocaleMock: vi.fn(() => "en-GB"),
11+
getUserDefaultMock: vi.fn((_key: string) => false),
12+
relaunchMock: vi.fn(),
13+
spawnMock: vi.fn(() => ({ unref: unrefMock })),
14+
unrefMock,
15+
};
16+
},
17+
);
18+
19+
vi.mock("node:child_process", () => ({ spawn: spawnMock }));
1020

1121
vi.mock("electron", () => ({
1222
app: {
1323
getSystemLocale: getSystemLocaleMock,
24+
relaunch: relaunchMock,
1425
},
1526
systemPreferences: {
1627
getUserDefault: getUserDefaultMock,
@@ -23,8 +34,74 @@ describe("ElectronApp", () => {
2334
beforeEach(() => {
2435
getSystemLocaleMock.mockReset();
2536
getUserDefaultMock.mockReset();
37+
relaunchMock.mockReset();
38+
spawnMock.mockClear();
39+
unrefMock.mockReset();
2640
});
2741

42+
it("waits for the old process before starting the new one on Linux", () => {
43+
const launch = ElectronApp.linuxRelaunchCommand(4321, "/opt/t3code/t3code", ["--flag", "it's"]);
44+
assert.strictEqual(launch.command, "/bin/sh");
45+
assert.deepStrictEqual(launch.args.slice(2), [
46+
"t3code-relaunch",
47+
"4321",
48+
"/opt/t3code/t3code",
49+
"--flag",
50+
"it's",
51+
]);
52+
});
53+
54+
it.effect("relaunches through a detached spawn on Linux instead of Electron's relauncher", () =>
55+
Effect.gen(function* () {
56+
const electronApp = yield* ElectronApp.ElectronApp;
57+
yield* electronApp
58+
.relaunch({ execPath: "/opt/t3code/t3code", args: ["--x"] })
59+
.pipe(
60+
Effect.provideService(HostProcess.Platform, "linux"),
61+
Effect.provideService(HostProcess.Environment, {}),
62+
);
63+
64+
assert.strictEqual(relaunchMock.mock.calls.length, 0);
65+
assert.strictEqual(spawnMock.mock.calls.length, 1);
66+
const [command, args, options] = spawnMock.mock.calls[0] as unknown as [
67+
string,
68+
ReadonlyArray<string>,
69+
{ detached: boolean; stdio: string },
70+
];
71+
assert.strictEqual(command, "/bin/sh");
72+
assert.deepStrictEqual(args.slice(3), [String(process.pid), "/opt/t3code/t3code", "--x"]);
73+
assert.deepStrictEqual(options, { detached: true, stdio: "ignore" });
74+
assert.strictEqual(unrefMock.mock.calls.length, 1);
75+
}).pipe(Effect.provide(ElectronApp.layer)),
76+
);
77+
78+
it.effect("relaunches the AppImage file rather than its temporary mount", () =>
79+
Effect.gen(function* () {
80+
const electronApp = yield* ElectronApp.ElectronApp;
81+
yield* electronApp.relaunch({ execPath: process.execPath, args: ["--x"] }).pipe(
82+
Effect.provideService(HostProcess.Platform, "linux"),
83+
Effect.provideService(HostProcess.Environment, {
84+
APPIMAGE: "/home/me/Apps/T3-Code.AppImage",
85+
}),
86+
);
87+
88+
const [, args] = spawnMock.mock.calls[0] as unknown as [string, ReadonlyArray<string>];
89+
assert.deepStrictEqual(args.slice(4), ["/home/me/Apps/T3-Code.AppImage", "--x"]);
90+
}).pipe(Effect.provide(ElectronApp.layer)),
91+
);
92+
93+
it.effect("keeps Electron's relauncher on other platforms", () =>
94+
Effect.gen(function* () {
95+
const electronApp = yield* ElectronApp.ElectronApp;
96+
yield* electronApp
97+
.relaunch({ args: ["--x"] })
98+
.pipe(Effect.provideService(HostProcess.Platform, "darwin"));
99+
100+
assert.deepStrictEqual(relaunchMock.mock.calls, [[{ args: ["--x"] }]]);
101+
assert.strictEqual(spawnMock.mock.calls.length, 0);
102+
}).pipe(Effect.provide(ElectronApp.layer)),
103+
);
104+
28105
it("keeps the macOS region override and hour cycle in the locale tag", () => {
29106
assert.strictEqual(ElectronApp.toSystemLocaleTag("en_GB"), "en-GB");
30107
assert.strictEqual(ElectronApp.toSystemLocaleTag("en-US@rg=nlzzzz", "h12"), "en-NL-u-hc-h12");

‎apps/desktop/src/electron/ElectronApp.ts‎

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// @effect-diagnostics nodeBuiltinImport:off
2+
// The Linux relaunch must outlive this process; scoped ChildProcess kills children on exit.
3+
import * as NodeChildProcess from "node:child_process";
4+
15
import * as HostProcess from "@t3tools/shared/HostProcess";
26
import * as Context from "effect/Context";
37
import * as Effect from "effect/Effect";
@@ -39,6 +43,29 @@ export class ElectronAppWhenReadyError extends Schema.TaggedError<ElectronAppWhe
3943
}
4044
}
4145

46+
/**
47+
* Electron's Linux relauncher helper gets no_new_privs from Chromium's launch
48+
* defaults, and the relaunched app and its terminals inherit it, breaking sudo.
49+
* Node's spawn doesn't set it: wait for this process to exit, then start again.
50+
*/
51+
export function linuxRelaunchCommand(
52+
pid: number,
53+
execPath: string,
54+
args: ReadonlyArray<string>,
55+
): { readonly command: string; readonly args: ReadonlyArray<string> } {
56+
return {
57+
command: "/bin/sh",
58+
args: [
59+
"-c",
60+
'while kill -0 "$1" 2>/dev/null; do sleep 0.1; done; shift; exec "$@"',
61+
"t3code-relaunch",
62+
String(pid),
63+
execPath,
64+
...args,
65+
],
66+
};
67+
}
68+
4269
export class ElectronApp extends Context.Service<
4370
ElectronApp,
4471
{
@@ -171,9 +198,25 @@ export const make = ElectronApp.of({
171198
Electron.app.exit(code);
172199
});
173200
}),
174-
relaunch: Effect.fn("ElectronApp.relaunch")(function (options) {
175-
return Effect.sync(() => {
176-
Electron.app.relaunch(options);
201+
relaunch: Effect.fn("ElectronApp.relaunch")(function* (options) {
202+
const platform = yield* HostProcess.Platform;
203+
if (platform !== "linux") {
204+
yield* Effect.sync(() => Electron.app.relaunch(options));
205+
return;
206+
}
207+
const execPath = options.execPath ?? process.execPath;
208+
// The AppImage mount goes away with this process, so start the AppImage itself.
209+
const appImage = (yield* HostProcess.Environment).APPIMAGE?.trim();
210+
const launch = linuxRelaunchCommand(
211+
process.pid,
212+
appImage && execPath === process.execPath ? appImage : execPath,
213+
options.args ?? process.argv.slice(1),
214+
);
215+
yield* Effect.sync(() => {
216+
NodeChildProcess.spawn(launch.command, launch.args, {
217+
detached: true,
218+
stdio: "ignore",
219+
}).unref();
177220
});
178221
}),
179222
setPath: Effect.fn("ElectronApp.setPath")(function (name, path) {

0 commit comments

Comments
 (0)