Skip to content

Commit 6e784c3

Browse files
committed
fix(lsp): name Go init() callers per file in the Go LSP
DeusData#1910 gives every Go init() its own QN, pkg.init#<file>:<N>, on the def side and in compute_func_qn. The Go LSP computes its enclosing-function QN on its own and still produced the flat pkg.init. The pipeline joins LSP rows to textual calls by exact caller QN, so every LSP resolution inside an init body missed the join: typed calls fell back to the registry resolver and callable references (Register(fn)) were dropped. On kubernetes (8c078bc3), without this commit all 499 lsp_* CALLS from init bodies fell back (import_map, same_module, unique_name) and all 562 CALL_REFERENCE edges from them were lost; with it, the init edges match main's strategy mix. - helpers.c: cbm_go_init_qn is the one formula for all three sides (def extractor, compute_func_qn, Go LSP process_function). The per-file ordinal now walks the top-level declarations with a cursor: ts_node_named_child(root, i) is O(i) per call, so the indexed loop was quadratic on wide generated files. - go_lsp: GoLSPContext carries the file's rel_path. cbm_run_go_lsp, cbm_run_go_lsp_cross and cbm_run_go_lsp_cross_with_registry take it, so the per-file walk, the shared Tier-2 registry walk and the per-file fallback all name init callers like the def side. Tier 3 copies the per-file rows' caller QN, so it follows. - pass_lsp_cross: the dispatch passes rel to the Tier-2 walk and to the per-file fallback (cbm_pxc_run_one_with_php_bindings gains rel_path; the incremental route can run without shared registries). - compute_func_qn: a NULL base QN keeps the flat QN, as the receiver branch above it does. Tests: golsp_init_caller_qn_matches_def (per-file rows and the caller join), golsp_crossfile_init_caller_qn_per_file (both cross entry points), pipeline_go_init_lsp_edges_per_file and its _parallel twin (three inits in two files: lsp_ CALLS and a CALL_REFERENCE on the right init, no cross-wiring, nothing onto a decoy), and pipeline_go_init_dispatch_fallback_names_init_caller. Each fails with its piece reverted; the pipeline tests also fail with the compute_func_qn mirror disabled, which no test covered before. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 776f6f5 commit 6e784c3

13 files changed

Lines changed: 503 additions & 86 deletions

File tree

‎internal/cbm/cbm.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3919,7 +3919,7 @@ static CBMFileResult *extract_file_ex_body(const char *source, int source_len, C
39193919
uint64_t lsp_start = now_ns();
39203920
if (!result->lsp_skipped) {
39213921
if (language == CBM_LANG_GO) {
3922-
cbm_run_go_lsp(a, result, source, source_len, root);
3922+
cbm_run_go_lsp(a, result, source, source_len, root, rel_path);
39233923
}
39243924
if (language == CBM_LANG_C || language == CBM_LANG_CPP || language == CBM_LANG_CUDA) {
39253925
if (!cbm_run_c_lsp_with_test_owners(a, result, source, source_len, root,

‎internal/cbm/extract_defs.c‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7069,14 +7069,13 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
70697069
* above the function — the QN is node identity, and a line suffix would
70707070
* churn nodes on every unrelated insertion. Nothing ever joins on the
70717071
* plain QN — calling init explicitly is illegal in Go — and the
7072-
* call-scope side (compute_func_qn in extract_unified.c) computes the
7073-
* same cbm_go_init_ordinal so init-body calls keep their attribution. */
7072+
* call-scope side (compute_func_qn in extract_unified.c) and the Go LSP
7073+
* (process_function in lsp/go_lsp.c) name it through the same
7074+
* cbm_go_init_qn, so init-body calls keep their attribution. */
70747075
if (ctx->language == CBM_LANG_GO && strcmp(def.label, "Function") == 0 &&
70757076
strcmp(name, "init") == 0) {
7076-
const char *base = strrchr(ctx->rel_path, '/');
7077-
base = base ? base + 1 : ctx->rel_path;
7078-
def.qualified_name = cbm_arena_sprintf(a, "%s#%s:%d", def.qualified_name, base,
7079-
cbm_go_init_ordinal(node, ctx->source));
7077+
def.qualified_name =
7078+
cbm_go_init_qn(a, def.qualified_name, ctx->rel_path, node, ctx->source);
70807079
}
70817080

70827081
// C++/CUDA: out-of-line method definition (`Foo::bar` in a .cc/.cpp). The

‎internal/cbm/extract_unified.c‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,17 +1136,18 @@ static const char *compute_func_qn(CBMExtractCtx *ctx, TSNode node, const CBMLan
11361136
/* Go init(): the def extractor folds the file basename and the init's
11371137
* per-file ordinal into the QN so every init in a package survives the
11381138
* upsert (#1910, the #495 cfg-twin pattern; ordinal, not line, so the QN
1139-
* is stable under edits above the function). Mirror the exact formula
1140-
* here, or init-body calls fall back to File-node attribution
1139+
* is stable under edits above the function). Name it through the same
1140+
* cbm_go_init_qn, or init-body calls fall back to File-node attribution
11411141
* (calls_find_source). */
11421142
if (ctx->language == CBM_LANG_GO && strcmp(name, "init") == 0 &&
11431143
strcmp(ts_node_type(node), "function_declaration") == 0) {
11441144
const char *base_qn = cbm_fqn_compute_source_lang(ctx->arena, ctx->project, ctx->rel_path,
11451145
name, ctx->language);
1146-
const char *base = strrchr(ctx->rel_path, '/');
1147-
base = base ? base + 1 : ctx->rel_path;
1148-
return cbm_arena_sprintf(ctx->arena, "%s#%s:%d", base_qn, base,
1149-
cbm_go_init_ordinal(node, ctx->source));
1146+
/* NULL only on allocation failure: fall through to the flat QN, as
1147+
* the receiver branch above does. */
1148+
if (base_qn) {
1149+
return cbm_go_init_qn(ctx->arena, base_qn, ctx->rel_path, node, ctx->source);
1150+
}
11501151
}
11511152

11521153
/* Nix: a binding's own attrpath contributes scope (`a.b.fn = …`), and the def

‎internal/cbm/helpers.c‎

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,48 +2330,71 @@ int cbm_classify_string(const char *str, int len) {
23302330
return NOT_FOUND;
23312331
}
23322332

2333-
/* Flatten a JS/TS `template_string` node into plain text (issue #1006).
2334-
* String fragments are kept verbatim; each ${...} substitution becomes the
2335-
* "{}" placeholder so client URLs built from template literals share the
2336-
* canonical parameter shape of server-side route paths
2337-
* (`/things/${id}/x` -> "/things/{}/x"). Returns NULL when the node yields
2338-
* no text or exceeds the route-sized buffer. */
23392333
/* 1-based source-order ordinal of a top-level Go `init` function_declaration
2340-
* among the file's `init`s. Both the definition extractor and compute_func_qn
2341-
* fold it into the QN (`pkg.init#file.go:N`, #1910), so it must be computed
2342-
* from the tree on both sides with this one formula. Unlike a line number,
2343-
* the ordinal is stable under edits above the function — QNs are node
2344-
* identity, and a line-based suffix would churn nodes on every unrelated
2345-
* insertion (the #495 cfg suffix and the GoogleTest derivation are both
2346-
* edit-stable for the same reason). */
2347-
int cbm_go_init_ordinal(TSNode fn_node, const char *source) {
2334+
* among the file's `init`s. Unlike a line number, the ordinal is stable under
2335+
* edits above the function — QNs are node identity, and a line-based suffix
2336+
* would churn nodes on every unrelated insertion (the #495 cfg suffix and the
2337+
* GoogleTest derivation are both edit-stable for the same reason).
2338+
* One cursor pass over the top-level declarations: ts_node_named_child(root, i)
2339+
* is O(i), so an indexed loop is O(n^2) per init on a wide file, and generated
2340+
* Go (.pb.go, tables) carries thousands of top-level declarations next to its
2341+
* init(). */
2342+
static int go_init_ordinal(TSNode fn_node, const char *source) {
23482343
TSNode root = fn_node;
23492344
for (TSNode p = ts_node_parent(root); !ts_node_is_null(p); p = ts_node_parent(p)) {
23502345
root = p;
23512346
}
23522347
int ordinal = 0;
2353-
uint32_t n = ts_node_named_child_count(root);
2354-
for (uint32_t i = 0; i < n; i++) {
2355-
TSNode child = ts_node_named_child(root, i);
2356-
if (strcmp(ts_node_type(child), "function_declaration") != 0) {
2357-
continue;
2358-
}
2359-
TSNode name = ts_node_child_by_field_name(child, TS_FIELD("name"));
2360-
if (ts_node_is_null(name)) {
2361-
continue;
2362-
}
2363-
uint32_t start = ts_node_start_byte(name);
2364-
if (ts_node_end_byte(name) - start != 4 || memcmp(source + start, "init", 4) != 0) {
2365-
continue;
2366-
}
2367-
ordinal++;
2368-
if (ts_node_eq(child, fn_node)) {
2369-
return ordinal;
2370-
}
2348+
TSTreeCursor cur = ts_tree_cursor_new(root);
2349+
if (ts_tree_cursor_goto_first_child(&cur)) {
2350+
do {
2351+
TSNode child = ts_tree_cursor_current_node(&cur);
2352+
if (strcmp(ts_node_type(child), "function_declaration") != 0) {
2353+
continue;
2354+
}
2355+
TSNode name = ts_node_child_by_field_name(child, TS_FIELD("name"));
2356+
if (ts_node_is_null(name)) {
2357+
continue;
2358+
}
2359+
uint32_t start = ts_node_start_byte(name);
2360+
if (ts_node_end_byte(name) - start != 4 || memcmp(source + start, "init", 4) != 0) {
2361+
continue;
2362+
}
2363+
ordinal++;
2364+
if (ts_node_eq(child, fn_node)) {
2365+
break;
2366+
}
2367+
} while (ts_tree_cursor_goto_next_sibling(&cur));
23712368
}
2369+
ts_tree_cursor_delete(&cur);
23722370
return ordinal;
23732371
}
23742372

2373+
/* Qualified name of a Go `init` function (#1910). Go allows any number of
2374+
* init() per package — even several in one file — so the flat `pkg.init`
2375+
* collides and the graph upsert keeps one node. The QN folds in the file
2376+
* basename and the init's per-file ordinal: `pkg.init#file.go:N`. Every side
2377+
* that names an init calls this one function — the definition extractor, the
2378+
* call-scope side (compute_func_qn) and the Go LSP's enclosing-function QN —
2379+
* so the three cannot drift. A NULL base QN or path keeps base_qn as is. */
2380+
const char *cbm_go_init_qn(CBMArena *a, const char *base_qn, const char *rel_path, TSNode fn_node,
2381+
const char *source) {
2382+
if (!base_qn || !rel_path) {
2383+
return base_qn;
2384+
}
2385+
const char *base = strrchr(rel_path, '/');
2386+
base = base ? base + 1 : rel_path;
2387+
const char *qn =
2388+
cbm_arena_sprintf(a, "%s#%s:%d", base_qn, base, go_init_ordinal(fn_node, source));
2389+
return qn ? qn : base_qn;
2390+
}
2391+
2392+
/* Flatten a JS/TS `template_string` node into plain text (issue #1006).
2393+
* String fragments are kept verbatim; each ${...} substitution becomes the
2394+
* "{}" placeholder so client URLs built from template literals share the
2395+
* canonical parameter shape of server-side route paths
2396+
* (`/things/${id}/x` -> "/things/{}/x"). Returns NULL when the node yields
2397+
* no text or exceeds the route-sized buffer. */
23752398
const char *cbm_template_string_text(CBMArena *a, TSNode node, const char *source) {
23762399
enum { TPL_BUF = 512 };
23772400
char buf[TPL_BUF];

‎internal/cbm/helpers.h‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,19 @@ bool cbm_is_loop_node_type(const char *kind);
281281
// Is this a module-level node? (not nested inside function/class body)
282282
bool cbm_is_module_level(TSNode node, CBMLanguage lang);
283283

284-
/* 1-based source-order ordinal of a Go `init` among its file's top-level
285-
* `init` function_declarations (QN disambiguation, #1910). */
286-
int cbm_go_init_ordinal(TSNode fn_node, const char *source);
287-
288284
// Same check, but the node's PARENT is supplied directly — avoids the
289285
// O(n) ts_node_parent rescan. Use at call sites iterating a known
290286
// parent's children (the common case). `parent` is the parent of the
291287
// node being classified.
292288
bool cbm_is_module_level_p(TSNode parent, CBMLanguage lang);
293289

290+
/* QN of a Go `init` function_declaration (#1910): base_qn + "#<file basename>:<N>",
291+
* N = its 1-based ordinal among the file's top-level `init`s. The ONE formula
292+
* for the def extractor, compute_func_qn and the Go LSP's enclosing-function
293+
* QN. A NULL base_qn or rel_path returns base_qn unchanged. */
294+
const char *cbm_go_init_qn(CBMArena *a, const char *base_qn, const char *rel_path, TSNode fn_node,
295+
const char *source);
296+
294297
// --- FQN computation ---
295298

296299
// Compute qualified name: project.rel_path_parts.name

‎internal/cbm/lsp/go_lsp.c‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,6 +1994,8 @@ static void process_function(GoLSPContext *ctx, TSNode func_node) {
19941994
// resolver. Since #1909 receiver-qualified Go method QNs, that shared
19951995
// form is package_qn.ReceiverType.method for methods (the ONE-formula
19961996
// contract: cbm_go_receiver_type_name) and package_qn.func for functions.
1997+
// A package may hold any number of init() functions, so each one is
1998+
// package_qn.init#<file>:<N> (#1910, cbm_go_init_qn — the same formula).
19971999
ctx->enclosing_func_qn = cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->package_qn, func_name);
19982000
if (strcmp(ts_node_type(func_node), "method_declaration") == 0) {
19992001
TSNode recv = ts_node_child_by_field_name(func_node, "receiver", 8);
@@ -2004,6 +2006,9 @@ static void process_function(GoLSPContext *ctx, TSNode func_node) {
20042006
recv_type, func_name);
20052007
}
20062008
}
2009+
} else if (strcmp(func_name, "init") == 0) {
2010+
ctx->enclosing_func_qn = cbm_go_init_qn(ctx->arena, ctx->enclosing_func_qn, ctx->rel_path,
2011+
func_node, ctx->source);
20072012
}
20082013

20092014
// Push function scope
@@ -2261,7 +2266,7 @@ static void go_registry_with_stdlib(CBMTypeRegistry *reg, CBMArena *arena) {
22612266
// --- Entry point: build registry from file defs + run LSP ---
22622267

22632268
void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source, int source_len,
2264-
TSNode root) {
2269+
TSNode root, const char *rel_path) {
22652270

22662271
CBMTypeRegistry reg;
22672272
cbm_registry_init(&reg, arena);
@@ -2619,6 +2624,7 @@ void cbm_run_go_lsp(CBMArena *arena, CBMFileResult *result, const char *source,
26192624
// Phase 2: Build LSP context and run
26202625
GoLSPContext lsp_ctx;
26212626
go_lsp_init(&lsp_ctx, arena, source, source_len, &reg, module_qn, &result->resolved_calls);
2627+
lsp_ctx.rel_path = rel_path;
26222628

26232629
// Add imports
26242630
for (int i = 0; i < result->imports.count; i++) {
@@ -3218,9 +3224,9 @@ static void extract_type_params_from_ast(CBMArena *arena, CBMTypeRegistry *reg,
32183224
extern const TSLanguage *tree_sitter_go(void);
32193225

32203226
void cbm_run_go_lsp_cross(CBMArena *arena, const char *source, int source_len,
3221-
const char *module_qn, CBMLSPDef *defs, int def_count,
3222-
const char **import_names, const char **import_qns, int import_count,
3223-
TSTree *cached_tree, CBMResolvedCallArray *out) {
3227+
const char *module_qn, const char *rel_path, CBMLSPDef *defs,
3228+
int def_count, const char **import_names, const char **import_qns,
3229+
int import_count, TSTree *cached_tree, CBMResolvedCallArray *out) {
32243230
if (!source || source_len <= 0)
32253231
return;
32263232

@@ -3483,6 +3489,7 @@ void cbm_run_go_lsp_cross(CBMArena *arena, const char *source, int source_len,
34833489
// 4. Build LSP context and run
34843490
GoLSPContext ctx;
34853491
go_lsp_init(&ctx, arena, source, source_len, &reg, module_qn, out);
3492+
ctx.rel_path = rel_path;
34863493

34873494
for (int i = 0; i < import_count; i++) {
34883495
if (import_names[i] && import_qns[i]) {
@@ -3624,10 +3631,10 @@ CBMTypeRegistry *cbm_go_build_cross_registry(CBMArena *arena, CBMLSPDef *defs, i
36243631
}
36253632

36263633
void cbm_run_go_lsp_cross_with_registry(CBMArena *arena, const char *source, int source_len,
3627-
const char *module_qn, CBMTypeRegistry *reg,
3628-
const char **import_names, const char **import_qns,
3629-
int import_count, TSTree *cached_tree,
3630-
CBMResolvedCallArray *out) {
3634+
const char *module_qn, const char *rel_path,
3635+
CBMTypeRegistry *reg, const char **import_names,
3636+
const char **import_qns, int import_count,
3637+
TSTree *cached_tree, CBMResolvedCallArray *out) {
36313638
if (!source || source_len <= 0 || !reg)
36323639
return;
36333640

@@ -3653,6 +3660,7 @@ void cbm_run_go_lsp_cross_with_registry(CBMArena *arena, const char *source, int
36533660

36543661
GoLSPContext ctx;
36553662
go_lsp_init(&ctx, arena, source, source_len, reg, module_qn, out);
3663+
ctx.rel_path = rel_path;
36563664
for (int i = 0; i < import_count; i++) {
36573665
if (import_names[i] && import_qns[i]) {
36583666
go_lsp_add_import(&ctx, import_names[i], import_qns[i]);
@@ -3802,8 +3810,8 @@ void cbm_batch_go_lsp_cross(CBMArena *arena, CBMBatchGoLSPFile *files, int file_
38023810

38033811
// Delegate to existing per-file function
38043812
cbm_run_go_lsp_cross(&file_arena, file->source, file->source_len, file->module_qn,
3805-
file->defs, file->def_count, file->import_names, file->import_qns,
3806-
file->import_count, file->cached_tree, &file_out);
3813+
file->rel_path, file->defs, file->def_count, file->import_names,
3814+
file->import_qns, file->import_count, file->cached_tree, &file_out);
38073815

38083816
// Copy results to output arena (must outlive per-file arena)
38093817
if (file_out.count > 0) {

‎internal/cbm/lsp/go_lsp.h‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ typedef struct {
2222
// Current function context
2323
const char* enclosing_func_qn;
2424
const char* package_qn; // module QN for this file
25+
const char* rel_path; // repo-relative file path: init() QN suffix (#1910); NULL = flat
2526

2627
// Output: resolved calls accumulate here
2728
CBMResolvedCallArray* resolved_calls;
@@ -63,8 +64,10 @@ const CBMRegisteredFunc* go_lookup_field_or_method(GoLSPContext* ctx,
6364

6465
// Entry point: build registry from file's own defs + run LSP resolution.
6566
// Called from cbm_extract_file() after definitions and imports are extracted.
67+
// rel_path is the file's repo-relative path: it names the file's init()
68+
// functions exactly as the def extractor does (cbm_go_init_qn, #1910).
6669
void cbm_run_go_lsp(CBMArena* arena, CBMFileResult* result,
67-
const char* source, int source_len, TSNode root);
70+
const char* source, int source_len, TSNode root, const char* rel_path);
6871

6972
// Register Go stdlib types and functions into a registry.
7073
// Auto-generated by scripts/gen-go-stdlib.go.
@@ -108,11 +111,14 @@ typedef struct {
108111
// Re-parses source with tree-sitter internally.
109112
// defs includes both file-local and cross-file definitions.
110113
// import_names/import_qns are parallel arrays of length import_count.
114+
// rel_path: the file's repo-relative path, for its init() QNs (#1910); NULL
115+
// leaves init callers flat (they then miss the caller join).
111116
// Results written to out (arena-allocated).
112117
void cbm_run_go_lsp_cross(
113118
CBMArena* arena,
114119
const char* source, int source_len,
115120
const char* module_qn,
121+
const char* rel_path,
116122
CBMLSPDef* defs, int def_count,
117123
const char** import_names, const char** import_qns, int import_count,
118124
TSTree* cached_tree, // NULL = parse internally
@@ -131,6 +137,7 @@ void cbm_run_go_lsp_cross_with_registry(
131137
CBMArena* arena,
132138
const char* source, int source_len,
133139
const char* module_qn,
140+
const char* rel_path, // see cbm_run_go_lsp_cross
134141
CBMTypeRegistry* reg, // pre-built, finalized, READ-ONLY
135142
const char** import_names, const char** import_qns, int import_count,
136143
TSTree* cached_tree, // NULL = parse internally
@@ -156,6 +163,7 @@ typedef struct {
156163
const char* source;
157164
int source_len;
158165
const char* module_qn;
166+
const char* rel_path; // repo-relative path (init() QNs, #1910); NULL = flat
159167
TSTree* cached_tree; // from TSTree caching (NULL = parse internally)
160168
CBMLSPDef* defs; // combined file-local + cross-file defs
161169
int def_count;

0 commit comments

Comments
 (0)