Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
feat(graph): resolve Go imports for go.mod in subdirectories (monorepo)
anirudhkumar-nanonets62b99ed8e787
diff --git a/src/graph/build.ts b/src/graph/build.tsindex fa60e7ff..09ca6575 100644--- a/src/graph/build.ts+++ b/src/graph/build.ts@@ -8,11 +8,11 @@* Edges (M2) and LLM summary/crux (M3) layer onto this without changing it.*/import { readFileSync } from "node:fs";-import { relative, resolve } from "node:path";+import { basename, dirname, relative, resolve, sep } from "node:path";import { walkDir } from "../ingest/fs.js";import { contextDirFor } from "../context/node-file.js";import { extractFile, languageOf, type Language, type RawEdge } from "./extract.js";-import { resolveEdges } from "./resolve.js";+import { resolveEdges, type GoModule } from "./resolve.js";import { enrichGraph, type EnrichStats } from "./enrich.js";import { readGraph, writeGraph, wiringPath } from "./write.js";import { writeCards, writeIndex, writeCovers } from "./cards.js";@@ -54,15 +54,24 @@ export function listSourceFiles(root: string, outDir: string): string[] {return walkDir(root).filter((f) => !f.startsWith(outDir) && languageOf(f) !== null);}-/** The module path from `<root>/go.mod` (`module github.com/x/y`), or undefined if the- * repo isn't a Go module. Lets edge resolution map Go import paths to in-repo files. */-function readGoModule(root: string): string | undefined {- try {- const m = readFileSync(resolve(root, "go.mod"), "utf8").match(/^\s*module\s+(\S+)/m);- return m?.[1];- } catch {- return undefined;+/** Every Go module in the repo: each `go.mod`'s declared `module` path and the repo+ * directory it lives in (posix, `.` for the root). Found anywhere in the tree, so a+ * monorepo whose module is in a subdir (e.g. `backend/go.mod`) resolves too. Lets edge+ * resolution map Go import paths to in-repo files. */+function readGoModules(root: string): GoModule[] {+ const mods: GoModule[] = [];+ for (const f of walkDir(root)) {+ if (basename(f) !== "go.mod") continue;+ try {+ const m = readFileSync(f, "utf8").match(/^\s*module\s+(\S+)/m);+ if (!m) continue;+ const rel = relative(root, dirname(f)).split(sep).join("/");+ mods.push({ module: m[1], dir: rel === "" ? "." : rel });+ } catch {+ /* unreadable go.mod — skip this module */+ }}+ return mods;}export async function buildGraph(@@ -101,7 +110,7 @@ export async function buildGraph(}});- const edges = resolveEdges(nodes, rawEdges, { goModule: readGoModule(root) });+ const edges = resolveEdges(nodes, rawEdges, { goModules: readGoModules(root) });// graph.json is its own Tier-2 cache: fold in the prior meaning layer so an// unchanged body is never re-summarized (and a Tier-1-only run never wipes it).diff --git a/src/graph/resolve.ts b/src/graph/resolve.tsindex 05e0a1f3..64921ed3 100644Binary files a/src/graph/resolve.ts and b/src/graph/resolve.ts differdiff --git a/test/graph-go.test.ts b/test/graph-go.test.tsindex 946981af..43474d23 100644--- a/test/graph-go.test.ts+++ b/test/graph-go.test.ts@@ -129,3 +129,39 @@ test("Go extraction: call and import edges", async () => {rmSync(dir, { recursive: true, force: true });}});++test("Go extraction: go.mod in a subdirectory resolves intra-module imports", async () => {+ // Monorepo shape: the module lives under backend/, not the repo root.+ const dir = mkdtempSync(join(tmpdir(), "graft-go-sub-"));+ try {+ mkdirSync(join(dir, "backend", "store"), { recursive: true });+ writeFileSync(join(dir, "backend", "go.mod"), "module example.com/app\n\ngo 1.21\n");+ writeFileSync(+ join(dir, "backend", "main.go"),+ `package main\n\nimport (\n\t"fmt"\n\t"example.com/app/store"\n)\n\nfunc main() {\n\tfmt.Println(store.New())\n}\n`,+ );+ writeFileSync(join(dir, "backend", "store", "store.go"), "package store\n\nfunc New() int { return 0 }\n");++ await buildGraph(dir);+ const graph = readGraph(wiringPath(join(dir, "graft")))!;++ // `example.com/app/store` → backend/store (module dir `backend` + subpath `store`)+ const internal = graph.edges.find(+ (e) =>+ e.relation === "imports" &&+ e.source === "backend/main.go" &&+ e.target === "backend/store/store.go",+ );+ assert.ok(internal, "subdir-module import should resolve to backend/store/store.go");++ // stdlib still stays external+ assert.ok(+ graph.edges.some(+ (e) => e.relation === "imports" && e.source === "backend/main.go" && e.target === "fmt",+ ),+ "fmt should remain an external package string",+ );+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});