fix: respect OPENCODE_CONFIG_DIR environment variable across all config paths

Multiple files were hardcoding ~/.config/opencode paths instead of using
getOpenCodeConfigDir() which respects the OPENCODE_CONFIG_DIR env var.

This broke profile isolation features like OCX ghost mode, where users
set OPENCODE_CONFIG_DIR to a custom path but oh-my-opencode.json and
other configs weren't being read from that location.

Changes:
- plugin-config.ts: Use getOpenCodeConfigDir() directly
- cli/doctor/checks: Use getOpenCodeConfigDir() for auth and config checks
- tools/lsp/config.ts: Use getOpenCodeConfigDir() for LSP config paths
- command loaders: Use getOpenCodeConfigDir() for global command dirs
- hooks: Use getOpenCodeConfigDir() for hook config paths
- config-path.ts: Mark getUserConfigDir() as deprecated
- tests: Ensure OPENCODE_CONFIG_DIR is properly isolated in tests
This commit is contained in:
Nguyen Khac Trung Kien 2026-01-22 12:15:09 +07:00
parent 80b4067b8e
commit e65d57285f
11 changed files with 36 additions and 68 deletions

View File

@ -1,11 +1,10 @@
import { existsSync, readFileSync } from "node:fs" import { existsSync, readFileSync } from "node:fs"
import { homedir } from "node:os"
import { join } from "node:path" import { join } from "node:path"
import type { CheckResult, CheckDefinition, AuthProviderInfo, AuthProviderId } from "../types" import type { CheckResult, CheckDefinition, AuthProviderInfo, AuthProviderId } from "../types"
import { CHECK_IDS, CHECK_NAMES } from "../constants" import { CHECK_IDS, CHECK_NAMES } from "../constants"
import { parseJsonc } from "../../../shared" import { parseJsonc, getOpenCodeConfigDir } from "../../../shared"
const OPENCODE_CONFIG_DIR = join(homedir(), ".config", "opencode") const OPENCODE_CONFIG_DIR = getOpenCodeConfigDir({ binary: "opencode" })
const OPENCODE_JSON = join(OPENCODE_CONFIG_DIR, "opencode.json") const OPENCODE_JSON = join(OPENCODE_CONFIG_DIR, "opencode.json")
const OPENCODE_JSONC = join(OPENCODE_CONFIG_DIR, "opencode.jsonc") const OPENCODE_JSONC = join(OPENCODE_CONFIG_DIR, "opencode.jsonc")

View File

@ -1,12 +1,11 @@
import { existsSync, readFileSync } from "node:fs" import { existsSync, readFileSync } from "node:fs"
import { homedir } from "node:os"
import { join } from "node:path" import { join } from "node:path"
import type { CheckResult, CheckDefinition, ConfigInfo } from "../types" import type { CheckResult, CheckDefinition, ConfigInfo } from "../types"
import { CHECK_IDS, CHECK_NAMES, PACKAGE_NAME } from "../constants" import { CHECK_IDS, CHECK_NAMES, PACKAGE_NAME } from "../constants"
import { parseJsonc, detectConfigFile } from "../../../shared" import { parseJsonc, detectConfigFile, getOpenCodeConfigDir } from "../../../shared"
import { OhMyOpenCodeConfigSchema } from "../../../config" import { OhMyOpenCodeConfigSchema } from "../../../config"
const USER_CONFIG_DIR = join(homedir(), ".config", "opencode") const USER_CONFIG_DIR = getOpenCodeConfigDir({ binary: "opencode" })
const USER_CONFIG_BASE = join(USER_CONFIG_DIR, `${PACKAGE_NAME}`) const USER_CONFIG_BASE = join(USER_CONFIG_DIR, `${PACKAGE_NAME}`)
const PROJECT_CONFIG_BASE = join(process.cwd(), ".opencode", PACKAGE_NAME) const PROJECT_CONFIG_BASE = join(process.cwd(), ".opencode", PACKAGE_NAME)

View File

