C#: members inside `#if` / `#else` blocks produce no nodes, and calls to them produce no edges
Summary
Any C# member declared inside a preprocessor conditional (#if, #else, #elif) is absent
from the graph. No node is created for it, and call sites that target it produce no calls
edge either — the call simply disappears.
This is not about picking the "wrong" branch: neither branch is extracted. A type whose
members are all guarded by #if ends up as a class node with no methods at all, so nothing
can ever point at those members.
Conditional compilation is common in multi-targeted libraries (#if NET8_0_OR_GREATER,
#if NETFRAMEWORK, #if DEBUG), so this silently removes real API surface from the graph.
Reproducer
Two files, no dependencies, net8.0. dotnet build -c Debug → 0 warnings, 0 errors, so this
is not a case of invalid C#.
Helper.cs
namespace Probe.Lib;
public static class Gated
{
// control case: outside any preprocessor directive
public static void Outside(string a) { }
#if NET8_0_OR_GREATER
public static void InsideIf(string a) { }
#else
public static void InsideElse(string a) { }
#endif
#if DEBUG
public static void InsideDebug(string a) { }
#endif
}
Caller.cs
namespace Probe.Lib;
public static class Caller
{
public static void Drive()
{
Gated.Outside("x");
Gated.InsideIf("x");
Gated.InsideDebug("x");
}
}
Run graphify extract <dir> on the directory holding the two .cs files.
Expected: member nodes for Outside, InsideIf, InsideDebug (and arguably
InsideElse), and three calls edges from Drive.
Actual:
member nodes: ['.Drive()', '.Outside()']
calls edges: .Drive() -> .Outside() (1 of 3)
InsideIf, InsideElse and InsideDebug have no node, and the two calls to them are
missing. The Gated class node exists and carries only .Outside().
Version span
Measured with the same reproducer on 0.9.8, 0.9.20, 0.9.34 and 0.9.39 — byte-identical
result in all four (2 member nodes, 1 calls edge). This is a long-standing gap, not a
regression in a recent release.
Impact in a real code base
Counted across 8 separately extracted C# code bases (roughly 10 000 .cs files),
not sampled:
- Method declarations that sit below a preprocessor directive in the syntax tree: 233
- Of these, at least 99 have no member node in the graph.
Method: for every .cs file in each graph that contains #if, the file is parsed with
tree-sitter and every method_declaration whose ancestors include a preproc_* node is
checked against the member nodes recorded for that file. The check matches on the member
name, so a guarded method whose name also occurs outside the block counts as "present" —
99 is therefore a lower bound, not an exact figure.
A concrete consequence from that code base: a public static helper class exposes its Setup
entry point only inside #if NET8_0_OR_GREATER / #else. The class node exists and carries
its nine unguarded members, but Setup is absent — so the five call sites that invoke it can
never be connected to it, and "who calls Setup?" is unanswerable.
Notes on what is not claimed here
- No claim about which branch should win. Extracting the members of every branch would already be a large improvement over dropping all of them; picking a branch by defined symbols is a separate design question.
- The cause was not traced in the extractor source. The reproducer localises the behaviour, nothing more.
- Non-method members (properties, fields, nested types) inside conditionals were not
examined; the count above covers
method_declarationonly.
I reproduced this on current v8 and traced it to the generic walker dropping the enclosing class context when it enters C# preproc_* wrapper nodes. I am adding a focused fix that keeps those wrappers transparent inside class bodies, with regression coverage for guarded methods and their call edges.
@rohit-jsfreaky Thank you for this quick fix.
This looks fixed on the v8 branch by 931ca226 (fix(csharp): keep names that differ only by _ or case apart, in ids and in member-call keys (#4415)). Could a maintainer confirm and close?