Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn claude_json() -> Value {
fn json_at(path: &PathBuf) -> Value {
fs::read_to_string(path)
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.and_then(|s| crate::jsonc::parse_jsonc(&s).ok())
.unwrap_or(Value::Null)
}

Expand Down Expand Up @@ -83,7 +83,7 @@ fn write_pinned_mode(path: &PathBuf) -> bool {
fn merge_json(path: &PathBuf, root_key: &str, key: &str, value: Value) -> bool {
let mut existing: Value = fs::read_to_string(path)
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.and_then(|s| crate::jsonc::parse_jsonc(&s).ok())
.unwrap_or_else(|| serde_json::json!({}));
if !existing.is_object() {
existing = serde_json::json!({});
Expand All @@ -106,7 +106,7 @@ fn merge_json(path: &PathBuf, root_key: &str, key: &str, value: Value) -> bool {
fn merge_opencode_mcp(path: &PathBuf, key: &str, entry: Value) -> bool {
let mut existing: Value = fs::read_to_string(path)
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.and_then(|s| crate::jsonc::parse_jsonc(&s).ok())
.unwrap_or_else(|| serde_json::json!({}));
if !existing.is_object() {
existing = serde_json::json!({});
Expand Down Expand Up @@ -812,6 +812,48 @@ mod tests {
});
}

#[test]
fn agentflare_mcp_opencode_apply_survives_jsonc_comments_and_trailing_comma() {
crate::paths::test_support::with_temp_home(|| {
let path = home()
.join(".config")
.join("opencode")
.join("opencode.jsonc");
fs::create_dir_all(path.parent().unwrap()).unwrap();
// Real jsonc: comments + trailing comma + an existing mcp server
// entry that must survive. Before the jsonc parser, this parse
// failure was treated as "no existing config" and everything
// below (including `other-server`) was silently dropped on write.
fs::write(
&path,
r#"{
// OpenCode configuration
"mcp": {
"other-server": { "type": "local", "command": ["other-server"] },
},
}"#,
)
.unwrap();

let components = get_components("opencode");
let agentflare_mcp = components
.iter()
.find(|c| c.id == "agentflare-mcp")
.unwrap();
(agentflare_mcp.apply)();

let value = json_at(&path);
assert!(
value["mcp"]["other-server"].is_object(),
"existing entry must survive"
);
assert!(
value["mcp"]["flare"].is_object(),
"flare entry must be added"
);
});
}

#[test]
fn agentflare_mcp_windsurf_check_then_apply_then_check() {
crate::paths::test_support::with_temp_home(|| {
Expand Down
40 changes: 39 additions & 1 deletion src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ fn wire_opencode() {

let mut config: Value = fs::read_to_string(&path)
.ok()
.and_then(|s| serde_json::from_str(&s).ok())
.and_then(|s| crate::jsonc::parse_jsonc(&s).ok())
.unwrap_or_else(|| json!({}));
if !config.is_object() {
config = json!({});
Expand Down Expand Up @@ -1015,6 +1015,44 @@ mod tests {
});
}

#[test]
fn wire_opencode_preserves_existing_content_with_comments() {
with_temp_home(|| {
let config_path = home()
.join(".config")
.join("opencode")
.join("opencode.jsonc");
let rules_dir = home().join(".config").join("opencode").join("rules");
fs::create_dir_all(config_path.parent().unwrap()).unwrap();
// Real jsonc: comments + a trailing comma + an existing mcp
// server entry that must survive. Before the jsonc parser, this
// parse failure was treated as "no existing config" and every
// key below (including `mcp`) was silently dropped on write.
fs::write(
&config_path,
r#"{
// OpenCode configuration
"mcp": {
"some-server": { "type": "local", "command": ["some-server"] }
},
}"#,
)
.unwrap();
fs::create_dir_all(&rules_dir).unwrap();
fs::write(rules_dir.join("exa.md"), "# exa\n").unwrap();

wire_opencode();

let content = fs::read_to_string(&config_path).unwrap();
let parsed: Value = crate::jsonc::parse_jsonc(&content).unwrap();
assert!(
parsed["mcp"]["some-server"].is_object(),
"existing mcp server entry must survive: {content}"
);
assert!(content.contains("exa.md"));
});
}

#[test]
fn wire_opencode_removes_legacy_engram_instruction_on_upgrade() {
with_temp_home(|| {
Expand Down
Loading
Loading