Просмотр исходного кода

fix incorrect config directory by lazily loading electron-store (#23373)

Luke Parker 4 дней назад
Родитель
Сommit
b34ca44abe

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

@@ -4,7 +4,7 @@ import { existsSync, readdirSync, readFileSync } from "node:fs"
 import { homedir } from "node:os"
 import { join } from "node:path"
 import { CHANNEL } from "./constants"
-import { getStore, store } from "./store"
+import { getStore } from "./store"
 
 const TAURI_MIGRATED_KEY = "tauriMigrated"
 
@@ -67,7 +67,7 @@ function migrateFile(datPath: string, filename: string) {
 }
 
 export function migrate() {
-  if (store.get(TAURI_MIGRATED_KEY)) {
+  if (getStore().get(TAURI_MIGRATED_KEY)) {
     log.log("tauri migration: already done, skipping")
     return
   }
@@ -77,7 +77,7 @@ export function migrate() {
 
   if (!existsSync(dir)) {
     log.log("tauri migration: no tauri data directory found, nothing to migrate")
-    store.set(TAURI_MIGRATED_KEY, true)
+    getStore().set(TAURI_MIGRATED_KEY, true)
     return
   }
 
@@ -87,5 +87,5 @@ export function migrate() {
   }
 
   log.log("tauri migration: complete")
-  store.set(TAURI_MIGRATED_KEY, true)
+  getStore().set(TAURI_MIGRATED_KEY, true)
 }

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

@@ -1,33 +1,33 @@
 import { app } from "electron"
 import { DEFAULT_SERVER_URL_KEY, WSL_ENABLED_KEY } from "./constants"
 import { getUserShell, loadShellEnv } from "./shell-env"
-import { store } from "./store"
+import { getStore } from "./store"
 
 export type WslConfig = { enabled: boolean }
 
 export type HealthCheck = { wait: Promise<void> }
 
 export function getDefaultServerUrl(): string | null {
-  const value = store.get(DEFAULT_SERVER_URL_KEY)
+  const value = getStore().get(DEFAULT_SERVER_URL_KEY)
   return typeof value === "string" ? value : null
 }
 
 export function setDefaultServerUrl(url: string | null) {
   if (url) {
-    store.set(DEFAULT_SERVER_URL_KEY, url)
+    getStore().set(DEFAULT_SERVER_URL_KEY, url)
     return
   }
 
-  store.delete(DEFAULT_SERVER_URL_KEY)
+  getStore().delete(DEFAULT_SERVER_URL_KEY)
 }
 
 export function getWslConfig(): WslConfig {
-  const value = store.get(WSL_ENABLED_KEY)
+  const value = getStore().get(WSL_ENABLED_KEY)
   return { enabled: typeof value === "boolean" ? value : false }
 }
 
 export function setWslConfig(config: WslConfig) {
-  store.set(WSL_ENABLED_KEY, config.enabled)
+  getStore().set(WSL_ENABLED_KEY, config.enabled)
 }
 
 export async function spawnLocalServer(hostname: string, port: number, password: string) {

+ 4 - 2
packages/desktop-electron/src/main/store.ts

@@ -4,6 +4,10 @@ import { SETTINGS_STORE } from "./constants"
 
 const cache = new Map<string, Store>()
 
+// We cannot instantiate the electron-store at module load time because
+// module import hoisting causes this to run before app.setPath("userData", ...)
+// in index.ts has executed, which would result in files being written to the default directory
+// (e.g. bad: %APPDATA%\@opencode-ai\desktop-electron\opencode.settings vs good: %APPDATA%\ai.opencode.desktop.dev\opencode.settings).
 export function getStore(name = SETTINGS_STORE) {
   const cached = cache.get(name)
   if (cached) return cached
@@ -11,5 +15,3 @@ export function getStore(name = SETTINGS_STORE) {
   cache.set(name, next)
   return next
 }
-
-export const store = getStore(SETTINGS_STORE)