Skip to content

Report malformed metadata JSON at the line it breaks on [patch] - #51

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/28-locate-metadata-parse-failure
Oct 20, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/28-locate-metadata-parse-failure

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #28

What was wrong

MetadataFile.Deserialize<T> reported every parse failure through context.Report, which always uses Location.None. A JsonException already carries LineNumber and BytePositionInLine, but malformed metadata produced a diagnostic that could not be clicked through to the bad line.

Change

  • In the JsonException branch, the diagnostic is now reported with ReportAt at a new private LocationOf(ex). The location is in the metadata file, using the existing LocationAt helper.
  • LineNumber counts \n line feeds from zero, as System.Text.Json does, so the line start is found the same way (LineStart). This also holds for CRLF files.
  • BytePositionInLine counts UTF-8 bytes. The line is walked one code point at a time (CharactersAt, Utf8Bytes): 1 to 3 bytes per BMP character and 4 per surrogate pair. The position is clamped to the line, and a position at the end of a line or file gives a zero-length span.
  • Location.None is kept for a JsonException without a line number, for the NotSupportedException / InvalidOperationException / ArgumentException branches, for the null-document case, and when there is no SourceText.

Tests (GeneratorBaseTests)

  • AMalformedMetadataFileIsReportedAtTheBadLine (LF and CRLF): {"things": [1,,2]} spread so the error is on line 3. The location is an external file named things.json, with StartLinePosition.Line == 2 and the column System.Text.Json reports (the first comma of the pair).
  • AParseFailureAfterMultiByteCharactersIsLocatedByCharacterNotByte: "é😀" before the error lands on the same character as an ASCII twin with the same character layout.
  • AParseFailureAtTheEndOfTheFileIsLocatedAtTheEnd: truncated JSON is located on its last line, within the line.
  • AShapeFailureWithNoPositionIsStillReportedWithoutALocation: an unsupported shape is still reported at Location.None (a control).

With the MetadataFile.cs change reverted, the 4 location tests fail and the control passes. With it, the full suite passes, 60/60 on net10.0. The local SonarCloud analyzer build (-p:CustomAfterMicrosoftCommonProps=.sonarlint/sonar-local.props) is clean. It flagged cognitive complexity on the first draft, which is why the walk is split into three small helpers.


Generated by Claude Code

MetadataFile.Deserialize reported every parse failure at Location.None,
so the most common toolkit diagnostic could not be clicked through to
the bad line even though the JsonException carries its position.

A JsonException with a line number is now reported at that line and
column in the metadata file. BytePositionInLine counts UTF-8 bytes, so
the position is walked through the line a code point at a time, and it
is clamped to the line. Failures without a position (NotSupported,
InvalidOperation, Argument, a null document) still report no location.

Fixes #28

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

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 5740044 into main Oct 20, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/28-locate-metadata-parse-failure branch October 20, 2026 12:02
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 has no location, so malformed JSON can't be clicked through to the bad line

2 participants