diff --git a/crates/ide-agent/src/mcp.rs b/crates/ide-agent/src/mcp.rs index 3b49ba90dc..782b667be9 100644 --- a/crates/ide-agent/src/mcp.rs +++ b/crates/ide-agent/src/mcp.rs @@ -941,107 +941,10 @@ fn normalize_agents_list(agents: Vec) -> Vec { out } -/// Strips `//` line comments, `/* ... */` block comments, and trailing commas before `}` or `]` -/// outside JSON string literals so JSONC config files parse cleanly with `serde_json`. -pub(crate) fn strip_jsonc(input: &str) -> String { - let bytes = input.as_bytes(); - let mut out = String::with_capacity(input.len()); - let mut i = 0; - let mut in_str = false; - while i < bytes.len() { - if in_str { - let ch = input[i..].chars().next().unwrap(); - out.push(ch); - i += ch.len_utf8(); - if ch == '\\' && i < bytes.len() { - let next = input[i..].chars().next().unwrap(); - out.push(next); - i += next.len_utf8(); - } else if ch == '"' { - in_str = false; - } - continue; - } - if bytes[i] == b'"' { - in_str = true; - out.push('"'); - i += 1; - continue; - } - if bytes[i] == b'/' && i + 1 < bytes.len() && bytes[i + 1] == b'/' { - i += 2; - while i < bytes.len() && bytes[i] != b'\n' { - i += 1; - } - continue; - } - if bytes[i] == b'/' && i + 1 < bytes.len() && bytes[i + 1] == b'*' { - i += 2; - while i + 1 < bytes.len() && !(bytes[i] == b'*' && bytes[i + 1] == b'/') { - if bytes[i] == b'\n' { - out.push('\n'); - i += 1; - } else { - let ch = input[i..].chars().next().unwrap(); - i += ch.len_utf8(); - } - } - i = (i + 2).min(bytes.len()); - continue; - } - let ch = input[i..].chars().next().unwrap(); - out.push(ch); - i += ch.len_utf8(); - } - remove_trailing_commas(&out) -} - -fn remove_trailing_commas(input: &str) -> String { - let bytes = input.as_bytes(); - let mut out = String::with_capacity(input.len()); - let mut i = 0; - let mut in_str = false; - while i < bytes.len() { - if in_str { - let ch = input[i..].chars().next().unwrap(); - out.push(ch); - i += ch.len_utf8(); - if ch == '\\' && i < bytes.len() { - let next = input[i..].chars().next().unwrap(); - out.push(next); - i += next.len_utf8(); - } else if ch == '"' { - in_str = false; - } - continue; - } - if bytes[i] == b'"' { - in_str = true; - out.push('"'); - i += 1; - continue; - } - if bytes[i] == b',' { - let mut j = i + 1; - while j < bytes.len() && bytes[j].is_ascii_whitespace() { - j += 1; - } - if j < bytes.len() && (bytes[j] == b'}' || bytes[j] == b']') { - i += 1; - continue; - } - } - let ch = input[i..].chars().next().unwrap(); - out.push(ch); - i += ch.len_utf8(); - } - out -} - fn parse_jsonc_value(raw: &str) -> Result { let trimmed = raw.trim().trim_start_matches('\u{feff}'); serde_json::from_str::(trimmed) - .or_else(|_| serde_json::from_str::(&strip_jsonc(trimmed))) + .or_else(|_| serde_json::from_str::(&ide_core::jsonc::strip(trimmed))) .map_err(|e| format!("Invalid JSON: {e}")) } diff --git a/crates/ide-agent/src/skills.rs b/crates/ide-agent/src/skills.rs index a1697fef91..cbb622b500 100644 --- a/crates/ide-agent/src/skills.rs +++ b/crates/ide-agent/src/skills.rs @@ -406,7 +406,7 @@ fn copilot_installed_plugin_roots(copilot_dir: &Path) -> Vec<(PathBuf, AgentSkil }; if let Ok(text) = std::fs::read_to_string(copilot_dir.join("config.json")) - && let Ok(root) = serde_json::from_str::(&crate::mcp::strip_jsonc(&text)) + && let Ok(root) = serde_json::from_str::(&ide_core::jsonc::strip(&text)) && let Some(installed) = root.get("installedPlugins").and_then(serde_json::Value::as_array) { for entry in installed { diff --git a/crates/ide-core/src/jsonc.rs b/crates/ide-core/src/jsonc.rs index 7b602e5d4f..476f591a7f 100644 --- a/crates/ide-core/src/jsonc.rs +++ b/crates/ide-core/src/jsonc.rs @@ -40,6 +40,10 @@ pub fn strip(src: &str) -> String { if prev == '*' && n == '/' { break; } + // Keep the comment's line breaks so parse errors report the original line. + if n == '\n' { + out.push('\n'); + } prev = n; } } @@ -65,7 +69,7 @@ pub fn strip(src: &str) -> String { in_str = true; cleaned.push(c); } else if c == ',' { - let next = bytes[i + 1..].iter().find(|ch| !ch.is_whitespace()); + let next = bytes[i + 1..].iter().find(|ch| !ch.is_ascii_whitespace()); if !matches!(next, Some('}') | Some(']')) { cleaned.push(c); } @@ -95,4 +99,53 @@ mod tests { let v: serde_json::Value = serde_json::from_str(&strip("\u{feff}{ \"a\": 1 }")).unwrap(); assert_eq!(v["a"], 1); } + + #[test] + fn keeps_line_breaks_of_block_comments_so_errors_report_the_original_line() { + assert_eq!(strip("{\"a\"/*\n\n*/:x}"), "{\"a\"\n\n:x}"); + let err = serde_json::from_str::(&strip("{\"a\"/*\n\n*/:x}")).unwrap_err(); + assert_eq!(err.line(), 3); + // A comment is whitespace, not a join: `1 2` stays invalid. + assert!(serde_json::from_str::(&strip("1/*\n*/2")).is_err()); + } + + #[test] + fn line_comments_keep_their_line_break_and_may_end_the_input() { + assert_eq!(strip("{\"a\":1} // tail"), "{\"a\":1} "); + assert_eq!(strip("// a\r\n{\"a\":1}"), "\n{\"a\":1}"); + assert_eq!(strip("[1, // c\n2]"), "[1, \n2]"); + } + + #[test] + fn unterminated_block_comment_drops_the_rest() { + assert_eq!(strip("{\"a\":1}/* open"), "{\"a\":1}"); + assert_eq!(strip("/*"), ""); + assert_eq!(strip("/*/ x"), ""); + assert_eq!(strip("/**/{}"), "{}"); + assert_eq!(strip("/***/{}"), "{}"); + } + + #[test] + fn comment_markers_inside_strings_survive_including_after_escapes() { + let src = "{\"a\":\"\\\\\",\"b\":\"//x\",\"c\":\"/*y*/\",\"d\":\"é,\u{1F600}\"} // é"; + let v: serde_json::Value = serde_json::from_str(&strip(src)).unwrap(); + assert_eq!(v, serde_json::json!({"a": "\\", "b": "//x", "c": "/*y*/", "d": "é,\u{1F600}"})); + } + + #[test] + fn trailing_commas_are_dropped_across_whitespace_and_comments_only_before_a_closer() { + assert_eq!(strip("[1,2, ]"), "[1,2 ]"); + assert_eq!(strip("[1,\r\n]"), "[1\r\n]"); + assert_eq!(strip("[1,/*c*/]"), "[1]"); + assert_eq!(strip("[1,//c\n]"), "[1\n]"); + assert_eq!(strip("[1,/*c*/ ,]"), "[1, ]"); + assert_eq!(strip("{\"a\":\"x,}\"}"), "{\"a\":\"x,}\"}"); + assert_eq!(strip("[1,2]"), "[1,2]"); + } + + #[test] + fn only_json_whitespace_may_separate_a_trailing_comma_from_its_closer() { + // U+00A0 is not JSON whitespace: the comma stays and serde_json reports it. + assert_eq!(strip("[1,\u{a0}]"), "[1,\u{a0}]"); + } }