Skip to content

Commit 5434e54

Browse files
authored
Merge pull request anomalyco#196 from different-ai/fix/support-jsonc-config
feat: support opencode.jsonc configs
2 parents a92363d + f299e30 commit 5434e54

5 files changed

Lines changed: 117 additions & 13 deletions

File tree

‎packages/desktop/src-tauri/Cargo.lock‎

Lines changed: 62 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/desktop/src-tauri/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ edition = "2021"
99
tauri-build = { version = "2", features = [] }
1010

1111
[dependencies]
12+
json5 = "0.4"
1213
serde = { version = "1", features = ["derive"] }
1314
serde_json = "1"
1415
tauri = { version = "2", features = [] }

‎packages/desktop/src-tauri/src/config.rs‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ use std::path::PathBuf;
44

55
use crate::types::{ExecResult, OpencodeConfigFile};
66

7-
pub fn resolve_opencode_config_path(scope: &str, project_dir: &str) -> Result<PathBuf, String> {
7+
fn opencode_config_candidates(scope: &str, project_dir: &str) -> Result<(PathBuf, PathBuf), String> {
88
match scope {
99
"project" => {
1010
if project_dir.trim().is_empty() {
1111
return Err("projectDir is required".to_string());
1212
}
13-
Ok(PathBuf::from(project_dir).join("opencode.json"))
13+
let root = PathBuf::from(project_dir);
14+
Ok((root.join("opencode.jsonc"), root.join("opencode.json")))
1415
}
1516
"global" => {
1617
let base = if let Ok(dir) = env::var("XDG_CONFIG_HOME") {
@@ -21,12 +22,27 @@ pub fn resolve_opencode_config_path(scope: &str, project_dir: &str) -> Result<Pa
2122
return Err("Unable to resolve config directory".to_string());
2223
};
2324

24-
Ok(base.join("opencode").join("opencode.json"))
25+
let root = base.join("opencode");
26+
Ok((root.join("opencode.jsonc"), root.join("opencode.json")))
2527
}
2628
_ => Err("scope must be 'project' or 'global'".to_string()),
2729
}
2830
}
2931

32+
pub fn resolve_opencode_config_path(scope: &str, project_dir: &str) -> Result<PathBuf, String> {
33+
let (jsonc_path, json_path) = opencode_config_candidates(scope, project_dir)?;
34+
35+
if jsonc_path.exists() {
36+
return Ok(jsonc_path);
37+
}
38+
39+
if json_path.exists() {
40+
return Ok(json_path);
41+
}
42+
43+
Ok(jsonc_path)
44+
}
45+
3046
pub fn read_opencode_config(scope: &str, project_dir: &str) -> Result<OpencodeConfigFile, String> {
3147
let path = resolve_opencode_config_path(scope.trim(), project_dir)?;
3248
let exists = path.exists();

‎packages/desktop/src-tauri/src/engine/spawn.rs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,20 @@ pub fn build_engine_command(program: &Path, hostname: &str, port: u16, project_d
3838
command.env("XDG_DATA_HOME", xdg_data_home);
3939
}
4040

41-
if let Some(xdg_config_home) = maybe_infer_xdg_home(
41+
let xdg_config_home = maybe_infer_xdg_home(
4242
"XDG_CONFIG_HOME",
4343
candidate_xdg_config_dirs(),
44-
Path::new("opencode/opencode.json"),
45-
) {
44+
Path::new("opencode/opencode.jsonc"),
45+
)
46+
.or_else(|| {
47+
maybe_infer_xdg_home(
48+
"XDG_CONFIG_HOME",
49+
candidate_xdg_config_dirs(),
50+
Path::new("opencode/opencode.json"),
51+
)
52+
});
53+
54+
if let Some(xdg_config_home) = xdg_config_home {
4655
command.env("XDG_CONFIG_HOME", xdg_config_home);
4756
}
4857

‎packages/desktop/src-tauri/src/workspace/files.rs‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,22 @@ pub fn ensure_workspace_files(workspace_path: &str, preset: &str) -> Result<(),
158158
.map_err(|e| format!("Failed to create .openwork/templates: {e}"))?;
159159
seed_templates(&templates_dir)?;
160160

161-
let config_path = root.join("opencode.json");
162-
let mut config: serde_json::Value = if config_path.exists() {
161+
let config_path_jsonc = root.join("opencode.jsonc");
162+
let config_path_json = root.join("opencode.json");
163+
let config_path = if config_path_jsonc.exists() {
164+
config_path_jsonc
165+
} else if config_path_json.exists() {
166+
config_path_json
167+
} else {
168+
config_path_jsonc
169+
};
170+
171+
let config_exists = config_path.exists();
172+
let mut config_changed = !config_exists;
173+
let mut config: serde_json::Value = if config_exists {
163174
let raw = fs::read_to_string(&config_path)
164175
.map_err(|e| format!("Failed to read {}: {e}", config_path.display()))?;
165-
serde_json::from_str(&raw).unwrap_or_else(|_| serde_json::json!({}))
176+
json5::from_str(&raw).unwrap_or_else(|_| serde_json::json!({}))
166177
} else {
167178
serde_json::json!({
168179
"$schema": "https://opencode.ai/config.json"
@@ -173,6 +184,7 @@ pub fn ensure_workspace_files(workspace_path: &str, preset: &str) -> Result<(),
173184
config = serde_json::json!({
174185
"$schema": "https://opencode.ai/config.json"
175186
});
187+
config_changed = true;
176188
}
177189

178190
let required_plugins: Vec<&str> = match preset {
@@ -196,7 +208,10 @@ pub fn ensure_workspace_files(workspace_path: &str, preset: &str) -> Result<(),
196208
_ => vec![],
197209
};
198210

199-
let merged = merge_plugins(existing_plugins, &required_plugins);
211+
let merged = merge_plugins(existing_plugins.clone(), &required_plugins);
212+
if merged != existing_plugins {
213+
config_changed = true;
214+
}
200215
if let Some(obj) = config.as_object_mut() {
201216
obj.insert(
202217
"plugin".to_string(),
@@ -205,8 +220,10 @@ pub fn ensure_workspace_files(workspace_path: &str, preset: &str) -> Result<(),
205220
}
206221
}
207222

208-
fs::write(&config_path, serde_json::to_string_pretty(&config).map_err(|e| e.to_string())?)
209-
.map_err(|e| format!("Failed to write {}: {e}", config_path.display()))?;
223+
if config_changed {
224+
fs::write(&config_path, serde_json::to_string_pretty(&config).map_err(|e| e.to_string())?)
225+
.map_err(|e| format!("Failed to write {}: {e}", config_path.display()))?;
226+
}
210227

211228
let openwork_path = root.join(".opencode").join("openwork.json");
212229
if !openwork_path.exists() {

0 commit comments

Comments
 (0)