From 0aaf3f1d7521ccbee01142c71433d78a7442fc46 Mon Sep 17 00:00:00 2001 From: Giancarlo Erra Date: Tue, 28 Apr 2026 00:04:01 +0100 Subject: [PATCH] fix(graph): resolve C# using directives via namespace scan (closes #33) C# imports previously always returned null because there is no direct filename mapping for `using X.Y.Z;`. As a result `codebase_graph_query` reported zero edges for C# projects, and the symbol-level tools (impact, flow, symbol) silently lost cross-file resolution because they walk the file-import graph for Tier 2/3 candidates. Build a namespace-to-files map once per graph build by regex-scanning every `.cs` file for `^namespace X.Y.Z` (covers both block-scoped and file-scoped C# 10+ syntax) and consult it from the csharp branch of resolveImport. When a namespace spans multiple files the first candidate is returned; multi-file fan-out is tracked as a follow-up. External namespaces (System.*, Microsoft.*) are still filtered upstream by isExternalModule, so the map is only consulted for project-internal names. Cost is O(n) reads at graph-build time, gated by a `hasCs` precheck so non-C# projects pay nothing. Adds 12 unit tests covering block-scoped, file-scoped, multi-file, multi-namespace-per-file, commented-out lines, non-.cs files, empty projects, unknown namespaces, and external (System.*) filtering. --- src/services/code-graph.ts | 13 ++- src/services/graph-resolution.ts | 71 +++++++++++- tests/unit/graph-resolution.test.ts | 173 +++++++++++++++++++++++++++- 3 files changed, 249 insertions(+), 8 deletions(-) diff --git a/src/services/code-graph.ts b/src/services/code-graph.ts index d2de430..fb5a22f 100644 --- a/src/services/code-graph.ts +++ b/src/services/code-graph.ts @@ -13,7 +13,7 @@ import type { } from "../types.js"; import { loadPathAliases } from "./graph-aliases.js"; import { extractImports } from "./graph-imports.js"; -import { buildJvmSuffixMap, resolveImport } from "./graph-resolution.js"; +import { buildCsNamespaceMap, buildJvmSuffixMap, resolveImport } from "./graph-resolution.js"; import { computeUnresolvedPct, resolveCallSites } from "./graph-symbol-resolution.js"; import { extractSymbolsAndCalls, rawCallsToUnresolvedEdges } from "./graph-symbols.js"; import { createIgnoreFilter, shouldIgnore } from "./ignore.js"; @@ -605,13 +605,20 @@ export async function buildCodeGraph( // Build a suffix lookup map for JVM multi-module projects (Java/Kotlin/Scala). // This resolves FQNs like com.example.Foo when the class lives under a nested // src/main/java/ tree (e.g. module-a/sub/src/main/java/com/example/Foo.java). - // Cost: O(n) once here, O(1) per import lookup — negligible vs. full AST parse. + // Cost: O(n) once here, O(1) per import lookup (negligible vs. full AST parse). const hasJvm = files.some((f) => { const e = path.extname(f).toLowerCase(); return e === ".java" || e === ".kt" || e === ".kts" || e === ".scala"; }); const jvmSuffixMap = hasJvm ? buildJvmSuffixMap(fileSet) : undefined; + // Build a namespace lookup map for C# projects. Each `namespace X.Y.Z` block + // (or file-scoped `namespace X.Y.Z;`) is recorded so `using X.Y.Z;` directives + // can be resolved to the file(s) that contribute to that namespace. Without + // this, every C# import resolved to null and the file graph was empty. + const hasCs = files.some((f) => path.extname(f).toLowerCase() === ".cs"); + const csNamespaceMap = hasCs ? buildCsNamespaceMap(fileSet, resolvedPath) : undefined; + for (const relPath of files) { const ext = path.extname(relPath).toLowerCase(); const lang = getAstGrepLang(ext); @@ -682,7 +689,7 @@ export async function buildCodeGraph( // Try to resolve to a project file // CSS imports from