Match Opencode Config Resolution Behavior (#949)

* fix(config): match opencode config resolution behavior

* fix(config): use primary opencode config file

---------

Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
This commit is contained in:
cyan
2026-04-21 22:47:42 +03:00
committed by GitHub
co-authored by Bohdan Triapitsyn
parent d4a4f43a83
commit a8719f0a8e
2 changed files with 36 additions and 6 deletions
+18 -3
View File
@@ -7,7 +7,7 @@ import { parse as parseJsonc } from 'jsonc-parser';
const OPENCODE_CONFIG_DIR = path.join(os.homedir(), '.config', 'opencode'); const OPENCODE_CONFIG_DIR = path.join(os.homedir(), '.config', 'opencode');
const AGENT_DIR = path.join(OPENCODE_CONFIG_DIR, 'agents'); const AGENT_DIR = path.join(OPENCODE_CONFIG_DIR, 'agents');
const COMMAND_DIR = path.join(OPENCODE_CONFIG_DIR, 'commands'); const COMMAND_DIR = path.join(OPENCODE_CONFIG_DIR, 'commands');
const CONFIG_FILE = path.join(OPENCODE_CONFIG_DIR, 'opencode.json'); const CONFIG_FILE = path.join(OPENCODE_CONFIG_DIR, 'config.json');
const CUSTOM_CONFIG_FILE = process.env.OPENCODE_CONFIG const CUSTOM_CONFIG_FILE = process.env.OPENCODE_CONFIG
? path.resolve(process.env.OPENCODE_CONFIG) ? path.resolve(process.env.OPENCODE_CONFIG)
: null; : null;
@@ -321,11 +321,25 @@ const getProjectConfigPath = (workingDirectory?: string): string | null => {
}; };
const getConfigPaths = (workingDirectory?: string) => ({ const getConfigPaths = (workingDirectory?: string) => ({
userPath: CONFIG_FILE, userPaths: [
path.join(OPENCODE_CONFIG_DIR, 'config.json'),
path.join(OPENCODE_CONFIG_DIR, 'opencode.json'),
path.join(OPENCODE_CONFIG_DIR, 'opencode.jsonc'),
],
projectPath: getProjectConfigPath(workingDirectory), projectPath: getProjectConfigPath(workingDirectory),
customPath: CUSTOM_CONFIG_FILE customPath: CUSTOM_CONFIG_FILE
}); });
const getPrimaryUserConfigPath = (userPaths: string[]): string => {
for (const userPath of userPaths) {
if (fs.existsSync(userPath)) {
return userPath;
}
}
return CONFIG_FILE;
};
const readConfigFile = (filePath?: string | null): Record<string, unknown> => { const readConfigFile = (filePath?: string | null): Record<string, unknown> => {
if (!filePath || !fs.existsSync(filePath)) return {}; if (!filePath || !fs.existsSync(filePath)) return {};
const content = fs.readFileSync(filePath, 'utf8'); const content = fs.readFileSync(filePath, 'utf8');
@@ -355,7 +369,8 @@ const mergeConfigs = (base: Record<string, unknown>, override: Record<string, un
}; };
const readConfigLayers = (workingDirectory?: string) => { const readConfigLayers = (workingDirectory?: string) => {
const { userPath, projectPath, customPath } = getConfigPaths(workingDirectory); const { userPaths, projectPath, customPath } = getConfigPaths(workingDirectory);
const userPath = getPrimaryUserConfigPath(userPaths);
const userConfig = readConfigFile(userPath); const userConfig = readConfigFile(userPath);
const projectConfig = readConfigFile(projectPath); const projectConfig = readConfigFile(projectPath);
const customConfig = readConfigFile(customPath); const customConfig = readConfigFile(customPath);
+18 -3
View File
@@ -10,7 +10,7 @@ const OPENCODE_CONFIG_DIR = path.join(os.homedir(), '.config', 'opencode');
const AGENT_DIR = path.join(OPENCODE_CONFIG_DIR, 'agents'); const AGENT_DIR = path.join(OPENCODE_CONFIG_DIR, 'agents');
const COMMAND_DIR = path.join(OPENCODE_CONFIG_DIR, 'commands'); const COMMAND_DIR = path.join(OPENCODE_CONFIG_DIR, 'commands');
const SKILL_DIR = path.join(OPENCODE_CONFIG_DIR, 'skills'); const SKILL_DIR = path.join(OPENCODE_CONFIG_DIR, 'skills');
const CONFIG_FILE = path.join(OPENCODE_CONFIG_DIR, 'opencode.json'); const CONFIG_FILE = path.join(OPENCODE_CONFIG_DIR, 'config.json');
const CUSTOM_CONFIG_FILE = process.env.OPENCODE_CONFIG const CUSTOM_CONFIG_FILE = process.env.OPENCODE_CONFIG
? path.resolve(process.env.OPENCODE_CONFIG) ? path.resolve(process.env.OPENCODE_CONFIG)
: null; : null;
@@ -115,12 +115,26 @@ function getProjectConfigPath(workingDirectory) {
function getConfigPaths(workingDirectory) { function getConfigPaths(workingDirectory) {
return { return {
userPath: CONFIG_FILE, userPaths: [
path.join(OPENCODE_CONFIG_DIR, 'config.json'),
path.join(OPENCODE_CONFIG_DIR, 'opencode.json'),
path.join(OPENCODE_CONFIG_DIR, 'opencode.jsonc'),
],
projectPath: getProjectConfigPath(workingDirectory), projectPath: getProjectConfigPath(workingDirectory),
customPath: CUSTOM_CONFIG_FILE customPath: CUSTOM_CONFIG_FILE
}; };
} }
function getPrimaryUserConfigPath(userPaths) {
for (const userPath of userPaths) {
if (fs.existsSync(userPath)) {
return userPath;
}
}
return CONFIG_FILE;
}
function readConfigFile(filePath) { function readConfigFile(filePath) {
if (!filePath || !fs.existsSync(filePath)) { if (!filePath || !fs.existsSync(filePath)) {
return {}; return {};
@@ -163,7 +177,8 @@ function mergeConfigs(base, override) {
} }
function readConfigLayers(workingDirectory) { function readConfigLayers(workingDirectory) {
const { userPath, projectPath, customPath } = getConfigPaths(workingDirectory); const { userPaths, projectPath, customPath } = getConfigPaths(workingDirectory);
const userPath = getPrimaryUserConfigPath(userPaths);
const userConfig = readConfigFile(userPath); const userConfig = readConfigFile(userPath);
const projectConfig = readConfigFile(projectPath); const projectConfig = readConfigFile(projectPath);
const customConfig = readConfigFile(customPath); const customConfig = readConfigFile(customPath);