Repository navigation
Commit 3af8e25
authored
fix(connect): keep string contents when stripping trailing commas (#1768)
## Bug
`unmarshalLenientJSON` (the JSONC fallback added for #922) removes
trailing commas with the regex `,\s*([}\]])`. The regex has no notion of
JSON strings, so it also rewrites `, }` and `, ]` inside string values.
The fallback runs whenever strict parsing fails, for example on a
hand-edited `opencode.json` with one trailing comma. Connect,
disconnect, undo and preview then work on the altered document, and
connect writes it back to the client's config file. Another server's
arguments are silently changed on disk.
`stripJSONComments`, the other half of the same fallback, is already
string-aware for exactly this reason.
## Trigger
`~/.config/opencode/opencode.json`:
```json
{
"mcp": {
"other": {"type": "local", "command": ["sh", "-c", "echo {a, } [b, ]"]},
},
}
```
After `mcpproxy connect opencode`, the other server's command on disk is
`["sh", "-c", "echo {a} [b]"]`.
## Fix
Replace the regex with `stripTrailingCommas`, a small scanner that
tracks string and escape state the same way `stripJSONComments` does. It
drops a `,` only when it is outside a string and the next non-whitespace
byte is `}` or `]`. The now-unused `regexp` import is removed.
## Test
`TestConnect_TrailingCommaCleanupPreservesStringContents` runs a real
`Connect("opencode", ...)` on the file above and checks the other
server's command is byte-for-byte unchanged after the write. The
existing trailing-comma and JSONC tests still pass.
## Commands run
- `CGO_ENABLED=0 go vet ./internal/connect`: clean
- `CGO_ENABLED=0 go test -count=1 ./internal/connect`: ok
- With the fix reverted (`git stash` of `connect.go`), the new test
fails with `other server's command rewritten: got ["sh" "-c" "echo {a}
[b]"]`.
- `gofmt -l` on the edited files: no output1 parent aedfc21 commit 3af8e25
2 files changed
Lines changed: 87 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | 10 | | |
12 | 11 | | |
13 | 12 | | |
| |||
1701 | 1700 | | |
1702 | 1701 | | |
1703 | 1702 | | |
1704 | | - | |
1705 | | - | |
1706 | 1703 | | |
1707 | 1704 | | |
1708 | 1705 | | |
| |||
1715 | 1712 | | |
1716 | 1713 | | |
1717 | 1714 | | |
1718 | | - | |
| 1715 | + | |
1719 | 1716 | | |
1720 | 1717 | | |
1721 | 1718 | | |
| |||
1791 | 1788 | | |
1792 | 1789 | | |
1793 | 1790 | | |
| 1791 | + | |
| 1792 | + | |
| 1793 | + | |
| 1794 | + | |
| 1795 | + | |
| 1796 | + | |
| 1797 | + | |
| 1798 | + | |
| 1799 | + | |
| 1800 | + | |
| 1801 | + | |
| 1802 | + | |
| 1803 | + | |
| 1804 | + | |
| 1805 | + | |
| 1806 | + | |
| 1807 | + | |
| 1808 | + | |
| 1809 | + | |
| 1810 | + | |
| 1811 | + | |
| 1812 | + | |
| 1813 | + | |
| 1814 | + | |
| 1815 | + | |
| 1816 | + | |
| 1817 | + | |
| 1818 | + | |
| 1819 | + | |
| 1820 | + | |
| 1821 | + | |
| 1822 | + | |
| 1823 | + | |
| 1824 | + | |
| 1825 | + | |
| 1826 | + | |
| 1827 | + | |
| 1828 | + | |
1794 | 1829 | | |
1795 | 1830 | | |
1796 | 1831 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
0 commit comments