Skip to content

Commit 96c7bf6

Browse files
authored
fix(desktop): isolate packaged macOS maintenance profiles (#29)
* fix(desktop): isolate packaged macOS maintenance profiles * chore: trigger normal CI for reviewed isolation repair --------- Co-authored-by: nullStack65 <nullStack65@users.noreply.github.com>
1 parent aa1e770 commit 96c7bf6

51 files changed

Lines changed: 1532 additions & 410 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/app/DesktopApp.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,9 @@ const startup = Effect.gen(function* () {
325325
yield* appIdentity.configure;
326326
yield* applicationMenu.configure;
327327
yield* updates.configure;
328-
yield* DesktopRemoteUpdates.listen;
328+
if (environment.isolationProfile === undefined) {
329+
yield* DesktopRemoteUpdates.listen;
330+
}
329331
yield* linuxUrlHandler.register;
330332
yield* bootstrap.pipe(Effect.catchCause((cause) => fatalStartupCause("bootstrap", cause)));
331333
}).pipe(Effect.withSpan("desktop.startup"));

‎apps/desktop/src/app/DesktopAppIdentity.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const makeElectronAppLayer = (calls: ElectronAppCalls) =>
4343
name: Effect.succeed("T3 Code"),
4444
systemLocale: Effect.succeed("en-US"),
4545
whenReady: Effect.void,
46+
requestSingleInstanceLock: Effect.succeed(true),
4647
quit: Effect.void,
4748
exit: () => Effect.void,
4849
relaunch: () => Effect.void,

‎apps/desktop/src/app/DesktopAppIdentity.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ const normalizeCommitHash = (value: string): Option.Option<string> => {
4747

4848
export const resolveUserDataPath = Effect.gen(function* () {
4949
const environment = yield* DesktopEnvironment.DesktopEnvironment;
50+
if (environment.isolationProfile !== undefined) {
51+
return environment.userDataDirectory;
52+
}
5053
const fileSystem = yield* FileSystem.FileSystem;
5154
const legacyPath = environment.path.join(
5255
environment.appDataDirectory,

‎apps/desktop/src/app/DesktopClerk.test.ts‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,29 @@ import * as ElectronWindow from "../electron/ElectronWindow.ts";
2929
import * as DesktopClerk from "./DesktopClerk.ts";
3030
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
3131

32-
const makeDesktopClerkLayer = (isDevelopment = true, events: string[] = []) => {
32+
const makeDesktopClerkLayer = (
33+
isDevelopment = true,
34+
events: string[] = [],
35+
isolationProfile?: NonNullable<
36+
DesktopEnvironment.DesktopEnvironment["Service"]["isolationProfile"]
37+
>,
38+
) => {
3339
const environment = DesktopEnvironment.DesktopEnvironment.of({
3440
stateDir: "/tmp/t3-state",
3541
isDevelopment,
3642
appDataDirectory: "/tmp/app-data",
43+
userDataDirectory: isolationProfile?.userDataDirectory ?? "/tmp/app-data/t3code-dev",
44+
isolationProfile,
3745
userDataDirName: isDevelopment ? "t3code-dev" : "t3code",
3846
legacyUserDataDirName: isDevelopment ? "T3 Code (Dev)" : "T3 Code (Alpha)",
3947
path: { join: (...parts: ReadonlyArray<string>) => parts.join("/") },
4048
} as unknown as DesktopEnvironment.DesktopEnvironment["Service"]);
4149

4250
const electronApp = {
51+
requestSingleInstanceLock: Effect.sync(() => {
52+
events.push("requestSingleInstanceLock");
53+
return true;
54+
}),
4355
setPath: (name: string, value: string) =>
4456
Effect.sync(() => {
4557
events.push(`setPath:${name}:${value}`);
@@ -63,6 +75,28 @@ describe("DesktopClerk", () => {
6375
storageMock.mockReset();
6476
});
6577

78+
it.effect("uses the isolated Electron lock without creating a Clerk bridge", () => {
79+
const profile = {
80+
root: "/tmp/t3-profile",
81+
homeDirectory: "/tmp/t3-profile",
82+
appDataDirectory: "/tmp/t3-profile/Library/Application Support",
83+
userDataDirectory: "/tmp/t3-profile/userData",
84+
sessionDataDirectory: "/tmp/t3-profile/sessionData",
85+
t3Home: "/tmp/t3-profile/.t3",
86+
};
87+
const events: string[] = [];
88+
89+
return Effect.gen(function* () {
90+
yield* Effect.scoped(Layer.build(makeDesktopClerkLayer(true, events, profile)));
91+
assert.deepEqual(events, [
92+
`setPath:userData:${profile.userDataDirectory}`,
93+
"requestSingleInstanceLock",
94+
]);
95+
assert.equal(createClerkBridgeMock.mock.calls.length, 0);
96+
assert.equal(storageMock.mock.calls.length, 0);
97+
});
98+
});
99+
66100
it.effect("acquires and releases the SDK bridge with the layer", () => {
67101
const cleanup = vi.fn();
68102
const events: string[] = [];

‎apps/desktop/src/app/DesktopClerk.ts‎

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -97,27 +97,30 @@ export const make = Effect.gen(function* () {
9797
const userDataPath = yield* DesktopAppIdentity.resolveUserDataPath;
9898
yield* electronApp.setPath("userData", userDataPath);
9999

100-
const bridge = yield* Effect.acquireRelease(
101-
Effect.try({
102-
try: () => createDesktopClerkBridge(environment.stateDir, environment.isDevelopment),
103-
catch: (cause) =>
104-
new DesktopClerkBridgeInitializationError({
105-
stateDir: environment.stateDir,
106-
isDevelopment: environment.isDevelopment,
107-
cause,
108-
}),
109-
}),
110-
(bridge) =>
111-
Effect.try({
112-
try: () => bridge.cleanup(),
113-
catch: (cause) =>
114-
new DesktopClerkBridgeCleanupError({
115-
stateDir: environment.stateDir,
116-
isDevelopment: environment.isDevelopment,
117-
cause,
100+
const isPrimaryInstance =
101+
environment.isolationProfile !== undefined
102+
? yield* electronApp.requestSingleInstanceLock
103+
: yield* Effect.acquireRelease(
104+
Effect.try({
105+
try: () => createDesktopClerkBridge(environment.stateDir, environment.isDevelopment),
106+
catch: (cause) =>
107+
new DesktopClerkBridgeInitializationError({
108+
stateDir: environment.stateDir,
109+
isDevelopment: environment.isDevelopment,
110+
cause,
111+
}),
118112
}),
119-
}).pipe(Effect.orDie),
120-
);
113+
(bridge) =>
114+
Effect.try({
115+
try: () => bridge.cleanup(),
116+
catch: (cause) =>
117+
new DesktopClerkBridgeCleanupError({
118+
stateDir: environment.stateDir,
119+
isDevelopment: environment.isDevelopment,
120+
cause,
121+
}),
122+
}).pipe(Effect.orDie),
123+
).pipe(Effect.map((bridge) => bridge.isPrimaryInstance));
121124

122125
return DesktopClerk.of({
123126
configure: Effect.gen(function* () {
@@ -131,7 +134,7 @@ export const make = Effect.gen(function* () {
131134
// forwarded to the running app. In a secondary instance the bridge has
132135
// already begun quitting the app; app.quit() is asynchronous, so stop
133136
// bootstrap here before whenReady can fire.
134-
if (!bridge.isPrimaryInstance) {
137+
if (!isPrimaryInstance) {
135138
yield* electronApp.quit;
136139
return yield* Effect.interrupt;
137140
}

‎apps/desktop/src/app/DesktopEnvironment.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,34 @@ describe("DesktopEnvironment", () => {
120120
}),
121121
);
122122

123+
it.effect("drops inherited OTLP destinations and headers in an isolation profile", () =>
124+
Effect.gen(function* () {
125+
const root = "/tmp/t3-profile";
126+
const environment = yield* makeEnvironment(
127+
{
128+
isolationProfile: {
129+
root,
130+
homeDirectory: root,
131+
appDataDirectory: `${root}/Library/Application Support`,
132+
userDataDirectory: `${root}/userData`,
133+
sessionDataDirectory: `${root}/sessionData`,
134+
t3Home: `${root}/.t3`,
135+
},
136+
},
137+
{
138+
T3CODE_OTLP_TRACES_URL: "https://outside.invalid/traces",
139+
T3CODE_OTLP_HEADERS: "authorization=Bearer%20account-token",
140+
},
141+
);
142+
143+
assert.deepEqual(environment.otlpTracesUrl, Option.none());
144+
assert.deepEqual(environment.otlpMetricsUrl, Option.none());
145+
assert.deepEqual(environment.otlpLogsUrl, Option.none());
146+
assert.deepEqual(environment.otlpHeaders, Option.none());
147+
assert.equal(environment.baseDir, `${root}/.t3`);
148+
}),
149+
);
150+
123151
it.effect("uses the packaged Windows server sidecar as the backend root", () =>
124152
Effect.gen(function* () {
125153
const environment = yield* makeEnvironment({

‎apps/desktop/src/app/DesktopEnvironment.ts‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as Effect from "effect/Effect";
1010
import * as Layer from "effect/Layer";
1111
import * as Option from "effect/Option";
1212
import * as Path from "effect/Path";
13+
import type { IsolationProfilePaths } from "@t3tools/shared/isolationRoot";
1314

1415
import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
1516
import * as DesktopConfig from "./DesktopConfig.ts";
@@ -28,6 +29,7 @@ export interface MakeDesktopEnvironmentInput {
2829
readonly isPackaged: boolean;
2930
readonly resourcesPath: string;
3031
readonly runningUnderArm64Translation: boolean;
32+
readonly isolationProfile?: IsolationProfilePaths;
3133
}
3234

3335
export class DesktopEnvironment extends Context.Service<
@@ -43,6 +45,8 @@ export class DesktopEnvironment extends Context.Service<
4345
readonly appPath: string;
4446
readonly resourcesPath: string;
4547
readonly homeDirectory: string;
48+
readonly isolationProfile: IsolationProfilePaths | undefined;
49+
readonly userDataDirectory: string;
4650
readonly appDataDirectory: string;
4751
readonly baseDir: string;
4852
readonly stateDir: string;
@@ -154,20 +158,24 @@ const make = Effect.fn("desktop.environment.make")(function* (
154158
const path = yield* Path.Path;
155159
const config = yield* DesktopConfig.DesktopConfig;
156160
const homeDirectory = input.homeDirectory;
161+
const isolationProfile = input.isolationProfile;
157162
const devServerUrl = config.devServerUrl;
158163
const isDevelopment = Option.isSome(devServerUrl);
159164
const appDataDirectory =
160-
input.platform === "win32"
165+
isolationProfile?.appDataDirectory ??
166+
(input.platform === "win32"
161167
? Option.getOrElse(config.appDataDirectory, () =>
162168
path.join(homeDirectory, "AppData", "Roaming"),
163169
)
164170
: input.platform === "darwin"
165171
? path.join(homeDirectory, "Library", "Application Support")
166-
: Option.getOrElse(config.xdgConfigHome, () => path.join(homeDirectory, ".config"));
172+
: Option.getOrElse(config.xdgConfigHome, () => path.join(homeDirectory, ".config")));
173+
const effectiveT3Home =
174+
isolationProfile === undefined ? config.t3Home : Option.some(isolationProfile.t3Home);
167175
const baseDir = resolveDesktopBaseDir({
168176
homeDirectory,
169177
joinPath: path.join,
170-
t3Home: config.t3Home,
178+
t3Home: effectiveT3Home,
171179
});
172180
const rootDir = path.resolve(input.dirname, "../../..");
173181
const appRoot = input.isPackaged ? input.appPath : rootDir;
@@ -184,7 +192,7 @@ const make = Effect.fn("desktop.environment.make")(function* (
184192
baseDir,
185193
isDevelopment,
186194
joinPath: path.join,
187-
t3Home: config.t3Home,
195+
t3Home: effectiveT3Home,
188196
});
189197
const userDataDirName = isDevelopment ? "t3code-dev" : "t3code";
190198
const legacyUserDataDirName = isDevelopment ? "T3 Code (Dev)" : "T3 Code (Alpha)";
@@ -205,6 +213,9 @@ const make = Effect.fn("desktop.environment.make")(function* (
205213
appPath: input.appPath,
206214
resourcesPath,
207215
homeDirectory,
216+
isolationProfile,
217+
userDataDirectory:
218+
isolationProfile?.userDataDirectory ?? path.join(appDataDirectory, userDataDirName),
208219
appDataDirectory,
209220
baseDir,
210221
stateDir,
@@ -228,11 +239,11 @@ const make = Effect.fn("desktop.environment.make")(function* (
228239
devRemoteT3ServerEntryPath: config.devRemoteT3ServerEntryPath,
229240
configuredBackendPort: config.configuredBackendPort,
230241
commitHashOverride: config.commitHashOverride,
231-
otlpTracesUrl: config.otlpTracesUrl,
232-
otlpMetricsUrl: config.otlpMetricsUrl,
233-
otlpLogsUrl: config.otlpLogsUrl,
242+
otlpTracesUrl: isolationProfile === undefined ? config.otlpTracesUrl : Option.none(),
243+
otlpMetricsUrl: isolationProfile === undefined ? config.otlpMetricsUrl : Option.none(),
244+
otlpLogsUrl: isolationProfile === undefined ? config.otlpLogsUrl : Option.none(),
234245
otlpExportIntervalMs: config.otlpExportIntervalMs,
235-
otlpHeaders: config.otlpHeaders,
246+
otlpHeaders: isolationProfile === undefined ? config.otlpHeaders : Option.none(),
236247
otlpProtocol: config.otlpProtocol,
237248
branding,
238249
displayName,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as Electron from "electron";
2+
import * as NodeOS from "node:os";
3+
4+
import {
5+
ISOLATION_ROOT_ENV,
6+
resolveDesktopIsolationProfile,
7+
setElectronIsolationPaths,
8+
} from "@t3tools/shared/isolationRoot";
9+
10+
export const isolationProfile = resolveDesktopIsolationProfile({
11+
argv: process.argv,
12+
isPackaged: Electron.app.isPackaged,
13+
// oxlint-disable-next-line t3code/no-global-process-runtime -- The synchronous pre-ready bootstrap must reject unsupported OS targets before Effect services can start.
14+
platform: NodeOS.platform(),
15+
});
16+
17+
if (isolationProfile !== undefined) {
18+
// This app-owned marker is propagated only to owned child processes. It does
19+
// not rewrite HOME or CODEX_HOME and is always resolved from our CLI option.
20+
process.env[ISOLATION_ROOT_ENV] = isolationProfile.root;
21+
// Electron's session and single-instance storage paths are selected during
22+
// startup, before the Clerk bridge and BrowserWindow can initialize them.
23+
setElectronIsolationPaths((name, value) => Electron.app.setPath(name, value), isolationProfile);
24+
}
25+
26+
export const homeDirectory = (): string => isolationProfile?.homeDirectory ?? NodeOS.homedir();

‎apps/desktop/src/app/DesktopLifecycle.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ function makeElectronAppLayer(
3535
name: Effect.succeed("T3 Code"),
3636
systemLocale: Effect.succeed("en-US"),
3737
whenReady: Effect.void,
38+
requestSingleInstanceLock: Effect.succeed(true),
3839
quit,
3940
exit: () => Effect.void,
4041
relaunch: () => Effect.void,

‎apps/desktop/src/app/DesktopPreReadyPlatform.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// @effect-diagnostics nodeBuiltinImport:off - pre-ready Electron setup reads settings and prepares the Linux desktop entry synchronously before app services are available.
22
import * as NodeFS from "node:fs";
3-
import * as NodeOS from "node:os";
43
import * as NodePath from "node:path";
54
import * as Context from "effect/Context";
65
import * as Effect from "effect/Effect";
@@ -10,6 +9,7 @@ import * as Electron from "electron";
109
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
1110

1211
import * as DesktopEarlyElectronStartup from "./DesktopEarlyElectronStartup.ts";
12+
import * as DesktopIsolationProfile from "./DesktopIsolationProfile.ts";
1313
import { resolveDesktopAppBranding } from "./DesktopEnvironment.ts";
1414
import { renderUrlHandlerDesktopEntry } from "./DesktopLinuxUrlHandler.ts";
1515
import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
@@ -35,7 +35,7 @@ export const resolveEarlyLinuxElectronOptionsFromProcess =
3535
(): DesktopEarlyElectronStartup.EarlyLinuxElectronOptions =>
3636
DesktopEarlyElectronStartup.resolveEarlyLinuxElectronOptions({
3737
env: process.env,
38-
homeDirectory: NodeOS.homedir(),
38+
homeDirectory: DesktopIsolationProfile.homeDirectory(),
3939
joinPath: NodePath.posix.join,
4040
readFileString: (path) => NodeFS.readFileSync(path, "utf8"),
4141
});
@@ -64,7 +64,7 @@ export const make = Effect.gen(function* () {
6464
try {
6565
const applicationsDir = NodePath.posix.join(
6666
process.env.XDG_DATA_HOME?.trim() ||
67-
NodePath.posix.join(NodeOS.homedir(), ".local", "share"),
67+
NodePath.posix.join(DesktopIsolationProfile.homeDirectory(), ".local", "share"),
6868
"applications",
6969
);
7070
NodeFS.mkdirSync(applicationsDir, { recursive: true });

0 commit comments

Comments
 (0)