Skip to content

fix(opencode): tolerate CRLF in plugin self-heal regex - #54

Merged
hellolib merged 1 commit into
mainfrom
fix/opencode-crlf-selfheal
Aug 22, 2026
Merged

hellolib merged 1 commit into
mainfrom
fix/opencode-crlf-selfheal

Conversation

@hellolib

Copy link
Copy Markdown
Owner

Problem

The Test workflow has been red on go-test (windows-latest) across the last several runs (main + 2 PRs), while macOS/Ubuntu pass and the Release workflow is fine.

Failing tests:

  • TestRefreshPluginIfStaleRewritesOutdated
  • TestRefreshPluginIfStalePreservesBakedBinaryPath

Both fail with RefreshPluginIfStale() = false, want true for outdated plugin.

Root cause

binaryConstRe in internal/opencodehooks/settings.go anchored on $:

regexp.MustCompile(`(?m)^const BINARY = "([^"]*)";$`)
``"

`plugin/opencode.js` is LF in the repo, but **Windows runners default to `core.autocrlf=true`**, converting it to CRLF *before* `go:embed` bakes it into `PluginJS`. In Go multiline mode, `$` matches before `\n` — but in CRLF the `\r` sits between `;` and `\n`, breaking the `";$"` anchor. `bakedBinaryPath` returns `false` → `RefreshPluginIfStale` hits the "unrecognizable file" early-return → `(false, nil)`.

**This is a real user-facing bug, not just CI:** `WritePluginFile` writes CRLF on Windows, so the self-heal that delivers JS fixes to existing users was **completely broken on Windows** — the exact thing `RefreshPluginIfStale` exists to fix (see its doc comment).

## Fix

1. **Regex tolerates `\r`** — `;`\r?$`` — so it reads back the baked binary path from existing CRLF plugin files on disk (the key fix for existing Windows users).
2. **`.gitattributes` pins `plugin/opencode.js` to `eol=lf`** — makes the embedded content byte-identical across platforms, keeping `bytes.Equal` comparisons reliable.
3. **New regression test** `TestRefreshPluginIfStaleHandlesCRLFLineEndings` — verified it fails against the old regex.

## Audit

Swept the whole repo for the same class of bug:
- `(?m)` multiline regexes anchoring line ends → only `binaryConstRe` (fixed)
- `go:embed` text assets → only `plugin/opencode.js` (pinned LF)
- `bytes.Equal` on content → 5 sites; the other 4 compare Go-generated JSON (same-source, no git line-ending conversion), no risk

## Verification

- `go test ./...` → 513 passed
- `go vet ./...` → clean
- `go build ./...` → clean
- `gofmt` → no changes needed

Windows CI (go-test windows-latest) failed two RefreshPluginIfStale
tests. Root cause: plugin/opencode.js is LF in the repo, but Windows
runners default to core.autocrlf=true, converting it to CRLF before
go:embed bakes it into PluginJS. The binaryConstRe regex
(?m)^const BINARY = "([^"]*)";$ anchored on $ which matches before
\n — but \r sits between ; and \n in CRLF, breaking the anchor.
bakedBinaryPath returned false → RefreshPluginIfStale silently
no-op'd, so the self-heal meant to deliver JS fixes to existing users
was completely broken on Windows.

Two fixes:
- binaryConstRe now uses ;\r?$ to tolerate CRLF (fixes reading
  existing CRLF plugin files on disk — the real user-facing bug)
- .gitattributes pins plugin/opencode.js to eol=lf so the embedded
  content is byte-identical across platforms and bytes.Equal
  comparisons stay reliable

Added TestRefreshPluginIfStaleHandlesCRLFLineEndings as a regression
guard (verified it fails against the old regex).
@hellolib
hellolib merged commit 0a85230 into main Aug 22, 2026
4 checks passed
@hellolib
hellolib deleted the fix/opencode-crlf-selfheal branch August 22, 2026 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant