Skip to content

C# collection expression in a ternary branch fails to parse (reported as parse_partial) #1748

Description

@maksymshiftlive

Summary

A C# 12 collection expression used as a branch of a conditional (ternary) expression is not parsed
correctly. The file is reported as parse_partial, and the error range typically extends past the offending
statement, which suggests the parser loses sync and only recovers a few lines later — so neighbouring
declarations can go missing from the graph too, not just the conditional itself.

The simplest single-line form parses fine, so this only shows up once the expression is wrapped across lines
(the normal formatting for these) or once both branches are collection expressions.

Minimal reproduction

M.cs — the common real-world shape, a wrapped ternary with an empty collection expression:

class M
{
    async Task Go(bool c)
    {
        var x = c
            ? []
            : await F();
    }
    Task<List<int>> F() => null;
}
codebase-memory-mcp cli index_repository --repo_path=. --mode=fast --name=repro

Expected

parse_partial count: 0

Actual

parse_partial count: 1
  FLAGGED: M.cs -> 5-7

Variants tested

All of these are one-method files differing only in the marked way, each indexed as its own file in a single
run:

Body Result
var x = c
? []
: await F();
flagged 5-7
var x = c ? []
: await F();
flagged 5-6
List<int> x = c
? []
: [items.First()];
flagged 5-7
List<int> x = c ? [] : [1]; (single line, both branches) flagged 5-5
var x = c
? null
: await F(); (same shape, no collection expression)
clean
var x = c ? [] : items; (single line, one branch) clean
List<int> x = c ? [] : items; (single line, one branch) clean
var x = c ? [] : await F(); (single line, one branch) clean
return c ? [] : items; (single line, one branch) clean
var x = c ? items : []; (single line, second branch) clean
var x = c switch { 0 => [], _ => items }; (switch expression) clean
Take([1], items); (collection expression as an argument) clean

So collection expressions are handled correctly in general — as arguments, in switch expression arms, and in
a single-line conditional with one collection branch. It breaks when the conditional is split across lines,
or when both branches are collection expressions.

I did not fully characterise the boundary: at least one real-world site that is single-line with a single
collection branch is also flagged (a return c ? [] : items; under a brace-less if), so the trigger is
probably somewhat broader than the table above proves.

Impact

In one .NET 10 solution here this accounts for 8 of the flagged C# files. ? [] as a "no results" branch is
an extremely common idiom since C# 12, so the pattern tends to recur across a codebase rather than appear
once.

Environment

  • codebase-memory-mcp 0.10.8 (Windows x64 build)
  • Windows 11 Pro 10.0.22631
  • Reproduced via both the MCP index_repository tool and the cli index_repository one-shot path
  • C# sources targeting .NET 10 / C# 13-14

Notes

Probably upstream in the tree-sitter C# grammar rather than in this project's own code — plausibly a
[ ambiguity between a collection expression and an attribute list when [ starts a wrapped line. Feel free
to reclassify or forward.

Metadata

Metadata

Assignees

No one assigned

    Labels

    windowsWindows-specific issues

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions