graphify icon indicating copy to clipboard operation
graphify copied to clipboard

C#: explicit type arguments at a call site (`X.M<T>(...)`) drop the `calls` edge

Open JensD-git opened this issue 3 months ago • 1 comments

Summary

In C#, any member call written with an explicit type argument list at the call site produces no calls edge, even though caller and target member both exist as nodes and the receiver is named explicitly or is an explicitly typed local. The type arguments end up inside the callee name, so the member lookup key never matches.

What decides it is the call-site syntax, not the declaration: a generic method called without an explicit type argument (type inference) resolves fine. Static vs. instance receiver, same file vs. another file, out var, one or two type arguments, block- vs. expression-bodied caller make no difference.

This is long-standing, not a regression: identical behaviour on 0.9.8, 0.9.20, 0.9.34 and 0.9.39 (same repro, same 2 of 5 edges).

Minimal repro

Five files, no build system needed. (The snippet compiles as-is: dotnet build on net8.0 reports 0 errors, 0 warnings.)

Payload.cs

namespace Demo
{
    public class Payload
    {
        public int Size;
    }
}

Registry.cs

namespace Demo
{
    public static class Registry
    {
        // generic
        public static bool Fetch<TItem>(string key) { return true; }

        // non-generic
        public static bool Has(string key) { return true; }

        // generic, but callable without an explicit type argument
        public static bool Pick<TItem>(TItem item) { return true; }
    }
}

Box.cs

namespace Demo
{
    public class Box
    {
        public bool Read<TItem>(string key) { return true; }
    }
}

Bag.cs

namespace Demo
{
    public static class Bag<TItem>
    {
        public static bool Make(string key) { return true; }
    }
}

Consumer.cs

namespace Demo
{
    public class Consumer
    {
        // 1) static call to a GENERIC method
        public bool A() { return Registry.Fetch<Payload>("k"); }

        // 2) instance call to a GENERIC method, receiver explicitly typed
        public bool B()
        {
            Box box = new Box();
            return box.Read<Payload>("k");
        }

        // 3) control: static call to a NON-generic method
        public bool C() { return Registry.Has("k"); }

        // 4) control: generic method, call site has NO type argument
        public bool D()
        {
            Payload item = new Payload();
            return Registry.Pick(item);
        }

        // 5) receiver is a generic type, method itself is not generic
        public bool E() { return Bag<Payload>.Make("k"); }
    }
}
graphify extract . --code-only --no-cluster

Expected

calls  Consumer.A()  ->  Registry.Fetch()
calls  Consumer.B()  ->  Box.Read()
calls  Consumer.C()  ->  Registry.Has()
calls  Consumer.D()  ->  Registry.Pick()
calls  Consumer.E()  ->  Bag.Make()

Actual (0.9.39, and identically on 0.9.34 / 0.9.20 / 0.9.8)

calls  Consumer.C()  ->  Registry.Has()
calls  Consumer.D()  ->  Registry.Pick()

All ten member nodes are present in the graph (listed here with their owning class node, the labels themselves are .Name()), so this is purely the match between call site and member:

Bag.Make()  Box.Read()  Registry.Fetch()  Registry.Has()  Registry.Pick()
Consumer.A()  Consumer.B()  Consumer.C()  Consumer.D()  Consumer.E()

Cases 3 and 4 are the informative controls: 4 shows that a generic declaration is fine, so the trigger is the <...> written at the call site.

Mechanism (read in 0.9.39)

_CSHARP_CONFIG (extract.py:889) resolves a call's callee through call_accessor_field="name", i.e. the name field of the member_access_expression. For a generic call that field is not an identifier but a generic_name node, and the whole node text is taken verbatim (extractors/engine.py:4769-4771):

attr = func_node.child_by_field_name(config.call_accessor_field)
if attr:
    callee_name = _read_text(attr, source)      # -> "Fetch<Payload>", not "Fetch"

_resolve_csharp_member_calls then normalises names by dropping every non-alphanumeric character (extract.py:3055):

def _key(label: str) -> str:
    return re.sub(r"[^a-zA-Z0-9]+", "", str(label)).lower()

The method index is built from member labels (extract.py:3084), so the target key is _key(".Fetch()") == "fetch", while the call site yields _key("Fetch<Payload>") == "fetchpayload". The lookup at extract.py:3206 therefore misses and the call is dropped:

