Skip to content

Commit 4efccc8

Browse files
committed
fix(desktop): keep userData under OS app-data keyed by appId [anomalyco#233]
1 parent 655154c commit 4efccc8

3 files changed

Lines changed: 86 additions & 16 deletions

File tree

‎packages/ellamaka-desktop/src/main/index.ts‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { EventEmitter } from "node:events"
33
import * as http from "node:http"
44
import { createServer } from "node:net"
55
import { homedir } from "node:os"
6-
import { join } from "node:path"
76
import { getCACertificates, setDefaultCACertificates } from "node:tls"
87
import type { Event } from "electron"
98
import { app, BrowserWindow } from "electron"
@@ -53,18 +52,14 @@ import { checkUpdate, checkForUpdates, installUpdate, setupAutoUpdater } from ".
5352
import { probeWopalHomeFromShell } from "./onboarding-gate"
5453
import { isVmwareVirtualGpu } from "./gpu-detect"
5554
import { recoverMainWindow } from "./window-show-guard"
55+
import { resolveAppId, resolveUserDataPath } from "./user-data"
5656
import { Deferred, Effect, Fiber } from "effect"
5757

5858
const APP_NAMES: Record<string, string> = {
5959
main: "Ellamaka Main",
6060
beta: "Ellamaka Beta",
6161
stable: "Ellamaka",
6262
}
63-
const APP_IDS: Record<string, string> = {
64-
main: "ai.ellamaka.desktop.main",
65-
beta: "ai.ellamaka.desktop.beta",
66-
stable: "ai.ellamaka.desktop",
67-
}
6863
const jsCallStackFeature = "DocumentPolicyIncludeJSCallStacksInCrashReports"
6964

7065
let logger: ReturnType<typeof initLogging>
@@ -327,18 +322,15 @@ const main = Effect.gen(function* () {
327322
process.chdir(homedir())
328323
} catch {}
329324

330-
const appId = app.isPackaged ? APP_IDS[CHANNEL] : `ai.ellamaka.desktop.${CHANNEL}`
331-
// Electron userData (electron-store) does not follow WOPAL_HOME. When
332-
// WOPAL_HOME is customized (dev.sh desktop sandbox), isolate userData under
333-
// it so dev runs never mutate the real app's settings.
334-
const devUserDataRoot = process.env.WOPAL_HOME ? join(process.env.WOPAL_HOME, "ellamaka", "desktop") : undefined
325+
const appId = resolveAppId(app.isPackaged, CHANNEL)
335326
app.setName(app.isPackaged ? APP_NAMES[CHANNEL] : "Ellamaka Dev")
336327
app.setAppUserModelId(appId)
337-
app.setPath(
338-
"userData",
339-
devUserDataRoot ?? join(app.getPath("appData"), appId),
340-
)
341-
if (devUserDataRoot) app.setPath("sessionData", join(devUserDataRoot, "session"))
328+
// Electron userData always lives under the OS standard app-data directory,
329+
// keyed by appId. Development builds use a distinct appId
330+
// (`ai.ellamaka.desktop.<channel>` vs `ai.ellamaka.desktop.local`), so dev
331+
// runs never touch the packaged app's settings without redirecting userData
332+
// into WOPAL_HOME — see #233 for the regression this avoids.
333+
app.setPath("userData", resolveUserDataPath(app.isPackaged, app.getPath("appData"), CHANNEL))
342334
logger = initLogging()
343335
initCrashReporter()
344336

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { join } from "node:path"
3+
4+
import { resolveAppId, resolveUserDataPath } from "./user-data"
5+
6+
describe("resolveAppId", () => {
7+
test("packaged builds use the channel-specific app id", () => {
8+
expect(resolveAppId(true, "stable")).toBe("ai.ellamaka.desktop")
9+
expect(resolveAppId(true, "beta")).toBe("ai.ellamaka.desktop.beta")
10+
expect(resolveAppId(true, "main")).toBe("ai.ellamaka.desktop.main")
11+
})
12+
13+
test("development builds use the local channel suffix", () => {
14+
expect(resolveAppId(false, "local")).toBe("ai.ellamaka.desktop.local")
15+
})
16+
17+
test("unknown channels fall back to the channel-suffixed id", () => {
18+
expect(resolveAppId(true, "future")).toBe("ai.ellamaka.desktop.future")
19+
expect(resolveAppId(false, "future")).toBe("ai.ellamaka.desktop.future")
20+
})
21+
})
22+
23+
describe("resolveUserDataPath", () => {
24+
test("packaged userData always lives under the OS app-data directory", () => {
25+
expect(resolveUserDataPath(true, "/Users/me/Library/Application Support", "beta")).toBe(
26+
join("/Users/me/Library/Application Support", "ai.ellamaka.desktop.beta"),
27+
)
28+
})
29+
30+
test("development userData is appId-scoped but still under app-data", () => {
31+
expect(resolveUserDataPath(false, "/Users/me/Library/Application Support", "local")).toBe(
32+
join("/Users/me/Library/Application Support", "ai.ellamaka.desktop.local"),
33+
)
34+
})
35+
36+
test("packaged and development paths never collide", () => {
37+
const appData = "/Users/me/Library/Application Support"
38+
expect(resolveUserDataPath(true, appData, "beta")).not.toBe(resolveUserDataPath(false, appData, "local"))
39+
})
40+
41+
test("is independent of WOPAL_HOME", () => {
42+
const appData = "/Users/me/Library/Application Support"
43+
const saved = process.env.WOPAL_HOME
44+
try {
45+
process.env.WOPAL_HOME = "/Users/me/.wopal"
46+
expect(resolveUserDataPath(true, appData, "beta")).toBe(join(appData, "ai.ellamaka.desktop.beta"))
47+
} finally {
48+
if (saved === undefined) delete process.env.WOPAL_HOME
49+
else process.env.WOPAL_HOME = saved
50+
}
51+
})
52+
})
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { join } from "node:path"
2+
3+
// Desktop app identity. Packaged builds resolve to the channel-specific
4+
// application id; development builds (electron-vite dev, app.isPackaged ===
5+
// false) use the `local` suffix so dev runs never mutate a packaged app's
6+
// settings. See #233 — userData must never be redirected based on WOPAL_HOME,
7+
// only separated by appId.
8+
export const APP_IDS: Record<string, string> = {
9+
main: "ai.ellamaka.desktop.main",
10+
beta: "ai.ellamaka.desktop.beta",
11+
stable: "ai.ellamaka.desktop",
12+
}
13+
14+
export function resolveAppId(isPackaged: boolean, channel: string): string {
15+
if (isPackaged) return APP_IDS[channel] ?? `ai.ellamaka.desktop.${channel}`
16+
return `ai.ellamaka.desktop.${channel}`
17+
}
18+
19+
// The Electron userData directory always lives under the OS standard
20+
// app-data path, keyed by appId. It is independent of WOPAL_HOME: both
21+
// packaged and development builds read/write their own appId-scoped path,
22+
// which keeps CLI (env-inheriting) and Finder/Dock (cold-start) launches on
23+
// the exact same path.
24+
export function resolveUserDataPath(isPackaged: boolean, appData: string, channel: string): string {
25+
return join(appData, resolveAppId(isPackaged, channel))
26+
}

0 commit comments

Comments
 (0)