Skip to content

[Bug]: C#: a local named required / async / partial / nint / nuint / with gives a syntax-parse error from tree-sitter-c-sharp 0.23.5 #5043

Closed

Description

@AhowCsharp

Pre-flight checks

  • I have checked the Troubleshooting section in the README

What happened?

Summary

tree-sitter-c-sharp 0.23.5 cannot parse a local variable whose name is a C# contextual keyword that can also appear as a member modifier. The grammar reads the identifier as the start of a member declaration, the enclosing block collapses into ERROR nodes, and graphify reports

warning: 1 file(s) had syntax errors and may be partially extracted: Domain/Entities/Order.cs (first error at line 2921, 276 symbol(s) extracted)

for files that dotnet build accepts with 0 errors. Declaration symbols are still extracted, but the statements in the affected region are lost, so calls and member-access edges go missing silently.

Related but distinct: #5006 (same class of bug in Swift / tree-sitter-swift), #5002 (C#: an #if block closing the class early), #3376 (the existing byte-length-preserving C# source normalization, _normalize_csharp_verbatim_interpolation).

Affected names. I swept all 41 C# contextual keywords that are legal identifiers; 6 fail. Every failing case below compiles cleanly — dotnet build, net8.0, LangVersion=latest: 0 errors, only CS0219 unused-value warnings.

identifier what triggers the ERROR fixed by @-escaping every occurrence
required a statement starting with the name: required.Add(1);, required = x;, and as the body of if (c) required.Add(1); yes
async same yes
partial same yes
nint a statement starting with the name, assignment form only (nint = 1;) yes
nuint same yes
with the declaration itself (var with = ...;), not a statement-initial use yes

Not affected (swept, all parse fine): file, scoped, record, managed, unmanaged, notnull, allows, dynamic, value, global, when, where, from, select, let, on, into, orderby, join, equals, by, ascending, descending, add, remove, get, set, init, args, alias, and, or, not, yield, nameof.

Two secondary observations

  1. The reported line number points at the wrong line. For Order.cs the warning says first error at line 2921, which is if (!stop.NoBolDeclared) — ordinary code. The real trigger is line 2922, required.Add(ImageType.BOL_PICTURE);. The if gets reported because its embedded statement never parses, which is why this looks at first like the parser choking on normal code.
  2. Only one of the affected files is reported. In our repo 3 files have root_node.has_error == True, but the warning lists only Domain/Entities/Order.cs. Domain/Services/StageDocRequirementResolver.cs and Domain/Services/TripDocCompletenessChecker.cs are partially extracted with no warning at all.

Impact in our repo (ASP.NET Core 8 Clean Architecture, ~5,700 code files, 60.7k nodes): 3 files, 8 invocation nodes and 13 member-access nodes dropped. The declaration symbol count is unchanged (245 before and after renaming the variable), so the loss is invisible unless you diff edges — graphify query just quietly returns a thinner subgraph for the affected methods.

Suggested fix shape (verified), in the same family as #3376

A C# @-escaped verbatim identifier is semantically identical to the bare name — @required is required — and tree-sitter-c-sharp parses it as a plain identifier. So the existing _normalize_csharp_* approach applies, and it can stay byte-length preserving by consuming one byte of the preceding indentation:

    required.Add(1);      ->     @required.Add(1);
^^^^ four spaces               ^^^ three spaces + '@'

Verified end-to-end on the three real files with tree_sitter_c_sharp 0.23.5, rewrite applied as re.sub(rb"(\n[ \t]*) (required\b)", rb"\1@\2", src):

file before after bytes
Domain/Entities/Order.cs has_error=True, ERROR/missing at lines 2921, 2927, 2928 has_error=False, 3 rewrites 193,799 -> 193,799
Domain/Services/StageDocRequirementResolver.cs has_error=True, ERROR/missing at lines 178, 187, 210 has_error=False, 3 rewrites 14,168 -> 14,168
Domain/Services/TripDocCompletenessChecker.cs has_error=True, ERROR/missing at line 137 has_error=False, 1 rewrite 32,221 -> 32,221

Three caveats for whoever picks this up:

  • The identifier text then reads @required, so the extractor would need to strip a leading @ when naming C# symbols. That is correct C# semantics regardless, and it would also make hand-written escapes like @class resolve to the same node as the bare name.
  • with needs the declaration escaped, not just statements, and the eat-one-indentation-byte trick does not apply mid-line. Escaping every occurrence of the name fixes all six (verified); doing it length-preservingly for a mid-line declaration needs a different trick, or that one case can be left as-is.
  • 0.23.5 is the newest tree-sitter-c-sharp on PyPI, so there is no grammar bump available — same situation as C#: a verbatim interpolated string ending in }""" is rejected by the bundled grammar — file partially extracted, warning points at the recovery line #3376.

Steps to reproduce

# 1. Minimal repro against tree_sitter_c_sharp 0.23.5 (as shipped with graphify 0.9.103)
python - <<'PY'
import tree_sitter_c_sharp as ts
from tree_sitter import Language, Parser
p = Parser(Language(ts.language()))
bad = lambda s: p.parse(s.encode()).root_node.has_error

for name in ["required", "async", "partial", "with", "ok_name"]:
    src = ("class X { void M() {\n"
           f"    var {name} = new System.Collections.Generic.List<int>();\n"
           f"    {name}.Add(1);\n"
           "} }")
    print(f"{name:10s} has_error={bad(src)}   @-escaped={bad(src.replace(name, '@' + name))}")
PY
# required   has_error=True    @-escaped=False
# async      has_error=True    @-escaped=False
# partial    has_error=True    @-escaped=False
# with       has_error=True    @-escaped=False
# ok_name    has_error=False   @-escaped=False

# 2. The same C# compiles with 0 errors, so this is a parser bug, not invalid input:
cat > K.cs <<'CS'
using System.Collections.Generic;
class K {
    void Call() {
        var required = new List<int>();
        required.Add(1);
        var async = new List<int>();
        async.Add(1);
        var partial = new List<int>();
        partial.Add(1);
        var with = new List<int>();
        with.Add(1);
    }
    void Assign() {
        object required = null;
        required = null;
        nint nint = 0;
        nint = 1;
        nuint nuint = 0;
        nuint = 1;
    }
}
CS
dotnet build        # -> Build succeeded. 5 Warning(s) (all CS0219), 0 Error(s)

# 3. In a real repo, the symptom from `graphify update .`
PYTHONHASHSEED=0 graphify update .

Error output or graph output

$ PYTHONHASHSEED=0 graphify update .
  AST extraction: 113/113 uncached files (100%) [10 workers]
  warning: 1 file(s) had syntax errors and may be partially extracted:
    Domain/Entities/Order.cs (first error at line 2921, 276 symbol(s) extracted)

Line 2921 is `if (!stop.NoBolDeclared)` and line 2922 is `required.Add(ImageType.BOL_PICTURE);`
-- the `if` is reported, the statement below it is the actual trigger.

Two more files are affected but NOT reported:
  Domain/Services/StageDocRequirementResolver.cs   root_node.has_error == True (lines 178, 187, 210)
  Domain/Services/TripDocCompletenessChecker.cs    root_node.has_error == True (line 137)

Edges lost across the three files: 8 invocation nodes, 13 member_access nodes.
Declaration symbols: 245 before and after renaming the local -- unchanged, so the
loss does not show up in any node count.

Graphify version

0.9.103

Operating System

macOS

Activity

  1. github-actions commented on Nov 6, 2026

    @github-actions

    Thanks for opening this issue, @AhowCsharp. A maintainer will take a look soon.

    If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

    To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions