Skip to content

fix: guard undefined contents in diff renderer to fix share viewer SSR crash#21763

Merged
Brendonovich merged 3 commits intoanomalyco:devfrom
YGoetschel:fix/share-viewer-ssr-crash
May 7, 2026
Merged

fix: guard undefined contents in diff renderer to fix share viewer SSR crash#21763
Brendonovich merged 3 commits intoanomalyco:devfrom
YGoetschel:fix/share-viewer-ssr-crash

Conversation

@YGoetschel
Copy link
Copy Markdown
Contributor

@YGoetschel YGoetschel commented Apr 9, 2026

Issue for this PR

Closes #21694

Type of change

  • Bug fix

What does this PR do?

Certain shared sessions crash the share viewer with Cannot read properties of undefined (reading 'split') in the @pierre/diffs diff renderer. The crash happens because parseDiffFromFile calls createTwoFilesPatch(oldFile.name, newFile.name, oldFile.contents, newFile.contents, ...) — when contents is undefined, LineDiff.tokenize crashes calling .split() on it.

This occurs in two related places:

  1. packages/ui/src/components/file-ssr.tsxDiffSSRViewer.hydrate was passing local.before/local.after directly to FileDiff.hydrate without guarding contents. Added the same typeof x.contents === "string" ? x.contents : "" normalization already present in file.tsx (lines 1056–1057 and 1090–1091).

  2. packages/ui/src/components/message-part.tsx — The edit tool renderer constructs before/after as props.metadata?.filediff?.before || props.input.oldString. When both fields are absent (e.g. new-file creation, or edit results where metadata omits content), contents was undefined. Added || "" fallback so the diff renders the file as fully added/removed instead of crashing.

An empty-string contents is the correct representation for "no prior content".

How did you verify your code works?

  • Ran bun turbo typecheck --filter=@opencode-ai/ui --filter=@opencode-ai/enterprise — both packages pass cleanly.
  • Reviewed FileDiff.js in @pierre/diffs to confirm that parseDiffFromFile is not called when contents is "" (empty string is valid input to createTwoFilesPatch).
  • Confirmed the guard pattern matches the one already used in file.tsx lines 1056–1057.

Screenshots / recordings

No UI change — the fix prevents a crash rather than changing visible output.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

…nderer

When edit tool results lack filediff.before/after or input oldString/newString,
contents was passed as undefined to parseDiffFromFile (via FileDiff.hydrate or
preloadMultiFileDiff), causing a crash in createTwoFilesPatch → LineDiff.tokenize.

- file-ssr.tsx: normalize before/after contents to "" in DiffSSRViewer.hydrate,
  matching the guard already present in file.tsx (lines 1056-1057)
- message-part.tsx: default edit tool before/after contents to "" when both
  metadata filediff and input string fields are absent

Fixes anomalyco#21694
@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Apr 9, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 9, 2026

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@Brendonovich Brendonovich merged commit fea9a0b into anomalyco:dev May 7, 2026
9 checks passed
leohenon pushed a commit to leohenon/opencode-vim that referenced this pull request May 7, 2026
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.

Share viewer SSR crash: Cannot read properties of undefined (reading 'split') in diff renderer

2 participants