@ -1,10 +1,9 @@
import { promises as fs, type Dirent } from "fs" import { promises as fs, type Dirent } from "fs"
import { join, basename } from "path" import { join, basename } from "path"
import { homedir } from "os"
import { parseFrontmatter } from "../../shared/frontmatter" import { parseFrontmatter } from "../../shared/frontmatter"
import { sanitizeModelField } from "../../shared/model-sanitizer" import { sanitizeModelField } from "../../shared/model-sanitizer"
import { isMarkdownFile } from "../../shared/file-utils" import { isMarkdownFile } from "../../shared/file-utils"
import { getClaudeConfigDir } from "../../shared" import { getClaudeConfigDir, getOpenCodeConfigDir } from "../../shared"
import { log } from "../../shared/logger" import { log } from "../../shared/logger"
import type { CommandScope, CommandDefinition, CommandFrontmatter, LoadedCommand } from "./types" import type { CommandScope, CommandDefinition, CommandFrontmatter, LoadedCommand } from "./types"
@ -122,7 +121,8 @@ export async function loadProjectCommands(): Promise<Record<string, CommandDefin
} }
export async function loadOpencodeGlobalCommands(): Promise<Record<string, CommandDefinition>> { export async function loadOpencodeGlobalCommands(): Promise<Record<string, CommandDefinition>> {
const opencodeCommandsDir = join(homedir(), ".config", "opencode", "command") const configDir = getOpenCodeConfigDir({ binary: "opencode" })
const opencodeCommandsDir = join(configDir, "command")
const commands = await loadCommandsFromDir(opencodeCommandsDir, "opencode") const commands = await loadCommandsFromDir(opencodeCommandsDir, "opencode")
return commandsToRecord(commands) return commandsToRecord(commands)
} }

View File

@ -1,12 +1,12 @@
import { existsSync, readdirSync, readFileSync } from "fs" import { existsSync, readdirSync, readFileSync } from "fs"
import { join, basename, dirname } from "path" import { join, basename, dirname } from "path"
import { homedir } from "os"
import { import {
parseFrontmatter, parseFrontmatter,
resolveCommandsInText, resolveCommandsInText,
resolveFileReferencesInText, resolveFileReferencesInText,
sanitizeModelField, sanitizeModelField,
getClaudeConfigDir, getClaudeConfigDir,
getOpenCodeConfigDir,
} from "../../shared" } from "../../shared"
import type { CommandFrontmatter } from "../../features/claude-code-command-loader/types" import type { CommandFrontmatter } from "../../features/claude-code-command-loader/types"
import { isMarkdownFile } from "../../shared/file-utils" import { isMarkdownFile } from "../../shared/file-utils"
@ -101,9 +101,10 @@ export interface ExecutorOptions {
} }
async function discoverAllCommands(options?: ExecutorOptions): Promise<CommandInfo[]> { async function discoverAllCommands(options?: ExecutorOptions): Promise<CommandInfo[]> {
const configDir = getOpenCodeConfigDir({ binary: "opencode" })
const userCommandsDir = join(getClaudeConfigDir(), "commands") const userCommandsDir = join(getClaudeConfigDir(), "commands")
const projectCommandsDir = join(process.cwd(), ".claude", "commands") const projectCommandsDir = join(process.cwd(), ".claude", "commands")
const opencodeGlobalDir = join(homedir(), ".config", "opencode", "command") const opencodeGlobalDir = join(configDir, "command")
const opencodeProjectDir = join(process.cwd(), ".opencode", "command") const opencodeProjectDir = join(process.cwd(), ".opencode", "command")
const userCommands = discoverCommandsFromDir(userCommandsDir, "user") const userCommands = discoverCommandsFromDir(userCommandsDir, "user")

View File

@ -1,16 +1,12 @@
import * as path from "node:path" import * as path from "node:path"
import * as os from "node:os" import * as os from "node:os"
import * as fs from "node:fs" import * as fs from "node:fs"
import { getOpenCodeConfigDir } from "../../shared"
export const PACKAGE_NAME = "oh-my-opencode" export const PACKAGE_NAME = "oh-my-opencode"
export const NPM_REGISTRY_URL = `https://registry.npmjs.org/-/package/${PACKAGE_NAME}/dist-tags` export const NPM_REGISTRY_URL = `https://registry.npmjs.org/-/package/${PACKAGE_NAME}/dist-tags`
export const NPM_FETCH_TIMEOUT = 5000 export const NPM_FETCH_TIMEOUT = 5000
/**
* OpenCode plugin cache directory
* - Linux/macOS: ~/.cache/opencode/
* - Windows: %LOCALAPPDATA%/opencode/
*/
function getCacheDir(): string { function getCacheDir(): string {
if (process.platform === "win32") { if (process.platform === "win32") {
return path.join(process.env.LOCALAPPDATA ?? os.homedir(), "opencode") return path.join(process.env.LOCALAPPDATA ?? os.homedir(), "opencode")
@ -27,38 +23,11 @@ export const INSTALLED_PACKAGE_JSON = path.join(
"package.json" "package.json"
) )
/**
* OpenCode config file locations (priority order)
* On Windows, checks ~/.config first (cross-platform), then %APPDATA% (fallback)
* This matches shared/config-path.ts behavior for consistency
*/
function getUserConfigDir(): string {
if (process.platform === "win32") {
const crossPlatformDir = path.join(os.homedir(), ".config")
const appdataDir = process.env.APPDATA ?? path.join(os.homedir(), "AppData", "Roaming")
// Check cross-platform path first (~/.config)
const crossPlatformConfig = path.join(crossPlatformDir, "opencode", "opencode.json")
const crossPlatformConfigJsonc = path.join(crossPlatformDir, "opencode", "opencode.jsonc")
if (fs.existsSync(crossPlatformConfig) || fs.existsSync(crossPlatformConfigJsonc)) {
return crossPlatformDir
}
// Fall back to %APPDATA%
return appdataDir
}
return process.env.XDG_CONFIG_HOME ?? path.join(os.homedir(), ".config")
}
/**
* Get the Windows-specific APPDATA directory (for fallback checks)
*/
export function getWindowsAppdataDir(): string | null { export function getWindowsAppdataDir(): string | null {
if (process.platform !== "win32") return null if (process.platform !== "win32") return null
return process.env.APPDATA ?? path.join(os.homedir(), "AppData", "Roaming") return process.env.APPDATA ?? path.join(os.homedir(), "AppData", "Roaming")
} }
export const USER_CONFIG_DIR = getUserConfigDir() export const USER_CONFIG_DIR = getOpenCodeConfigDir({ binary: "opencode" })
export const USER_OPENCODE_CONFIG = path.join(USER_CONFIG_DIR, "opencode", "opencode.json") export const USER_OPENCODE_CONFIG = path.join(USER_CONFIG_DIR, "opencode.json")
export const USER_OPENCODE_CONFIG_JSONC = path.join(USER_CONFIG_DIR, "opencode", "opencode.jsonc") export const USER_OPENCODE_CONFIG_JSONC = path.join(USER_CONFIG_DIR, "opencode.jsonc")

View File

@ -1,8 +1,8 @@
import { existsSync } from "fs" import { existsSync } from "fs"
import { homedir } from "os"
import { join } from "path" import { join } from "path"
import type { ClaudeHookEvent } from "./types" import type { ClaudeHookEvent } from "./types"
import { log } from "../../shared/logger" import { log } from "../../shared/logger"
import { getOpenCodeConfigDir } from "../../shared"
export interface DisabledHooksConfig { export interface DisabledHooksConfig {
Stop?: string[] Stop?: string[]
@ -16,7 +16,7 @@ export interface PluginExtendedConfig {
disabledHooks?: DisabledHooksConfig disabledHooks?: DisabledHooksConfig
} }
const USER_CONFIG_PATH = join(homedir(), ".config", "opencode", "opencode-cc-plugin.json") const USER_CONFIG_PATH = join(getOpenCodeConfigDir({ binary: "opencode" }), "opencode-cc-plugin.json")
function getProjectConfigPath(): string { function getProjectConfigPath(): string {
return join(process.cwd(), ".opencode", "opencode-cc-plugin.json") return join(process.cwd(), ".opencode", "opencode-cc-plugin.json")

View File

@ -4,7 +4,7 @@ import { OhMyOpenCodeConfigSchema, type OhMyOpenCodeConfig } from "./config";
import { import {
log, log,
deepMerge, deepMerge,
getUserConfigDir, getOpenCodeConfigDir,
addConfigLoadError, addConfigLoadError,
parseJsonc, parseJsonc,
detectConfigFile, detectConfigFile,
@ -94,12 +94,9 @@ export function loadPluginConfig(
directory: string, directory: string,
ctx: unknown ctx: unknown
): OhMyOpenCodeConfig { ): OhMyOpenCodeConfig {
// User-level config path (OS-specific) - prefer .jsonc over .json // User-level config path - prefer .jsonc over .json
const userBasePath = path.join( const configDir = getOpenCodeConfigDir({ binary: "opencode" });
getUserConfigDir(), const userBasePath = path.join(configDir, "oh-my-opencode");
"opencode",
"oh-my-opencode"
);
const userDetected = detectConfigFile(userBasePath); const userDetected = detectConfigFile(userBasePath);
const userConfigPath = const userConfigPath =
userDetected.format !== "none" userDetected.format !== "none"

View File

@ -4,11 +4,7 @@ import * as fs from "fs"
/** /**
* Returns the user-level config directory based on the OS. * Returns the user-level config directory based on the OS.
* - Linux/macOS: XDG_CONFIG_HOME or ~/.config * @deprecated Use getOpenCodeConfigDir() from opencode-config-dir.ts instead.
* - Windows: Checks ~/.config first (cross-platform), then %APPDATA% (fallback)
*
* On Windows, prioritizes ~/.config for cross-platform consistency.
* Falls back to %APPDATA% for backward compatibility with existing installations.
*/ */
export function getUserConfigDir(): string { export function getUserConfigDir(): string {
if (process.platform === "win32") { if (process.platform === "win32") {

View File

@ -144,6 +144,7 @@ describe("opencode-config-dir", () => {
// #given opencode CLI binary detected, platform is Linux // #given opencode CLI binary detected, platform is Linux
Object.defineProperty(process, "platform", { value: "linux" }) Object.defineProperty(process, "platform", { value: "linux" })
delete process.env.XDG_CONFIG_HOME delete process.env.XDG_CONFIG_HOME
delete process.env.OPENCODE_CONFIG_DIR
// #when getOpenCodeConfigDir is called with binary="opencode" // #when getOpenCodeConfigDir is called with binary="opencode"
const result = getOpenCodeConfigDir({ binary: "opencode", version: "1.0.200" }) const result = getOpenCodeConfigDir({ binary: "opencode", version: "1.0.200" })
@ -156,6 +157,7 @@ describe("opencode-config-dir", () => {
// #given opencode CLI binary detected, platform is Linux with XDG_CONFIG_HOME set // #given opencode CLI binary detected, platform is Linux with XDG_CONFIG_HOME set
Object.defineProperty(process, "platform", { value: "linux" }) Object.defineProperty(process, "platform", { value: "linux" })
process.env.XDG_CONFIG_HOME = "/custom/config" process.env.XDG_CONFIG_HOME = "/custom/config"
delete process.env.OPENCODE_CONFIG_DIR
// #when getOpenCodeConfigDir is called with binary="opencode" // #when getOpenCodeConfigDir is called with binary="opencode"
const result = getOpenCodeConfigDir({ binary: "opencode", version: "1.0.200" }) const result = getOpenCodeConfigDir({ binary: "opencode", version: "1.0.200" })
@ -168,6 +170,7 @@ describe("opencode-config-dir", () => {
// #given opencode CLI binary detected, platform is macOS // #given opencode CLI binary detected, platform is macOS
Object.defineProperty(process, "platform", { value: "darwin" }) Object.defineProperty(process, "platform", { value: "darwin" })
delete process.env.XDG_CONFIG_HOME delete process.env.XDG_CONFIG_HOME
delete process.env.OPENCODE_CONFIG_DIR
// #when getOpenCodeConfigDir is called with binary="opencode" // #when getOpenCodeConfigDir is called with binary="opencode"
const result = getOpenCodeConfigDir({ binary: "opencode", version: "1.0.200" }) const result = getOpenCodeConfigDir({ binary: "opencode", version: "1.0.200" })
@ -180,6 +183,7 @@ describe("opencode-config-dir", () => {
// #given opencode CLI binary detected, platform is Windows // #given opencode CLI binary detected, platform is Windows
Object.defineProperty(process, "platform", { value: "win32" }) Object.defineProperty(process, "platform", { value: "win32" })
delete process.env.APPDATA delete process.env.APPDATA
delete process.env.OPENCODE_CONFIG_DIR
// #when getOpenCodeConfigDir is called with binary="opencode" // #when getOpenCodeConfigDir is called with binary="opencode"
const result = getOpenCodeConfigDir({ binary: "opencode", version: "1.0.200", checkExisting: false }) const result = getOpenCodeConfigDir({ binary: "opencode", version: "1.0.200", checkExisting: false })
@ -257,6 +261,7 @@ describe("opencode-config-dir", () => {
// #given opencode CLI binary on Linux // #given opencode CLI binary on Linux
Object.defineProperty(process, "platform", { value: "linux" }) Object.defineProperty(process, "platform", { value: "linux" })
delete process.env.XDG_CONFIG_HOME delete process.env.XDG_CONFIG_HOME
delete process.env.OPENCODE_CONFIG_DIR
// #when getOpenCodeConfigPaths is called // #when getOpenCodeConfigPaths is called
const paths = getOpenCodeConfigPaths({ binary: "opencode", version: "1.0.200" }) const paths = getOpenCodeConfigPaths({ binary: "opencode", version: "1.0.200" })

View File

@ -1,8 +1,8 @@
import { existsSync, readFileSync } from "fs" import { existsSync, readFileSync } from "fs"
import { join } from "path" import { join } from "path"
import { homedir } from "os"
import { BUILTIN_SERVERS, EXT_TO_LANG, LSP_INSTALL_HINTS } from "./constants" import { BUILTIN_SERVERS, EXT_TO_LANG, LSP_INSTALL_HINTS } from "./constants"
import type { ResolvedServer, ServerLookupResult } from "./types" import type { ResolvedServer, ServerLookupResult } from "./types"
import { getOpenCodeConfigDir } from "../../shared"
interface LspEntry { interface LspEntry {
disabled?: boolean disabled?: boolean
@ -34,10 +34,11 @@ function loadJsonFile<T>(path: string): T | null {
function getConfigPaths(): { project: string; user: string; opencode: string } { function getConfigPaths(): { project: string; user: string; opencode: string } {
const cwd = process.cwd() const cwd = process.cwd()
const configDir = getOpenCodeConfigDir({ binary: "opencode" })
return { return {
project: join(cwd, ".opencode", "oh-my-opencode.json"), project: join(cwd, ".opencode", "oh-my-opencode.json"),
user: join(homedir(), ".config", "opencode", "oh-my-opencode.json"), user: join(configDir, "oh-my-opencode.json"),
opencode: join(homedir(), ".config", "opencode", "opencode.json"), opencode: join(configDir, "opencode.json"),
} }
} }
@ -199,10 +200,11 @@ export function isServerInstalled(command: string[]): boolean {
} }
const cwd = process.cwd() const cwd = process.cwd()
const configDir = getOpenCodeConfigDir({ binary: "opencode" })
const additionalBases = [ const additionalBases = [
join(cwd, "node_modules", ".bin"), join(cwd, "node_modules", ".bin"),
join(homedir(), ".config", "opencode", "bin"), join(configDir, "bin"),
join(homedir(), ".config", "opencode", "node_modules", ".bin"), join(configDir, "node_modules", ".bin"),
] ]
for (const base of additionalBases) { for (const base of additionalBases) {

View File

@ -1,7 +1,7 @@
import { tool, type ToolDefinition } from "@opencode-ai/plugin" import { tool, type ToolDefinition } from "@opencode-ai/plugin"
import { existsSync, readdirSync, readFileSync } from "fs" import { existsSync, readdirSync, readFileSync } from "fs"
import { join, basename, dirname } from "path" import { join, basename, dirname } from "path"
import { parseFrontmatter, resolveCommandsInText, resolveFileReferencesInText, sanitizeModelField } from "../../shared" import { parseFrontmatter, resolveCommandsInText, resolveFileReferencesInText, sanitizeModelField, getOpenCodeConfigDir } from "../../shared"
import type { CommandFrontmatter } from "../../features/claude-code-command-loader/types" import type { CommandFrontmatter } from "../../features/claude-code-command-loader/types"
import { isMarkdownFile } from "../../shared/file-utils" import { isMarkdownFile } from "../../shared/file-utils"
import { getClaudeConfigDir } from "../../shared" import { getClaudeConfigDir } from "../../shared"
@ -52,10 +52,10 @@ function discoverCommandsFromDir(commandsDir: string, scope: CommandScope): Comm
} }
export function discoverCommandsSync(): CommandInfo[] { export function discoverCommandsSync(): CommandInfo[] {
const { homedir } = require("os") const configDir = getOpenCodeConfigDir({ binary: "opencode" })
const userCommandsDir = join(getClaudeConfigDir(), "commands") const userCommandsDir = join(getClaudeConfigDir(), "commands")
const projectCommandsDir = join(process.cwd(), ".claude", "commands") const projectCommandsDir = join(process.cwd(), ".claude", "commands")
const opencodeGlobalDir = join(homedir(), ".config", "opencode", "command") const opencodeGlobalDir = join(configDir, "command")
const opencodeProjectDir = join(process.cwd(), ".opencode", "command") const opencodeProjectDir = join(process.cwd(), ".opencode", "command")
const userCommands = discoverCommandsFromDir(userCommandsDir, "user") const userCommands = discoverCommandsFromDir(userCommandsDir, "user")