From c156da1688e4e2b8b9c1dfb042094b018131d8f7 Mon Sep 17 00:00:00 2001 From: Giancarlo Erra Date: Tue, 5 May 2026 01:48:22 +0100 Subject: [PATCH] fix(graph): resolve Go imports via go.mod module path Resolves #45. Reported by @mrsuit92. Go projects produced 0 dependency edges in codebase_graph_query and codebase_graph_stats even though import extraction worked correctly. The Go case in resolveImport returned null unconditionally, with a comment that resolution required go.mod analysis. This patch adds that analysis and wires it into the resolver, mirroring the existing buildJvmSuffixMap and buildCsNamespaceMap patterns. Mechanism: - buildGoModuleInfo reads /go.mod once at graph-build time, parses the `module ` directive, and walks the file set to build a directory-to-representative-file map for every Go package. _test.go files are excluded from representative selection because Go does not allow them to be imported from non-test code in other packages. Files are sorted lexicographically for deterministic representative selection across machines and runs. Returns null when go.mod is missing or has no parseable module directive; the resolver treats null as "no Go resolution available" and behaves exactly as before this patch in those cases. - The Go case in resolveImport now strips the module path prefix from the import (handling the bare-module-path root case as well as subpackage paths) and looks up the resulting directory in the package map. Imports outside the module path return null and are treated as external dependencies (or stdlib already filtered upstream by isExternalModule). - Map keys are forward-slash paths, not OS-native, so resolution works on Windows: Go imports are always forward-slash regardless of host OS, but path.dirname produces backslashes on Windows for nested directories. Normalising the key to forward slashes at build time keeps the lookup correct across platforms. Limitations (deferred to follow-up issues if any user reports them): - The parenthesised module ( path ) form in go.mod is not parsed. Not used by any mainstream Go project (verified against cobra, gin-gonic/gin, uber-go/zap real-world go.mod files). - vendor/ directory shadowing of external imports is not honoured. - replace directives in go.mod are not honoured. - go.work multi-module workspaces are not handled (each workspace module would need its own go.mod read and prefix matching). These are real Go features but each one widens the patch and narrowly affects specific user populations. They can be added as separate small PRs if a real user hits them. Tests: 16 new cases in tests/unit/graph-resolution.test.ts covering the new buildGoModuleInfo function (parses simple go.mod, handles leading whitespace and trailing content, returns null on missing or malformed go.mod, excludes _test.go from representative selection, omits test-only directories, uses forward-slash keys for nested packages) and the Go resolveImport case (back-compat null without goModuleInfo, subpackage import resolves to lex-smallest non-test .go file, root-package import resolves to a project-root .go file, external imports return null, missing or malformed go.mod returns null, _test.go excluded from representative selection, lexically smallest file picked deterministically, similar-prefix imports do not falsely resolve, nested-package imports work cross-platform). Existing 735 unit tests continue to pass unchanged. Total: 751. typecheck, biome, and CodeRabbit local review all clean. CodeRabbit caught a real Windows path-separator bug in the first iteration; the fix and a regression test for it are included. Co-authored-by: mrsuit92 --- src/services/code-graph.ts | 12 +- src/services/graph-resolution.ts | 110 +++++++++- tests/unit/graph-resolution.test.ts | 298 +++++++++++++++++++++++++++- 3 files changed, 414 insertions(+), 6 deletions(-) diff --git a/src/services/code-graph.ts b/src/services/code-graph.ts index cc140a6..f566b56 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 { buildCsNamespaceMap, buildJvmSuffixMap, resolveImport } from "./graph-resolution.js"; +import { buildCsNamespaceMap, buildGoModuleInfo, 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"; @@ -668,6 +668,14 @@ export async function buildCodeGraph( const hasCs = files.some((f) => path.extname(f).toLowerCase() === ".cs"); const csNamespaceMap = hasCs ? buildCsNamespaceMap(fileSet, resolvedPath) : undefined; + // Build Go module-resolution info from go.mod (issue #45). Without this, + // every Go import resolved to null and Go projects produced an empty + // file graph. The info is null when go.mod is missing or unparseable; + // the resolver treats null as "no Go resolution available" and behaves + // exactly as it did before this PR for those cases. + const hasGo = files.some((f) => f.endsWith(".go")); + const goModuleInfo = hasGo ? buildGoModuleInfo(fileSet, resolvedPath) : undefined; + for (const relPath of files) { const ext = path.extname(relPath).toLowerCase(); const lang = getAstGrepLang(ext); @@ -738,7 +746,7 @@ export async function buildCodeGraph( // Try to resolve to a project file // CSS imports from