Skip to content

Commit b34ca44

Browse files
authored
fix incorrect config directory by lazily loading electron-store (anomalyco#23373)
1 parent 40ba8f3 commit b34ca44

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

packages/desktop-electron/src/main/migrate.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { existsSync, readdirSync, readFileSync } from "node:fs"
44
import { homedir } from "node:os"
55
import { join } from "node:path"
66
import { CHANNEL } from "./constants"
7-
import { getStore, store } from "./store"
7+
import { getStore } from "./store"
88

99
const TAURI_MIGRATED_KEY = "tauriMigrated"
1010

@@ -67,7 +67,7 @@ function migrateFile(datPath: string, filename: string) {
6767
}
6868

6969
export function migrate() {
70-
if (store.get(TAURI_MIGRATED_KEY)) {
70+
if (getStore().get(TAURI_MIGRATED_KEY)) {
7171
log.log("tauri migration: already done, skipping")
7272
return
7373
}
@@ -77,7 +77,7 @@ export function migrate() {
7777

7878
if (!existsSync(dir)) {
7979
log.log("tauri migration: no tauri data directory found, nothing to migrate")
80-
store.set(TAURI_MIGRATED_KEY, true)
80+
getStore().set(TAURI_MIGRATED_KEY, true)
8181
return
8282
}
8383

@@ -87,5 +87,5 @@ export function migrate() {
8787
}
8888

8989
log.log("tauri migration: complete")
90-
store.set(TAURI_MIGRATED_KEY, true)
90+
getStore().set(TAURI_MIGRATED_KEY, true)
9191
}

packages/desktop-electron/src/main/server.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
import { app } from "electron"
22
import { DEFAULT_SERVER_URL_KEY, WSL_ENABLED_KEY } from "./constants"
33
import { getUserShell, loadShellEnv } from "./shell-env"
4-
import { store } from "./store"
4+
import { getStore } from "./store"
55

66
export type WslConfig = { enabled: boolean }
77

88
export type HealthCheck = { wait: Promise<void> }
99

1010
export function getDefaultServerUrl(): string | null {
11-
const value = store.get(DEFAULT_SERVER_URL_KEY)
11+
const value = getStore().get(DEFAULT_SERVER_URL_KEY)
1212
return typeof value === "string" ? value : null
1313
}
1414

1515
export function setDefaultServerUrl(url: string | null) {
1616
if (url) {
17-
store.set(DEFAULT_SERVER_URL_KEY, url)
17+
getStore().set(DEFAULT_SERVER_URL_KEY, url)
1818
return
1919
}
2020

21-
store.delete(DEFAULT_SERVER_URL_KEY)
21+
getStore().delete(DEFAULT_SERVER_URL_KEY)
2222
}
2323

2424
export function getWslConfig(): WslConfig {
25-
const value = store.get(WSL_ENABLED_KEY)
25+
const value = getStore().get(WSL_ENABLED_KEY)
2626
return { enabled: typeof value === "boolean" ? value : false }
2727
}
2828

2929
export function setWslConfig(config: WslConfig) {
30-
store.set(WSL_ENABLED_KEY, config.enabled)
30+
getStore().set(WSL_ENABLED_KEY, config.enabled)
3131
}
3232

3333
export async function spawnLocalServer(hostname: string, port: number, password: string) {

packages/desktop-electron/src/main/store.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import { SETTINGS_STORE } from "./constants"
44

55
const cache = new Map<string, Store>()
66

7+
// We cannot instantiate the electron-store at module load time because
8+
// module import hoisting causes this to run before app.setPath("userData", ...)
9+
// in index.ts has executed, which would result in files being written to the default directory
10+
// (e.g. bad: %APPDATA%\@opencode-ai\desktop-electron\opencode.settings vs good: %APPDATA%\ai.opencode.desktop.dev\opencode.settings).
711
export function getStore(name = SETTINGS_STORE) {
812
const cached = cache.get(name)
913
if (cached) return cached
1014
const next = new Store({ name, fileExtension: "", accessPropertiesByDotNotation: false })
1115
cache.set(name, next)
1216
return next
1317
}
14-
15-
export const store = getStore(SETTINGS_STORE)

0 commit comments

Comments
 (0)