From ba851f4ae95144c995c35c0a678c844964760a36 Mon Sep 17 00:00:00 2001 From: digua Date: Tue, 19 May 2026 20:39:25 +0800 Subject: [PATCH] fix(electron): fix data migration path mismatch after package extraction The extraction of Electron code to apps/desktop/ changed the package name from "ChatLab" to "@openchatlab/desktop", which caused app.getPath('userData') to return a wrong path in dev mode. This made the migration system unable to find existing databases, resulting in a fresh-app experience for upgrading users. - Add productName "ChatLab" to desktop package.json to ensure correct userData path - Add verifyDataPath() safety net: auto-correct config.toml when configured dir has no databases but old Electron path does --- apps/desktop/main/index.ts | 4 ++++ apps/desktop/main/paths.ts | 30 ++++++++++++++++++++++++++++++ apps/desktop/package.json | 1 + 3 files changed, 35 insertions(+) diff --git a/apps/desktop/main/index.ts b/apps/desktop/main/index.ts index 2ae8ff91..7dfd4932 100644 --- a/apps/desktop/main/index.ts +++ b/apps/desktop/main/index.ts @@ -12,6 +12,7 @@ import { cleanupPendingDeleteDir, needsUnifiedDirMigration, migrateToUnifiedDirs, + verifyDataPath, getSystemDataDir, getAiDataDir, } from './paths' @@ -96,6 +97,9 @@ class MainProcess { this.migrateToUnifiedDirsIfNeeded() } + // 验证数据路径是否正确(安全网:防止 config.toml 指向空目录) + verifyDataPath() + // 确保应用目录存在 ensureAppDirs() diff --git a/apps/desktop/main/paths.ts b/apps/desktop/main/paths.ts index abd1ede4..04b68762 100644 --- a/apps/desktop/main/paths.ts +++ b/apps/desktop/main/paths.ts @@ -720,3 +720,33 @@ export function migrateToUnifiedDirs(): { success: boolean; error?: string } { return { success: false, error: errorMsg } } } + +/** + * Verify that the configured user_data_dir actually has database files. + * If the configured dir is empty but the old Electron path has databases, + * auto-correct config.toml to point to the old path. + * + * This acts as a safety net in case migration was skipped or config was overwritten. + */ +export function verifyDataPath(): void { + const currentDir = getUserDataDir() + const currentDbDir = path.join(currentDir, 'databases') + const hasCurrentDbs = fs.existsSync(currentDbDir) && fs.readdirSync(currentDbDir).some((f) => f.endsWith('.db')) + + if (hasCurrentDbs) return + + const oldDir = getElectronLegacyDataDir() + if (path.resolve(currentDir) === path.resolve(oldDir)) return + + const oldDbDir = path.join(oldDir, 'databases') + if (!fs.existsSync(oldDbDir)) return + + const hasOldDbs = fs.readdirSync(oldDbDir).some((f) => f.endsWith('.db')) + if (!hasOldDbs) return + + console.warn( + `[Paths] Data path mismatch: configured dir ${currentDir} has no databases, but old path ${oldDir} does. Auto-correcting config.toml.` + ) + writeConfigField('data', 'user_data_dir', oldDir) + _userDataDir = oldDir +} diff --git a/apps/desktop/package.json b/apps/desktop/package.json index 88f46e57..41f76368 100644 --- a/apps/desktop/package.json +++ b/apps/desktop/package.json @@ -1,6 +1,7 @@ { "name": "@openchatlab/desktop", "version": "0.19.0", + "productName": "ChatLab", "private": true, "description": "ChatLab Desktop — Electron 桌面客户端", "main": "./out/main/index.js",