method_nid = _method_on_type_or_bases(type_nid, _key(callee))
if not method_nid:
    continue  # receiver typed, but the type has no such method — skip

Because the type arguments are folded into the key by _key, the mismatch is systematic: every distinct type argument produces a distinct key, and none of them can ever match a member label. The extractor already handles generic_name correctly on the type-reference side — _read_csharp_type_name and _csharp_collect_type_refs (both in extractors/engine.py) descend into the node's name field — so the callee side is the outlier.

Case 5 is a second, independent spot with the same root cause on the receiver side: the receiver is only captured when it is a plain identifier (extractors/engine.py:4778-4780), so a generic_name receiver such as Bag<Payload> leaves member_receiver unset, and the receiver-typed resolver bails on if not receiver: continue.

Impact measured on our corpus

Counted, not sampled — over the complete .cs file set of eight graphs we build from one product's source trees, 10,331 distinct files: 272 lost calls edges, from 13,611 call sites carrying the shape. Each of the 272 was checked pair-level (graphify de-duplicates call edges per caller/target pair, so a per-line check would overstate the loss): no edge exists between the enclosing member and the target member, not even via another call. 49 further shape matches are deliberately not counted, because the calling member has no node of its own — mostly property accessors and constructors — so no edge could exist there for reasons unrelated to this issue.

The 272 come from 26 distinct (type, method) pairs, every one checked against its declaration in the source. They cluster on a few static registry/lookup helpers: the four largest account for 68, 60, 56 and 35 lost call sites. Those four are service locators, i.e. precisely the nodes a "who uses this service?" traversal depends on, so the loss costs more than its share of the graph suggests. Case-5 shape (generic receiver): 20 call sites, 8 confirmed missing edges.

A fresh 0.9.39 build of the graph carrying 116 of those losses reproduces the same 116, so the number is not an artifact of the version we run in production.

One caution for the fix

Please strip the type arguments only where the callee is matched against a receiver-typed member index — not for the evidence-free bare-name path. Reason, measured in the same corpus: receiver resolution already prefers a same-named in-corpus class over an imported external type, so relaxing the callee lookup alone would turn silence into confidently wrong edges.

Measured on 0.9.39 with a neutral three-file repro (caution/ in the attachment): a corpus class Corp.Internal.TextCodec plus two callers of TextCodec.Encode(...) — one in Corp.Internal (control), one in Corp.App, which imports a foreign namespace and does not import Corp.Internal. Both get an EXTRACTED 1.0 edge to the corpus class, including the one that means the imported foreign type.

That fallback is documented, so it is not itself news. The point is what a name-only fix would add: our corpus holds 9 generic call sites whose receiver name exists in-corpus but is not visible from the calling file. Five of the nine we verified line by line — they call a System.Text.Json API, all five files carry using System.Text.Json; and none mentions the unrelated in-corpus namespace that happens to declare a same-named class, so all five would turn into wrong edges. Two of those five files already carry wrong edges today from their non-generic calls to the same API, pointing into that unrelated internal class, whose same-named members are instance methods with unrelated signatures. The remaining four sites (three other receiver names) we did not verify individually.

The calling file's using set plus its own namespace would be enough to disambiguate; that information is already available to CsharpNameResolver.

Environment

Windows 11, Python 3.14.6, graphifyy 0.9.39 (also checked on 0.9.34, 0.9.20 and 0.9.8), graphify extract --code-only --no-cluster.

JensD-git avatar Aug 11 '26 13:08 JensD-git

I reproduced this on current v8 — 2 of the 5 calls get edges. tree-sitter wraps a call-site name carrying explicit type arguments in a generic_name, so the callee reads as Fetch<Payload> while the declaration is stored bare and the two keys never match. Bag<Payload>.Make() misses for a second reason: its receiver is a generic_name too, and only a plain-identifier receiver is captured. The same cause also drops Local<Payload>("k") and this.Local<Payload>("k"), which aren't in the repro. Opening a PR that takes the type-argument list off the call-site name so it matches the declaration, with regression tests for all of them.

rohit-jsfreaky avatar Aug 12 '26 14:08 rohit-jsfreaky