Skip to content

fix(csharp): extract past a verbatim interpolated string ending in an escaped quote (#3376) - #3612

Closed
rohit-jsfreaky wants to merge 2 commits into
Graphify-Labs:v8from
rohit-jsfreaky:fix/csharp-verbatim-interpolated-string
Closed

rohit-jsfreaky wants to merge 2 commits into
Graphify-Labs:v8from
rohit-jsfreaky:fix/csharp-verbatim-interpolated-string

Conversation

@rohit-jsfreaky

Copy link
Copy Markdown
Contributor

Fixes #3376

What

A C# file containing a verbatim interpolated string whose text ends with an interpolation hole, an escaped quote and then the terminator is now extracted in full. Before this, the bundled grammar rejected the literal and every member declared after that line was silently missing from the graph.

Why

The spelling is valid C# — dotnet build accepts it — but tree-sitter-c-sharp 0.23.5 rejects it. As the issue shows, every neighbouring spelling parses; the trigger is specifically the } "" " adjacency at the end of the literal. 0.23.5 is the newest release on PyPI and the project already allows >=0.23,<0.25, so there is no grammar bump to take.

The cost is silent. The file still extracts, just not all of it, and the warning names the parser's recovery line rather than the literal — the issue reports chasing an unrelated [InlineData] attribute because of it.

How

extract_csharp now passes the source through _normalize_csharp_verbatim_interpolation, the same source_override seam extract_js, extract_vue and extract_cpp already use.

Only the trailing escape is overwritten, with two spaces. The rewrite is byte-length preserving — nothing is inserted or deleted and no line break is touched — so every offset, line and column still points at the same place in the file on disk and reported source_location values stay accurate (the #2876 contract that _normalize_cpp_cli documents). The interpolation hole itself is kept, so identifiers referenced inside it still resolve.

Two gates keep it inert everywhere else:

  • a file that already parses is returned untouched, so healthy files never enter the rewrite;
  • if the masked source still has parse errors the original is used, leaving today's behaviour exactly as it is.

The scanner walks the file once, skipping comments, char literals, raw strings and every other string form, so the spelling written inside a comment or another string is never mistaken for code.

Testing

tests/test_csharp_verbatim_interpolation.py, 7 tests.

Before, on a class with six methods and the literal in the third:

warning: Config.cs (first error at line 20, 5 symbol(s) extracted)
extracted: BeforeOne()  BeforeTwo()  Offender()  Config  Config.cs  Demo
missing:   AfterOne()   AfterTwo()   AfterThree()

After: 10 nodes, all six methods, no warning.

Byte-level check on that same file — 783 bytes, 39 newlines:

same total length:   True
same newline count:  True
bytes changed:       2, at offsets 422 and 423 (both on line 20): b'"' -> b' '
every other byte:    identical
line number for every sampled offset: unchanged

and each member's reported location matches the line it is really on (L7, L12, L17, L24, L29, L34).

The two behavioural tests fail with the source_override hook removed and pass with it; the other five exercise the normalizer directly (length preservation, multi-line literals, two literals in one file, a healthy file left untouched, and the spelling appearing only inside comments).

Full suite: 16 failed, 5565 passed, 194 skipped. Those 16 are pre-existing — re-running the same ten files with this change reverted gives the identical 16 failed / 477 passed, and none of them touch C# or extraction. All 159 existing C# tests pass. ruff check is clean on both files.

Scope

This does not fix the grammar, which is where the bug actually is — upstream tree-sitter-c-sharp should accept that spelling. It stops graphify silently dropping symbols while the grammar is what it is, and steps aside the moment a file parses cleanly. If you would rather wait for an upstream fix instead of carrying this, that is a fair call.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 1 advisory finding(s) below merit a look before merge.

Formal verification. No changes could be formally verified in this run.


Graphify review — findings

Works around a tree-sitter-c-sharp 0.23.5 grammar bug (#3376) where a verbatim interpolated string ending in an interpolation hole, an escaped quote and the terminator causes a parse error that silently drops every member after it. extract_csharp now feeds _extract_generic a source override from _normalize_csharp_verbatim_interpolation, which parses the file, and only if it errors, masks the offending trailing "" escapes (located by _csharp_broken_escape_offsets, which skips comments, char literals and other strings) with two spaces and re-parses. The rewrite is byte-length preserving so reported line/column offsets stay accurate, and it falls back to the original source untouched when the grammar is unavailable, the file already parses, or masking doesn't clear the errors.

Worth a look

  • Quadratic scan on malformed C# prefix runs — graphify/extract.py:2546 · Escalate · medium
    • agreed by 2 of 2 members but NOT verified (no proof, no reproducing execution) — consensus is not a verdict; needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 2027 functions depend on the 267 functions this change touches.

Health — this change adds coupling hotspots:

  • new: extract() — 647 callers, 45 callees
  • new: _rebuild_code() — 129 callers, 54 callees
  • new: extract_js() — 85 callers, 4 callees
  • new: extract_xaml() — 19 callers, 17 callees
  • new: dispatch_command() — 2 callers, 125 callees
  • new: _get_extractor() — 26 callers, 6 callees
  • new: run_pipeline() — 8 callers, 13 callees
  • new: collect_files() — 17 callers, 6 callees
  • …and 30 more — each is listed as a finding

Verification — 2027 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 1852 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

118 of 287 test file(s) selected (41%) via static blast radius.

  • tests/test_astro_extraction.py — impact
  • tests/test_astro_import_ids.py — impact
  • tests/test_build.py — impact
  • tests/test_builtin_global_type_refs.py — impact
  • tests/test_case_sensitive_resolution.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_cpp_objc_cross_file_calls.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_cross_language_call_resolution.py — impact
  • tests/test_cross_repo_external_call_guards.py — impact
  • tests/test_cross_repo_member_calls.py — impact
  • tests/test_csharp_call_site_generic_args.py — impact
  • tests/test_csharp_enum_members.py — impact
  • tests/test_csharp_field_generic_args.py — impact
  • tests/test_csharp_generic_callsites.py — impact
  • tests/test_csharp_interface_dispatch.py — impact
  • tests/test_csharp_member_calls.py — impact
  • tests/test_csharp_member_nodes.py — impact
  • tests/test_csharp_object_creation.py — impact
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_csharp_type_resolution.py — impact
  • tests/test_csharp_verbatim_interpolation.py — impact, changed-test
  • tests/test_definition_file_portability.py — impact
  • tests/test_detect.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_duplicate_annotation_edges.py — impact
  • tests/test_elixir_import_resolution.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_cache_location.py — impact
  • tests/test_file_label_disambiguation.py — impact
  • tests/test_file_node_id_spec.py — impact
  • tests/test_forwarding_review_findings.py — impact
  • tests/test_go_builtin_call_targets.py — impact
  • tests/test_go_qualified_resolution.py — impact
  • tests/test_import_extension_resolution.py — impact
  • tests/test_import_self_loops.py — impact
  • tests/test_imported_export_forwarding.py — impact
  • tests/test_incremental.py — impact
  • tests/test_indirect_call_arrow_single_param_shadow.py — impact
  • tests/test_indirect_call_catch_binding_shadow.py — impact
  • tests/test_indirect_call_external_import_shadow.py — impact
  • tests/test_indirect_call_for_of_binding_shadow.py — impact
  • tests/test_indirect_call_function_expression_shadow.py — impact
  • tests/test_indirect_call_nested_closure_shadow.py — impact
  • tests/test_indirect_dispatch.py — impact
  • tests/test_indirect_dispatch_assign_return.py — impact
  • tests/test_indirect_dispatch_getattr.py — impact
  • tests/test_inferred_confidence_rubric.py — impact
  • tests/test_inherited_field_receivers.py — impact
  • … and 68 more

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

Could not verify: Could not verify extract\_csharp.

The verifier did not have enough to check extract\_csharp, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: parameter `path` is annotated `Path` — outside the synthesizable primitive/collection set

· 38 more finding(s) on lines outside this diff (see the check run).

@rohit-jsfreaky

Copy link
Copy Markdown
Contributor Author

Follow-up pushed for the review finding.

Quadratic prefix scan — confirmed, then fixed. I measured it before changing anything, on a file that is a run of $ characters:

N time (before)
500 0.013s
1,000 0.051s (3.99x)
2,000 0.216s (4.24x)
4,000 0.834s (3.86x)

Doubling N quadrupled the time, so the finding was right, and the path is reachable — the scan only runs on files the grammar already rejected, which is exactly what such a file is. The cause was mine: when no string opened at a position I advanced a single byte, so a long $/@ run was re-walked from every offset.

_csharp_string_opener now always reports where the prefix run ended, even on a miss, and the caller resumes there. After: ~1.8x per doubling, and 1,000,000 $ characters take 0.097s (the same input would have taken hours before).

Added an eighth test that feeds it 1,000,000 characters with a 5-second ceiling, so it cannot regress quietly. The end-to-end behaviour is unchanged: the repro file still yields 10 nodes with all six methods.

Separately, correcting a number in my own PR description above: I wrote "All 159 existing C# tests pass". That 159 counted this PR's own test file. The real split is 153 existing + 8 added here = 161. Apologies for the inflated figure.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

Looks safe to merge — no coupling regressions and no blocking issues, checked against the code graph (not a self-assessment).

Formal verification. No changes could be formally verified in this run.


Graphify review — findings

Works around a tree-sitter-c-sharp 0.23.5 grammar bug (#3376) where a verbatim interpolated string ending in }"" is rejected and every C# member after it silently drops from the graph. extract_csharp now runs _normalize_csharp_verbatim_interpolation, which reparses the file, locates the offending trailing "" escapes via a single-pass scan that skips comments, char literals and other strings, and overwrites each with two spaces — a byte-length-preserving mask that keeps all source locations accurate. The rewrite is only applied when the file genuinely fails to parse and the mask actually fixes it; otherwise the original source is used unchanged.

No blocking issues surfaced. 8 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 2029 functions depend on the 269 functions this change touches.

Health — this change adds coupling hotspots:

  • new: extract() — 647 callers, 45 callees
  • new: _rebuild_code() — 129 callers, 54 callees
  • new: extract_js() — 85 callers, 4 callees
  • new: extract_xaml() — 19 callers, 17 callees
  • new: dispatch_command() — 2 callers, 125 callees
  • new: _get_extractor() — 26 callers, 6 callees
  • new: run_pipeline() — 8 callers, 13 callees
  • new: collect_files() — 17 callers, 6 callees
  • …and 30 more — each is listed as a finding

Verification — 2029 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 1854 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

118 of 287 test file(s) selected (41%) via static blast radius.

  • tests/test_astro_extraction.py — impact
  • tests/test_astro_import_ids.py — impact
  • tests/test_build.py — impact
  • tests/test_builtin_global_type_refs.py — impact
  • tests/test_case_sensitive_resolution.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_cpp_objc_cross_file_calls.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_cross_language_call_resolution.py — impact
  • tests/test_cross_repo_external_call_guards.py — impact
  • tests/test_cross_repo_member_calls.py — impact
  • tests/test_csharp_call_site_generic_args.py — impact
  • tests/test_csharp_enum_members.py — impact
  • tests/test_csharp_field_generic_args.py — impact
  • tests/test_csharp_generic_callsites.py — impact
  • tests/test_csharp_interface_dispatch.py — impact
  • tests/test_csharp_member_calls.py — impact
  • tests/test_csharp_member_nodes.py — impact
  • tests/test_csharp_object_creation.py — impact
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_csharp_type_resolution.py — impact
  • tests/test_csharp_verbatim_interpolation.py — impact, changed-test
  • tests/test_definition_file_portability.py — impact
  • tests/test_detect.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_duplicate_annotation_edges.py — impact
  • tests/test_elixir_import_resolution.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_cache_location.py — impact
  • tests/test_file_label_disambiguation.py — impact
  • tests/test_file_node_id_spec.py — impact
  • tests/test_forwarding_review_findings.py — impact
  • tests/test_go_builtin_call_targets.py — impact
  • tests/test_go_qualified_resolution.py — impact
  • tests/test_import_extension_resolution.py — impact
  • tests/test_import_self_loops.py — impact
  • tests/test_imported_export_forwarding.py — impact
  • tests/test_incremental.py — impact
  • tests/test_indirect_call_arrow_single_param_shadow.py — impact
  • tests/test_indirect_call_catch_binding_shadow.py — impact
  • tests/test_indirect_call_external_import_shadow.py — impact
  • tests/test_indirect_call_for_of_binding_shadow.py — impact
  • tests/test_indirect_call_function_expression_shadow.py — impact
  • tests/test_indirect_call_nested_closure_shadow.py — impact
  • tests/test_indirect_dispatch.py — impact
  • tests/test_indirect_dispatch_assign_return.py — impact
  • tests/test_indirect_dispatch_getattr.py — impact
  • tests/test_inferred_confidence_rubric.py — impact
  • tests/test_inherited_field_receivers.py — impact
  • … and 68 more

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

Could not verify: Could not verify extract\_csharp.

The verifier did not have enough to check extract\_csharp, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: parameter `path` is annotated `Path` — outside the synthesizable primitive/collection set

· 38 more finding(s) on lines outside this diff (see the check run).

@rohit-jsfreaky
rohit-jsfreaky force-pushed the fix/csharp-verbatim-interpolated-string branch from 17811f4 to 7f0fd6d Compare October 12, 2026 11:05
@rohit-jsfreaky

Copy link
Copy Markdown
Contributor Author

Rebased onto v8 at 0.9.86 (a5ba60d), no conflicts. The new tests still fail on clean 0.9.86 and pass here. Full suite with this and my other refreshed PRs applied: 3 failed, 7324 passed. The same 3 fail on clean 0.9.86 on my machine.

safishamsi added a commit that referenced this pull request Oct 12, 2026
Python property-accessor decorators and `Self` (#4385, #4386), Swift
`Self` (#4387), Go generic type params (#4388), the Python forward
type-hint file-node fix (#4343), PowerShell dot-source paths (#4369),
and the C# verbatim-interpolated-string recovery (#3612) all change AST
extraction output, so warm caches must re-extract.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.88, just released to PyPI as graphifyy 0.9.88. Landed via an authorship-preserving cherry-pick (your commit keeps your name and counts on your contributor graph), which is why GitHub marks this closed rather than merged. Thanks @rohit-jsfreaky!

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.

C#: a verbatim interpolated string ending in }""" is rejected by the bundled grammar — file partially extracted, warning points at the recovery line

2 participants