Skip to content

Locate a parse failure in a CR-only metadata file on its real line [patch] - #62

Open
matt-edmondson wants to merge 1 commit into
mainfrom
fix/60-cr-only-parse-location
Open

matt-edmondson wants to merge 1 commit into
mainfrom
fix/60-cr-only-parse-location

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #60

What was wrong

System.Text.Json counts lines by \n only. In a metadata file with CR-only line endings it reports every error on LineNumber: 0, with BytePositionInLine measured from the start of the file. MetadataFile.LocationOf stopped its column walk (and its length check) at the first \r, so the diagnostic was clamped to the end of the file's first line.

What changed

  • New EndsLine(index) helper: a character ends a line when it is \n, or the \r of a \r\n pair. That is System.Text.Json's notion of a line.
  • The column walk and the length check in LocationOf use it. LocationAt then maps the resulting offset to Roslyn's line/column, which does treat a lone \r as a line break.

Tests

  • AMalformedMetadataFileIsReportedAtTheBadLine gains a CR data row next to LF and CRLF. The expected column now comes from the literal bad line instead of Split('\n'), which only works for the LF/CRLF rows.
  • With the fix reverted, the CR row fails (Assert.AreEqual(2, span.StartLinePosition.Line)). With it, all 67 tests pass, including the existing LF, CRLF and multi-byte/surrogate location tests.

Generated by Claude Code

…atch]

System.Text.Json counts lines by \n only, so in a file with CR-only line
endings it reports every error on line zero, measured from the start of
the file. LocationOf stopped its column walk at the first \r, which
clamped the diagnostic to the end of the file's first line. Treat \r as
ending a line only when \n follows it, in both the walk and the length
check, and let LocationAt map the offset to Roslyn's line and column.

Fixes #60

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5t9HHcsUTKWJqPdLK
@sonarqubecloud

sonarqubecloud Bot commented Nov 2, 2026

Copy link
Copy Markdown

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.

Metadata parse-failure diagnostic points at line 1, column 2 for a JSON file with CR-only line endings, whatever line the error is on

2 participants