Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- On Windows, the `codegraph` command in Git Bash now works in a terminal that turns off path conversion with `MSYS_NO_PATHCONV=1` or `MSYS2_ARG_CONV_EXCL=*`, as some agent harnesses do, instead of failing with "Cannot find module". Thanks @BlueberryDuck for the report. (#3464)
- On projects with fewer than 500 files, every tool named in `CODEGRAPH_MCP_TOOLS` is now listed to the agent, instead of only the small-repo defaults. Thanks @nchrisr for the report and the fix. (#3462)
- `codegraph callers` and `codegraph_callers` list callers outside tests before tests, so a symbol called mostly from tests no longer shows only tests under the default limit while its real callers are cut. Thanks @blairjordan for the report and the fix. (#3456)
- In Go, a variable given a one-line chain that starts with `new(T)`, `make(T)` or a struct literal, like `sub := new(Route).Host("x").Subrouter()`, now has the type the chain returns, so a method called on it links to that type's method, not to a same-named method of the type the chain starts from. Thanks @inth3shadows for the report and the analysis. (#3447)
- Go calls on a local variable link to the right method when its value is a method chain split across lines after a dot. Thanks @maxmilian for the report and @zhangkom for the fix. (#3455)
- In Go, a variable given a chain that starts with `new(T)`, `make(T)` or a struct literal, like `sub := new(Route).Host("x").Subrouter()`, now has the type the chain returns, so a method called on it links to that type's method, not to a same-named method of the type the chain starts from. Thanks @inth3shadows for the report and the analysis, and @maxmilian. (#3447)
- In Go, a variable given a method chain broken across lines after its `.`s, the way gofmt leaves a long chain, now has the type the chain returns, so a method called on it links again. Thanks @maxmilian for the report and @zhangkom for the fix. (#3455)

## [1.6.4] - 2026-11-20

Expand Down
209 changes: 209 additions & 0 deletions __tests__/go-broken-chain-locals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
/**
* gofmt breaks a long Go chain after its `.`s:
*
* sub := NewRoute().
* Host("x").
* Subrouter()
*
* A local's value was read to the end of its declaration's line, so it got
* no type from such a chain, or the type of a `new(T)` or literal it starts
* with, and `sub.Path("/a")` linked nothing or the wrong `Path` (#3455). A
* line that ends at a `.` now carries the value on, in a `:=`, a `var` and an
* `if` header, whatever the chain starts from.
*
* The fixture is the issue's own: each function calls `Path` on a local the
* way it names, and the table is where that call should link.
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import CodeGraph from '../src/index';

const SOURCE = `package repro

type Router struct{}
type Route struct{}
type Plain struct{ Name string }

func (r *Router) Path(p string) *Route { return &Route{} }
func (r *Route) Path(p string) *Route { return r }
func (r *Route) Host(h string) *Route { return r }
func (r *Route) Subrouter() *Router { return &Router{} }
func (p Plain) Path(s string) *Route { return &Route{} }
func (p Plain) Router() *Router { return &Router{} }

func NewRoute() *Route { return &Route{} }

func funcOneLine() {
sub := NewRoute().Host("x").Subrouter()
sub.Path("/a")
}

func funcBroken() {
sub := NewRoute().
Host("x").
Subrouter()
sub.Path("/a")
}

func funcBrokenOnce() {
sub := NewRoute().Host("x").
Subrouter()
sub.Path("/a")
}

func funcBrokenInParens() {
sub := NewRoute().Host(
"x",
).Subrouter()
sub.Path("/a")
}

func localOneLine() {
rt := NewRoute()
sub := rt.Host("x").Subrouter()
sub.Path("/a")
}

func localBroken() {
rt := NewRoute()
sub := rt.
Host("x").
Subrouter()
sub.Path("/a")
}

func varBroken() {
var sub = NewRoute().
Host("x").
Subrouter()
sub.Path("/a")
}

func ifBroken() {
if sub := NewRoute().
Host("x").
Subrouter(); sub != nil {
sub.Path("/a")
}
}

func ptrLiteralOneLine() {
sub := (&Route{}).Host("x").Subrouter()
sub.Path("/a")
}

func ptrLiteralBroken() {
sub := (&Route{}).
Host("x").
Subrouter()
sub.Path("/a")
}

func literalOneLine() {
sub := Plain{}.Router()
sub.Path("/a")
}

func literalBroken() {
sub := Plain{}.
Router()
sub.Path("/a")
}

func literalFieldsBroken() {
sub := Plain{
Name: "x",
}.
Router()
sub.Path("/a")
}

func newOneLine() {
sub := new(Route).Subrouter()
sub.Path("/a")
}

func newBroken() {
sub := new(Route).
Subrouter()
sub.Path("/a")
}

func newBuilderBroken() {
b := new(Route).
Host("x")
b.Path("/a")
}

func directBroken() {
NewRoute().
Host("x").
Subrouter().
Path("/a")
}

func returnBroken() *Route {
return NewRoute().
Host("x").
Subrouter().
Path("/a")
}
`;

/** Where each function's `Path` call links, from the issue's "Expected". */
const EXPECTED: Record<string, string> = {
funcOneLine: 'Router::Path',
funcBroken: 'Router::Path',
funcBrokenOnce: 'Router::Path',
funcBrokenInParens: 'Router::Path',
localOneLine: 'Router::Path',
localBroken: 'Router::Path',
varBroken: 'Router::Path',
ifBroken: 'Router::Path',
ptrLiteralOneLine: 'Router::Path',
ptrLiteralBroken: 'Router::Path',
literalOneLine: 'Router::Path',
literalBroken: 'Router::Path',
literalFieldsBroken: 'Router::Path',
newOneLine: 'Router::Path',
newBroken: 'Router::Path',
// The chain returns the type it starts from.
newBuilderBroken: 'Route::Path',
directBroken: 'Router::Path',
returnBroken: 'Router::Path',
};

let root = '';
let cg: CodeGraph;

beforeAll(async () => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-go-broken-chain-'));
fs.writeFileSync(path.join(root, 'go.mod'), 'module repro\n\ngo 1.21\n');
fs.writeFileSync(path.join(root, 'm.go'), SOURCE);
cg = await CodeGraph.init(root, { index: true });
}, 60_000);

afterAll(() => {
cg?.close();
if (root) fs.rmSync(root, { recursive: true, force: true });
});

/** The `Path` methods the function `name` calls, by qualified name. */
function pathCalls(name: string): string[] {
const fn = cg.getNodesByName(name).find((n) => n.kind === 'function');
expect(fn, name).toBeDefined();
return cg.getOutgoingEdges(fn!.id)
.filter((e) => e.kind === 'calls')
.map((e) => cg.getNode(e.target))
.filter((n) => n?.name === 'Path')
.map((n) => n!.qualifiedName)
.sort();
}

describe('a Go local given a chain broken after its `.`s (#3455)', () => {
it.each(Object.entries(EXPECTED))('%s calls %s', (name, target) => {
expect(pathCalls(name)).toEqual([target]);
});
});
20 changes: 10 additions & 10 deletions __tests__/go-chain-head-locals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* by the start of its value alone, `new(Route)`, so `sub.Path("/a")` reached
* `Route`'s `Path`: gorilla/mux's subrouter tests are written this way. Only
* a value that is the `new(T)` or the literal and nothing more types the
* local by what it writes; a chain is read like any other call's result. A
* chain broken after the `.` is left as it was, typed by its first line.
* local by what it writes; a chain is read like any other call's result,
* across its lines too when it is broken after a `.` (#3455).
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import * as fs from 'fs';
Expand Down Expand Up @@ -223,18 +223,18 @@ describe.each(['default', 'wasm'])('a Go local given a chain is typed by what th
expect(pathCalls('fromVar')).toEqual(ROUTER);
});

it('types a chain broken after the `.` by its start, as before: right when the chain returns that type', () => {
it('types a chain broken after the `.` by what it returns, here the type it starts from', () => {
expect(pathCalls('builderBroken')).toEqual(['b_route.go::Route::Path']);
expect(pathCalls('builderBrokenCommented')).toEqual(['b_route.go::Route::Path']);
});

// Not fixed here: a `:=` value is read to its line's end, so these keep the
// type their first line writes, and a function's broken chain has none.
it('leaves a broken chain that returns another type as it was', () => {
expect(pathCalls('fromNewBroken')).toEqual(['b_route.go::Route::Path']);
expect(pathCalls('fromLiteralBroken')).toEqual(['b_route.go::Plain::Path']);
expect(pathCalls('fromFilledLiteralBroken')).toEqual(['b_route.go::Plain::Path']);
expect(pathCalls('fromFuncBroken')).toEqual([]);
// The value is read on past a line that ends at a `.` (#3455), so a broken
// chain has the type it returns, whatever it starts from.
it('reads a chain broken after the `.` across its lines', () => {
expect(pathCalls('fromNewBroken')).toEqual(ROUTER);
expect(pathCalls('fromLiteralBroken')).toEqual(ROUTER);
expect(pathCalls('fromFilledLiteralBroken')).toEqual(ROUTER);
expect(pathCalls('fromFuncBroken')).toEqual(ROUTER);
});

it('reads a chain that starts with `make(T)` the same way', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/resolution/name-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3727,13 +3727,13 @@ function readGoScopes(code: string): GoScopeIndex {
/^[ \t]*new\([ \t]*((?:[A-Za-z_]\w*\.)?[A-Za-z_]\w*)[ \t]*\)/.exec(rhs);
// A selector after the literal or the `new(T)` makes it the start of a
// chain, `new(Route).Host("x").Subrouter()`: the local has the type the
// chain gives, read with its value like any call's result (#3447). Not
// when the line ends at the `.`: a value is read to its line's end, so a
// chain broken there keeps the type its first line writes.
// chain gives, read with its value like any call's result (#3447). So
// does one at a line's end, the way gofmt breaks a long chain: the value
// is read on past a line that ends at a `.` (#3455).
let after = i + 1 + (written?.[0].length ?? 0);
if (written && code[after] === '{') after = close.get(after) ?? code.length;
do after++; while (code[after] === ' ' || code[after] === '\t');
const onward = /\.[ \t]*\S/y;
const onward = /\.\s*\S/y;
onward.lastIndex = after;
const literal = onward.test(code) ? undefined : written?.[1];
const type = names.length === 1 && literal && !GO_TYPE_KEYWORDS.has(literal) ? literal : undefined;
Expand Down