Files
oh-my-opencode/src/shared/opencode-config-dir.ts
T
YeonGyu-Kim d7c2b6249b fix(config): fall back to legacy path when migration fails and use canonical basename everywhere (#3133)
Root cause: loadPluginConfig() unconditionally switched userConfigPath to the
canonical name after calling migrateLegacyConfigFile(), even when migration
failed (e.g. file lock on Windows, permission denied). This left the config
path pointing to a non-existent file, so the plugin config silently loaded
as empty defaults.

Additionally, several fallback/default paths were hardcoded to the legacy
'oh-my-opencode' basename instead of using CONFIG_BASENAME ('oh-my-openagent'),
causing CLI config commands (writeOmoConfig, detectCurrentConfig) to write to
the wrong filename.

Changes:
- plugin-config.ts: check migrateLegacyConfigFile() return value; only switch
  to canonical path if migration succeeded OR the canonical file already exists
- opencode-config-dir.ts: use CONFIG_BASENAME for omoConfig path in
  getOpenCodeConfigPaths()
- config-context.ts: getOmoConfigPath() now uses detectPluginConfigFile() to
  find whichever name variant actually exists on disk
- plugin-config.ts: default fallback paths use CONFIG_BASENAME instead of
  hardcoded legacy name
- Added test: loadPluginConfig still loads config when migration fails
  (read-only directory simulation)
2026-04-05 14:27:24 +09:00

136 lines
3.8 KiB
TypeScript

import { existsSync, realpathSync } from "node:fs"
import { homedir } from "node:os"
import { join, resolve, win32 } from "node:path"
import { CONFIG_BASENAME } from "./plugin-identity"
import type {
OpenCodeBinaryType,
OpenCodeConfigDirOptions,
OpenCodeConfigPaths,
} from "./opencode-config-dir-types"
export type {
OpenCodeBinaryType,
OpenCodeConfigDirOptions,
OpenCodeConfigPaths,
} from "./opencode-config-dir-types"
export const TAURI_APP_IDENTIFIER = "ai.opencode.desktop"
export const TAURI_APP_IDENTIFIER_DEV = "ai.opencode.desktop.dev"
export function isDevBuild(version: string | null | undefined): boolean {
if (!version) return false
return version.includes("-dev") || version.includes(".dev")
}
function getTauriConfigDir(identifier: string): string {
const platform = process.platform
switch (platform) {
case "darwin":
return join(homedir(), "Library", "Application Support", identifier)
case "win32": {
const appData = process.env.APPDATA || join(homedir(), "AppData", "Roaming")
return win32.join(appData, identifier)
}
case "linux":
default: {
const xdgConfig = process.env.XDG_CONFIG_HOME || join(homedir(), ".config")
return join(xdgConfig, identifier)
}
}
}
function resolveConfigPath(pathValue: string): string {
const resolvedPath = resolve(pathValue)
if (!existsSync(resolvedPath)) return resolvedPath
try {
return realpathSync(resolvedPath)
} catch {
return resolvedPath
}
}
function getCliConfigDir(): string {
const envConfigDir = process.env.OPENCODE_CONFIG_DIR?.trim()
if (envConfigDir) {
return resolveConfigPath(envConfigDir)
}
const xdgConfig = process.env.XDG_CONFIG_HOME || join(homedir(), ".config")
return resolveConfigPath(join(xdgConfig, "opencode"))
}
export function getOpenCodeConfigDir(options: OpenCodeConfigDirOptions): string {
const { binary, version, checkExisting = true } = options
if (binary === "opencode") {
return getCliConfigDir()
}
const identifier = isDevBuild(version) ? TAURI_APP_IDENTIFIER_DEV : TAURI_APP_IDENTIFIER
const tauriDirBase = getTauriConfigDir(identifier)
const tauriDir = process.platform === "win32"
? (win32.isAbsolute(tauriDirBase) ? win32.normalize(tauriDirBase) : win32.resolve(tauriDirBase))
: resolveConfigPath(tauriDirBase)
if (checkExisting) {
const legacyDir = getCliConfigDir()
const legacyConfig = join(legacyDir, "opencode.json")
const legacyConfigC = join(legacyDir, "opencode.jsonc")
if (existsSync(legacyConfig) || existsSync(legacyConfigC)) {
return legacyDir
}
}
return tauriDir
}
export function getOpenCodeConfigPaths(options: OpenCodeConfigDirOptions): OpenCodeConfigPaths {
const configDir = getOpenCodeConfigDir(options)
return {
configDir,
configJson: join(configDir, "opencode.json"),
configJsonc: join(configDir, "opencode.jsonc"),
packageJson: join(configDir, "package.json"),
omoConfig: join(configDir, `${CONFIG_BASENAME}.json`),
}
}
export function detectExistingConfigDir(binary: OpenCodeBinaryType, version?: string | null): string | null {
const locations: string[] = []
const envConfigDir = process.env.OPENCODE_CONFIG_DIR?.trim()
if (envConfigDir) {
locations.push(resolveConfigPath(envConfigDir))
}
if (binary === "opencode-desktop") {
const identifier = isDevBuild(version) ? TAURI_APP_IDENTIFIER_DEV : TAURI_APP_IDENTIFIER
locations.push(getTauriConfigDir(identifier))
if (isDevBuild(version)) {
locations.push(getTauriConfigDir(TAURI_APP_IDENTIFIER))
}
}
locations.push(getCliConfigDir())
for (const dir of locations) {
const configJson = join(dir, "opencode.json")
const configJsonc = join(dir, "opencode.jsonc")
if (existsSync(configJson) || existsSync(configJsonc)) {
return dir
}
}
return null
}