Skip to content

MetadataFile.FindLocation points at text inside // and /* */ comments, and a " in a comment sends a scoped lookup to the wrong entry #78

Open

Description

@matt-edmondson

What's wrong

Since #69 the default options read metadata with ReadCommentHandling = JsonCommentHandling.Skip (https://github.com/ktsu-dev/SourceGeneratorToolkit/blob/effd00a/SourceGeneratorToolkit/MetadataFile.cs#L40-L48), so comments are now expected in hand-written metadata. But the location search was not made comment-aware:

Repro (both reproduced by running code)

Scoped, wrong entry — the #66 symptom returns:

{
 "things": [
  { "base": "Meter", "name": "Foot" },
  // Yard: 3 ft, not "Rod
  { "base": "Meter", "name": "Yard" },
  { "base": "Meter", "name": "Rod" }
 ]
}

FindLocation("Yard", "Meter") returns line 5 (0-based, Rod's entry); expected line 4 (Yard's). The comment's quote makes Yard's { look like string content, the enclosing object becomes the root, and the search lands in the next entry. Remove the comment line and the same call is correct.

Unscoped, location inside the comment:

{
 // "Metre" is the British spelling
 "things": [ { "name": "Metre" } ]
}

FindLocation("Metre") points at line 1, inside the comment; expected line 2.

Why it matters

A generator reporting "Yard has a bad base unit" sends the user's click to Rod's entry or to a comment, in exactly the hand-annotated files #69 set out to support.

Suggested fix

One forward lexer over Text that tracks string, escape, line-comment and block-comment state; build TryGetEnclosingObject and the token search on it (or precompute a mask of comment spans and skip matches that start inside one).

Acceptance criteria

  • Tests for: a " inside a // comment between entries (scoped lookup lands on the right entry); a quoted needle mentioned in a // comment and in a /* */ comment (location is the real entry)
  • Existing FindLocation tests still pass

Related but distinct: #68 (JSON escapes).

Activity

  1. matt-edmondson commented on Nov 12, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions