graphify icon indicating copy to clipboard operation
graphify copied to clipboard

C#: partial-class merge (#2332) ignores assembly boundaries, conflating distinct types

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

Version: 0.9.32 (regression vs. 0.9.31) Related: #2332 ("partial classes split across files now attach to one merged class node")

Summary

In C#, partial is an assembly-local concept. Two partial class Foo declarations in the same namespace but different assemblies are two distinct types — they never fuse. 0.9.32 merges them into a single class node based on the fully-qualified name (FQN) alone, so unrelated types end up sharing one node and one member list.

Minimal reproducer

src/AsmOne/AsmOne.csproj          <AssemblyName>AsmOne</AssemblyName>
src/AsmOne/Widget.cs              namespace Shared { public partial class Widget { public int OnlyInAssemblyOne()  { return 1; } } }
src/AsmOne/Widget.Part2.cs        namespace Shared { public partial class Widget { public int AlsoInAssemblyOne()  { return 2; } } }
src/AsmTwo/AsmTwo.csproj          <AssemblyName>AsmTwo</AssemblyName>
src/AsmTwo/Widget.cs              namespace Shared { public partial class Widget { public int OnlyInAssemblyTwo()  { return 3; } } }

graphify extract src:

version Widget class nodes members attached
0.9.31 3 (one per file) one per file
0.9.32 1 all 3, across both assemblies

Expected: 2 class nodes — AsmOne.Widget (2 members, merged across its two files: that part of #2332 is correct and welcome) and AsmTwo.Widget (1 member).

Note the surviving node's source_file is AsmOne/Widget.Part2.cs, i.e. the merge also picked a file from a different assembly than some of the members it now owns.

Suggested fix

Key the partial merge on (FQN, assembly) instead of the FQN alone, where assembly = nearest ancestor directory containing a *.csproj (fallback: the file's own directory). That is the exact fix we had to apply in our own post-processing tool after hitting this same bug — the FQN-only key produced a large share of wrong groups on a real code base.

Files that are shared between projects via linked compile items (<Compile Include="..\Other\File.cs"><Link>…</Link></Compile>) legitimately belong to several assemblies. Attributing such a file to its containing directory's project (as above) keeps the merge conservative: it never fuses two assemblies, which is the important invariant.

Real-world impact

Measured on a production C# portfolio (8 graphs, identical sources, 0.9.31 vs 0.9.32):

  • 249 class FQNs collapsed across assembly boundaries (in one graph, 240 of 491 FQNs that legitimately exist in more than one assembly — about half)
  • 5 327 method nodes now hang under a class node from a different assembly
  • 18 776 edges touch a conflated node
  • 96 calls edges were silently rerouted into the wrong assembly: a call that resolved to …/DataClient/X.cs .Foo() in 0.9.31 now resolves to …/DataEntities/X.cs .Foo() — same member name, different type, different assembly. Separating rerouted edges from real changes, the portfolio delta is +177 genuinely new calls edges (the welcome #2346 effect), −11 genuine losses, and 96 false reroutes.

Typical case: a domain model generated by a T4 (Text Template Transformation Toolkit) template exists twice — once as a WCF (Windows Communication Foundation) client proxy (AssemblyName CollaborationDataClient, net45 portable) and once as an Entity Framework (EF) entity (AssemblyName CollaborationDataEntities, net471). Same namespace, same type name, different assemblies, different members (25 vs. 23 methods). Under 0.9.32 both become one node with 48 methods, so a query can no longer tell the client-side type from the database-side type — the layer separation that the analysis depends on is gone.

The within-assembly part of #2332 (root file + generated/ file of the same project) works correctly and is a genuine improvement; on the same portfolio it removed 2 534 duplicate class nodes.

Because the merge happens during extraction, downstream post-processing cannot repair it: a consumer can merge nodes, but it cannot reconstruct which of the fused declarations a given member or edge belonged to.

JensD-git avatar Aug 03 '26 06:08 JensD-git

Fixed in v0.9.33 (graphifyy==0.9.33 on PyPI). The partial-class merge now keys on assembly (nearest ancestor directory containing a .csproj/.fsproj/.vbproj) in addition to namespace and name, so two same-named partial classes in different assemblies stay distinct while genuine partial halves within one project still merge. A corpus with no project file keeps merging by namespace and name. Thanks for the precise diagnosis and the measured impact, that made this quick to pin down.

safishamsi avatar Aug 05 '26 00:08 safishamsi

Confirmed on our side — thank you, and thanks for turning it around this quickly.

We rebuilt our whole C# portfolio (8 graphs, both versions from the same source tree to rule out drift) and verified against the assembly boundary:

0.9.32 0.9.33
class FQNs collapsed across assemblies 249 0
method nodes attached to a foreign assembly's class 5 327 0
rerouted calls edges 96 0

The strongest signal is our largest solution: it contains 491 fully-qualified names that legitimately exist in more than one assembly (240 of them collapsed under 0.9.32). Under 0.9.33 not a single one collapses, while genuine partial halves within one project still merge correctly — including classes split across subdirectories of the same project, which we specifically checked because a naive directory comparison would get those wrong.

Net effect on calls edges after our post-processing: +168, with 0 reroutes. The +177 real edges we mentioned in the report arrived as expected.

We have promoted 0.9.33 to production.

One note for completeness, unrelated to this fix: while bisecting the remaining edge differences we found a separate receiver-typing regression that arrived in 0.9.32 (two declarations of the same identifier in one method, one of them untypeable, drop the typing for both). Filed separately as #2472 so it does not get mixed up with the partial-class work here.

JensD-git avatar Aug 05 '26 08:08 JensD-git