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): add Go language support (structs, methods, interfaces, imports)
anirudhkumar-nanonetsdad40957b032
diff --git a/package-lock.json b/package-lock.jsonindex ebcd845c..fec135b6 100644--- a/package-lock.json+++ b/package-lock.json@@ -15,6 +15,7 @@"gray-matter": "^4.0.3","openai": "^6.45.0","tree-sitter": "^0.21.1",+ "tree-sitter-go": "^0.23.4","tree-sitter-python": "^0.21.0","tree-sitter-typescript": "^0.23.2"},@@ -784,6 +785,25 @@"node-gyp-build": "^4.8.0"}},+ "node_modules/tree-sitter-go": {+ "version": "0.23.4",+ "resolved": "https://registry.npmjs.org/tree-sitter-go/-/tree-sitter-go-0.23.4.tgz",+ "integrity": "sha512-iQaHEs4yMa/hMo/ZCGqLfG61F0miinULU1fFh+GZreCRtKylFLtvn798ocCZjO2r/ungNZgAY1s1hPFyAwkc7w==",+ "hasInstallScript": true,+ "license": "MIT",+ "dependencies": {+ "node-addon-api": "^8.2.1",+ "node-gyp-build": "^4.8.2"+ },+ "peerDependencies": {+ "tree-sitter": "^0.21.1"+ },+ "peerDependenciesMeta": {+ "tree-sitter": {+ "optional": true+ }+ }+ },"node_modules/tree-sitter-javascript": {"version": "0.23.1","resolved": "https://registry.npmjs.org/tree-sitter-javascript/-/tree-sitter-javascript-0.23.1.tgz",diff --git a/package.json b/package.jsonindex 040e1d09..f2ad3606 100644--- a/package.json+++ b/package.json@@ -61,6 +61,7 @@"gray-matter": "^4.0.3","openai": "^6.45.0","tree-sitter": "^0.21.1",+ "tree-sitter-go": "^0.23.4","tree-sitter-python": "^0.21.0","tree-sitter-typescript": "^0.23.2"},diff --git a/src/graph/build.ts b/src/graph/build.tsindex fbcd0dbe..fa60e7ff 100644--- a/src/graph/build.ts+++ b/src/graph/build.ts@@ -54,6 +54,17 @@ 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;+ }+}+export async function buildGraph(dir: string,opts: GraphBuildOptions = {},@@ -90,7 +101,7 @@ export async function buildGraph(}});- const edges = resolveEdges(nodes, rawEdges);+ const edges = resolveEdges(nodes, rawEdges, { goModule: readGoModule(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/extract.ts b/src/graph/extract.tsindex 93632938..2b4e3826 100644--- a/src/graph/extract.ts+++ b/src/graph/extract.ts@@ -9,11 +9,12 @@import Parser from "tree-sitter";import TypeScript from "tree-sitter-typescript";import Python from "tree-sitter-python";+import Go from "tree-sitter-go";import { basename } from "node:path";import { contentHash } from "../util/id.js";import type { Kind, NodeV1, Relation } from "./types.js";-export type Language = "typescript" | "tsx" | "python";+export type Language = "typescript" | "tsx" | "python" | "go";/** Map a file path to a supported language, or null if unsupported. */export function languageOf(path: string): Language | null {@@ -21,6 +22,7 @@ export function languageOf(path: string): Language | null {if (p.endsWith(".tsx") || p.endsWith(".jsx")) return "tsx";if (/\.(ts|mts|cts|js|mjs|cjs)$/.test(p)) return "typescript";if (p.endsWith(".py") || p.endsWith(".pyi")) return "python";+ if (p.endsWith(".go")) return "go";return null;}@@ -98,6 +100,20 @@ const PY_KINDS: Record<string, Kind> = {function_definition: "function", // → "method" inside a class (resolved in the walk)};+// Go: `type_spec` is intentionally absent — its kind (struct/interface/type) depends on+// the named type's shape, so it's resolved dynamically in describe().+const GO_KINDS: Record<string, Kind> = {+ function_declaration: "function",+ method_declaration: "method",+};++const KINDS_BY_LANG: Record<Language, Record<string, Kind>> = {+ typescript: TS_KINDS,+ tsx: TS_KINDS,+ python: PY_KINDS,+ go: GO_KINDS,+};+const FUNCTION_VALUE_TYPES = new Set(["arrow_function","function",@@ -110,6 +126,7 @@ const GRAMMARS: Record<Language, unknown> = {typescript: TypeScript.typescript,tsx: TypeScript.tsx,python: Python,+ go: Go,};interface WalkCtx {@@ -122,9 +139,10 @@ interface WalkCtx {parentId: string; // nearest enclosing definition id, or the file id}-/** A definition we're about to emit, normalized across the two shapes we handle. */+/** A definition we're about to emit, normalized across the shapes we handle. */interface DefDescriptor {- name: string;+ name: string; // the bare symbol name (used for the node's `name` and call resolution)+ idName?: string; // id-scope segment when it differs from `name` (Go: `Receiver.method`)kind: Kind;headerEnd: number; // char index where the signature ends (body starts)hashNode: Parser.SyntaxNode; // node whose text forms body_hash / span@@ -167,7 +185,7 @@ export function extractFile(rel: string, source: string, lang: Language): Extracrel,source,lang,- kinds: lang === "python" ? PY_KINDS : TS_KINDS,+ kinds: KINDS_BY_LANG[lang],scope: [],enclosingKind: null,parentId: rel,@@ -182,7 +200,10 @@ export function extractFile(rel: string, source: string, lang: Language): Extracfunction walk(node: Parser.SyntaxNode, ctx: WalkCtx, out: NodeV1[], edges: RawEdge[]): void {const desc = describe(node, ctx);if (desc) {- const id = `${ctx.rel}#${[...ctx.scope, desc.name].join(".")}`;+ // `idName` scopes the id (e.g. a Go method under its receiver: `#DB.Count`) while+ // `name` stays the bare symbol name so member-call resolution matches it.+ const idPart = desc.idName ?? desc.name;+ const id = `${ctx.rel}#${[...ctx.scope, idPart].join(".")}`;out.push({id,name: desc.name,@@ -190,7 +211,12 @@ function walk(node: Parser.SyntaxNode, ctx: WalkCtx, out: NodeV1[], edges: RawEdpath: ctx.rel,span: `L${desc.hashNode.startPosition.row + 1}-L${desc.hashNode.endPosition.row + 1}`,signature: clean(ctx.source.slice(desc.hashNode.startIndex, desc.headerEnd)),- exported: ctx.lang === "python" ? !desc.name.startsWith("_") : tsExported(node),+ exported:+ ctx.lang === "python"+ ? !desc.name.startsWith("_")+ : ctx.lang === "go"+ ? goExported(desc.name)+ : tsExported(node),origin: "ast",body_hash: contentHash(desc.hashNode.text),body_text: searchBody(desc.hashNode.text),@@ -205,7 +231,7 @@ function walk(node: Parser.SyntaxNode, ctx: WalkCtx, out: NodeV1[], edges: RawEdconst childCtx: WalkCtx = {...ctx,- scope: [...ctx.scope, desc.name],+ scope: [...ctx.scope, idPart],enclosingKind: desc.kind,parentId: id,};@@ -226,7 +252,7 @@ function walk(node: Parser.SyntaxNode, ctx: WalkCtx, out: NodeV1[], edges: RawEdfile: ctx.rel,});}- } else if (isImport(node)) {+ } else if (isImport(node, ctx.lang)) {const spec = importSpecifier(node, ctx.lang);if (spec) edges.push({ source: ctx.rel, relation: "imports", specifier: spec, file: ctx.rel });}@@ -234,8 +260,11 @@ function walk(node: Parser.SyntaxNode, ctx: WalkCtx, out: NodeV1[], edges: RawEdfor (const child of node.namedChildren) walk(child, ctx, out, edges);}-/** Recognize the two definition shapes: mapped node types, and TS arrow-consts. */+/** Recognize the definition shapes: mapped node types, Go's type/method forms, and+ * TS arrow-consts. */function describe(node: Parser.SyntaxNode, ctx: WalkCtx): DefDescriptor | null {+ if (ctx.lang === "go") return describeGo(node, ctx);+const mapped = ctx.kinds[node.type];if (mapped) {const name = node.childForFieldName("name")?.text;@@ -249,7 +278,7 @@ function describe(node: Parser.SyntaxNode, ctx: WalkCtx): DefDescriptor | null {}// TS: `const foo = (…) => …` / `const foo = function () {}`- if (ctx.lang !== "python" && node.type === "variable_declarator") {+ if ((ctx.lang === "typescript" || ctx.lang === "tsx") && node.type === "variable_declarator") {const value = node.childForFieldName("value");if (value && FUNCTION_VALUE_TYPES.has(value.type)) {const name = node.childForFieldName("name")?.text;@@ -266,6 +295,67 @@ function describe(node: Parser.SyntaxNode, ctx: WalkCtx): DefDescriptor | null {return null;}+/** Go definition shapes: top-level funcs, receiver methods, and named types+ * (struct / interface / type alias). Methods carry no nesting — they're qualified+ * by their receiver type (`User.Save`) so calls can resolve and cards read clearly. */+function describeGo(node: Parser.SyntaxNode, _ctx: WalkCtx): DefDescriptor | null {+ if (node.type === "function_declaration") {+ const name = node.childForFieldName("name")?.text;+ if (!name) return null;+ const body = node.childForFieldName("body");+ return { name, kind: "function", headerEnd: body ? body.startIndex : node.endIndex, hashNode: node };+ }++ if (node.type === "method_declaration") {+ const name = node.childForFieldName("name")?.text;+ if (!name) return null;+ const recv = goReceiverType(node);+ const body = node.childForFieldName("body");+ // Bare `name` (so `recv.Method()` calls resolve); receiver-qualified `idName`+ // (so the id is `file.go#Receiver.Method` and stays unique per receiver).+ return {+ name,+ idName: recv ? `${recv}.${name}` : name,+ kind: "method",+ headerEnd: body ? body.startIndex : node.endIndex,+ hashNode: node,+ };+ }++ // `type Name <shape>` — one type_spec per name (grouped `type ( … )` yields several).+ if (node.type === "type_spec") {+ const name = node.childForFieldName("name")?.text;+ if (!name) return null;+ const type = node.childForFieldName("type");+ const kind: Kind =+ type?.type === "struct_type" ? "struct" : type?.type === "interface_type" ? "interface" : "type";+ // Header ends where the body opens (`{`) for struct/interface, else the whole node+ // (a one-line alias like `type ID int`).+ const headerEnd = type && (kind === "struct" || kind === "interface") ? type.startIndex : node.endIndex;+ return { name, kind, headerEnd, hashNode: node };+ }++ return null;+}++/** The receiver's base type name for a Go method, unwrapping a pointer receiver+ * (`func (u *User) …` → `User`). Null if it can't be read. */+function goReceiverType(node: Parser.SyntaxNode): string | null {+ const recv = node.childForFieldName("receiver"); // parameter_list+ const param = recv?.namedChildren.find((c) => c.type === "parameter_declaration");+ let type = param?.childForFieldName("type");+ if (type?.type === "pointer_type") type = type.namedChildren.at(-1) ?? null;+ return type?.type === "type_identifier" ? type.text : null;+}++/** Go visibility: a symbol is exported iff its own name starts with an uppercase+ * letter. For a receiver-qualified method name, the own name is the part after the dot. */+function goExported(name: string): boolean {+ const own = name.includes(".") ? name.slice(name.lastIndexOf(".") + 1) : name;+ const first = own[0] ?? "";+ return first !== first.toLowerCase() && first === first.toUpperCase();+}+function heritageEdges(node: Parser.SyntaxNode, classId: string, ctx: WalkCtx): RawEdge[] {const edges: RawEdge[] = [];if (ctx.lang === "python") {@@ -306,14 +396,22 @@ function calleeName(const a = fn.childForFieldName("attribute") ?? fn.namedChildren.at(-1);return a ? { name: a.text, viaMember: true } : null;}- if (lang !== "python" && fn.type === "member_expression") {+ if (lang === "go" && fn.type === "selector_expression") {+ // `pkg.Fn()` / `recv.Method()` — the called name is the trailing field.+ const p = fn.childForFieldName("field") ?? fn.namedChildren.at(-1);+ return p ? { name: p.text, viaMember: true } : null;+ }+ if ((lang === "typescript" || lang === "tsx") && fn.type === "member_expression") {const p = fn.childForFieldName("property") ?? fn.namedChildren.at(-1);return p ? { name: p.text, viaMember: true } : null;}return null;}-function isImport(node: Parser.SyntaxNode): boolean {+function isImport(node: Parser.SyntaxNode, lang: Language): boolean {+ // Go: match the per-import leaf, so single (`import "fmt"`) and grouped+ // (`import ( … )`) forms each yield one edge as the walk recurses into the list.+ if (lang === "go") return node.type === "import_spec";return node.type === "import_statement" || node.type === "import_from_statement";}@@ -324,6 +422,11 @@ function importSpecifier(node: Parser.SyntaxNode, lang: Language): string | nullnode.namedChildren.find((c) => c.type === "dotted_name" || c.type === "relative_import");return m?.text ?? null;}+ if (lang === "go") {+ // import_spec's `path` is an interpreted_string_literal, e.g. `"mymod/pkg/util"`.+ const path = node.childForFieldName("path") ?? node.namedChildren.at(-1);+ return path ? path.text.replace(/^["`]|["`]$/g, "") : null;+ }const str = node.namedChildren.find((c) => c.type === "string");if (!str) return null;const frag = str.namedChildren.find((c) => c.type === "string_fragment");diff --git a/src/graph/resolve.ts b/src/graph/resolve.tsindex 398a796e..05e0a1f3 100644Binary files a/src/graph/resolve.ts and b/src/graph/resolve.ts differdiff --git a/src/graph/types.ts b/src/graph/types.tsindex fdcd4a81..5584a691 100644--- a/src/graph/types.ts+++ b/src/graph/types.ts@@ -11,15 +11,16 @@* M1 populates Tier-1 only; Tier-2 fields ship as `pending`/null.*/-/** What a node represents. LSP SymbolKind, narrowed to what TS + Python produce. */+/** What a node represents. LSP SymbolKind, narrowed to what TS + Python + Go produce. */export type Kind =| "file"| "class"| "function"| "method"- | "interface" // TS only- | "type" // TS only (type alias)- | "enum"; // TS only+ | "interface" // TS + Go+ | "type" // TS + Go (type alias / named type)+ | "enum" // TS only+ | "struct"; // Go only/** How confident we are an edge is true. */export type Confidence = "extracted" | "inferred";diff --git a/test/graph-go.test.ts b/test/graph-go.test.tsnew file mode 100644index 00000000..946981af--- /dev/null+++ b/test/graph-go.test.ts@@ -0,0 +1,131 @@+/**+ * Tests for Go extraction in the Tier-1 code graph. Builds a small Go module in a+ * temp dir and asserts the emitted nodes (funcs, methods, structs, interfaces, type+ * aliases) and edges (calls, intra-module imports) match the AST walk in extract.ts.+ */+import { test } from "node:test";+import assert from "node:assert/strict";+import { mkdtempSync, writeFileSync, mkdirSync, rmSync } from "node:fs";+import { join } from "node:path";+import { tmpdir } from "node:os";+import { buildGraph } from "../src/graph/build.js";+import { readGraph, wiringPath } from "../src/graph/write.js";+import type { GraphV1, NodeV1 } from "../src/graph/types.js";++const MAIN_GO = `package main++import (+\t"fmt"+\t"mymod/pkg/util"+)++type ID int++type User struct {+\tName string+}++type Reader interface {+\tRead() error+}++func Foo() {+\tfmt.Println("hi")+\tutil.Helper()+}++func bar() {}++func (u *User) Save() error {+\tFoo()+\tu.name()+\treturn nil+}++func (u User) name() string { return u.Name }+`;++const UTIL_GO = `package util++func Helper() {}+`;++function makeFixture(): string {+ const dir = mkdtempSync(join(tmpdir(), "graft-go-"));+ writeFileSync(join(dir, "go.mod"), "module mymod\n\ngo 1.21\n");+ writeFileSync(join(dir, "main.go"), MAIN_GO);+ mkdirSync(join(dir, "pkg", "util"), { recursive: true });+ writeFileSync(join(dir, "pkg", "util", "util.go"), UTIL_GO);+ return dir;+}++function nodeById(graph: GraphV1, id: string): NodeV1 | undefined {+ return graph.nodes.find((n) => n.id === id);+}++test("Go extraction: funcs, methods, structs, interfaces, type aliases", async () => {+ const dir = makeFixture();+ try {+ const result = await buildGraph(dir); // $0, Tier-1 only+ assert.ok(result.languages.includes("go"), "languages should include go");++ const graph = readGraph(wiringPath(join(dir, "graft")));+ assert.ok(graph, "wiring graph should be written");++ // functions — exported by leading-uppercase+ const foo = nodeById(graph!, "main.go#Foo");+ assert.equal(foo?.kind, "function");+ assert.equal(foo?.exported, true);+ assert.equal(nodeById(graph!, "main.go#bar")?.exported, false);++ // methods — receiver-qualified name, file-scope, exported by method name+ const save = nodeById(graph!, "main.go#User.Save");+ assert.equal(save?.kind, "method");+ assert.equal(save?.exported, true);+ const name = nodeById(graph!, "main.go#User.name");+ assert.equal(name?.kind, "method");+ assert.equal(name?.exported, false);++ // named types+ assert.equal(nodeById(graph!, "main.go#User")?.kind, "struct");+ assert.equal(nodeById(graph!, "main.go#Reader")?.kind, "interface");+ assert.equal(nodeById(graph!, "main.go#ID")?.kind, "type");+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});++test("Go extraction: call and import edges", async () => {+ const dir = makeFixture();+ try {+ await buildGraph(dir);+ const graph = readGraph(wiringPath(join(dir, "graft")))!;++ // same-file plain call resolves: Save() → Foo()+ const call = graph.edges.find(+ (e) => e.relation === "calls" && e.source === "main.go#User.Save" && e.target === "main.go#Foo",+ );+ assert.ok(call, "Save should have a resolved calls edge to Foo");++ // member call `u.name()` resolves to the receiver method by its bare name+ const memberCall = graph.edges.find(+ (e) =>+ e.relation === "calls" && e.source === "main.go#User.Save" && e.target === "main.go#User.name",+ );+ assert.ok(memberCall, "Save should have a resolved calls edge to User.name");++ // intra-module import resolves to the target package's file node+ const internal = graph.edges.find(+ (e) => e.relation === "imports" && e.source === "main.go" && e.target === "pkg/util/util.go",+ );+ assert.ok(internal, "mymod/pkg/util should resolve to pkg/util/util.go");++ // stdlib import stays an unresolved package string+ const external = graph.edges.find(+ (e) => e.relation === "imports" && e.source === "main.go" && e.target === "fmt",+ );+ assert.ok(external, "fmt should remain an external package string");+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});