Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(extract): bump the index format for receiver-qualified Go method QNs

Martin Vogel87cafb9ef872
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index 9bf027d17..176d0a0b8 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -6696,8 +6696,13 @@ static void extract_func_def(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec
* call-scope side (compute_func_qn in extract_unified.c) mirrors
* this formula, and go_lsp consumers read the def QN and
* parent_class (receiver_type) verbatim, so resolution joins
- * stay exact. */
- def.qualified_name = cbm_arena_sprintf(a, "%s.%s", def.parent_class, name);
+ * stay exact. Go only: templ's Go-derived grammar reaches this
+ * branch too, but its call-scope QN (compute_func_qn) stays flat,
+ * and a def QN the calls do not share would orphan every call
+ * inside a templ method. */
+ if (ctx->language == CBM_LANG_GO) {
+ def.qualified_name = cbm_arena_sprintf(a, "%s.%s", def.parent_class, name);
+ }
}
}
diff --git a/src/pipeline/pass_semantic.c b/src/pipeline/pass_semantic.c
index 186b2dfdc..bc23e0d26 100644
--- a/src/pipeline/pass_semantic.c
+++ b/src/pipeline/pass_semantic.c
@@ -266,9 +266,9 @@ static int check_go_class_implements(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_nod
}
/* Resolve the struct's methods two ways and use whichever finds them:
* (a) the struct's DEFINES_METHOD edges, matched by method NAME — the real
- * pipeline path, where Go receiver methods carry a flat QN (e.g.
- * "pkg.Area" rather than "pkg.Circle.Area"), so QN-string
- * reconstruction would miss; and
+ * pipeline path. Go method QNs are receiver-qualified since #1913
+ * ("pkg.Circle.Area"), so (b) matches them as well, but the edges
+ * stay the primary join; and
* (b) the reconstructed QN "<ClassQN>.<methodName>" — used by tests and any
* extractor that does emit class-qualified method QNs without
* DEFINES_METHOD edges from the class. */
diff --git a/src/store/store.h b/src/store/store.h
index 803df81d8..0f291daf9 100644
--- a/src/store/store.h
+++ b/src/store/store.h
@@ -32,8 +32,10 @@ typedef struct cbm_store_read_scope cbm_store_read_scope_t;
* Dockerfile.<variant> keeps its suffix in its QNs.
* 3 Image identity invents no registry: a reference without a host is
* __image__<repository as written>, registry "unknown" -- no longer
- * __image__docker.io/[library/]<repository>. */
-#define CBM_INDEX_FORMAT_VERSION 3
+ * __image__docker.io/[library/]<repository>.
+ * 4 #1913: Go method QNs are receiver-qualified (pkg.Recv.method, generic
+ * receivers unwrapped); free functions stay pkg.func. */
+#define CBM_INDEX_FORMAT_VERSION 4
#define CBM_STORE_CANCELLED (-3)
#define CBM_STORE_SCAN_LIMIT (-4)
#define CBM_STORE_CALLBACK_ERR (-5)
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index 8dfc5e6e1..65919f6c3 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -7229,6 +7229,48 @@ TEST(extract_go_method_receiver_qualified_qn) {
PASS();
}
+/* Templ shares Go's method grammar (method_declaration with a receiver), but
+ * only Go method QNs are receiver-qualified (#1913); templ's call-scope QN
+ * stays flat. The method's def QN and the enclosing-function QN of a call
+ * inside it must agree, or the call has no source node and degrades to the
+ * File node. */
+TEST(extract_templ_method_qn_matches_call_scope) {
+ CBMFileResult *r = extract("package views\n"
+ "\n"
+ "type Page struct{}\n"
+ "\n"
+ "func (p Page) Title() string {\n"
+ "\treturn helper()\n"
+ "}\n"
+ "\n"
+ "func helper() string { return \"x\" }\n"
+ "\n"
+ "templ hello(name string) {\n"
+ "\t<div>{ name }</div>\n"
+ "}\n",
+ CBM_LANG_TEMPL, "t", "x.templ");
+ ASSERT_NOT_NULL(r);
+ const char *title_qn = NULL;
+ for (int i = 0; i < r->defs.count; i++) {
+ const CBMDefinition *d = &r->defs.items[i];
+ if (d->name && d->qualified_name && strcmp(d->name, "Title") == 0) {
+ title_qn = d->qualified_name;
+ }
+ }
+ const char *scope = NULL;
+ for (int i = 0; i < r->calls.count; i++) {
+ const CBMCall *c = &r->calls.items[i];
+ if (c->callee_name && strcmp(c->callee_name, "helper") == 0) {
+ scope = c->enclosing_func_qn;
+ }
+ }
+ ASSERT_NOT_NULL(title_qn);
+ ASSERT_NOT_NULL(scope);
+ ASSERT_STR_EQ(scope, title_qn);
+ cbm_free_result(r);
+ PASS();
+}
+
/* TS/JS/TSX receiver-aware flag (#592/#606; same intent as the Perl flag above).
* A member call x.foo() with a non-this/super receiver is flagged is_method so
* the resolver can suppress a weak short-name match (`re.test()` must not bind a
@@ -12301,6 +12343,7 @@ SUITE(extraction) {
RUN_TEST(extract_python_bare_call_flag_is_depth_independent);
RUN_TEST(extract_go_selector_call_flags_is_method);
RUN_TEST(extract_go_method_receiver_qualified_qn);
+ RUN_TEST(extract_templ_method_qn_matches_call_scope);
RUN_TEST(extract_ts_member_call_flags_is_method);
RUN_TEST(extract_ts_this_super_receiver_not_flagged);
RUN_TEST(extract_js_member_call_flags_is_method);
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 04925d6dc..bb4cd174d 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -8650,6 +8650,41 @@ TEST(pipeline_html_embedded_member_call_stays_unbound) {
PASS();
}
+/* CALLS edges whose source and target QNs end with the given suffixes and
+ * whose serialized properties contain `props_fragment` (NULL: any edge). QN
+ * suffixes pin the exact node, not just a same-named one. */
+static int go_calls_between(cbm_store_t *s, const char *project, const char *caller_suffix,
+ const char *callee_suffix, const char *props_fragment) {
+ cbm_edge_t *edges = NULL;
+ int edge_count = 0;
+ cbm_store_find_edges_by_type(s, project, "CALLS", &edges, &edge_count);
+ size_t cs = strlen(caller_suffix);
+ size_t ts = strlen(callee_suffix);
+ int hits = 0;
+ for (int i = 0; i < edge_count; i++) {
+ cbm_node_t src = {0};
+ cbm_node_t tgt = {0};
+ if (cbm_store_find_node_by_id(s, edges[i].source_id, &src) == CBM_STORE_OK &&
+ cbm_store_find_node_by_id(s, edges[i].target_id, &tgt) == CBM_STORE_OK &&
+ src.qualified_name && tgt.qualified_name) {
+ size_t sl = strlen(src.qualified_name);
+ size_t tl = strlen(tgt.qualified_name);
+ if (sl >= cs && tl >= ts && strcmp(src.qualified_name + sl - cs, caller_suffix) == 0 &&
+ strcmp(tgt.qualified_name + tl - ts, callee_suffix) == 0 &&
+ (!props_fragment ||
+ (edges[i].properties_json && strstr(edges[i].properties_json, props_fragment)))) {
+ hits++;
+ }
+ }
+ cbm_node_free_fields(&src);
+ cbm_node_free_fields(&tgt);
+ }
+ if (edges) {
+ cbm_store_free_edges(edges, edge_count);
+ }
+ return hits;
+}
+
TEST(pipeline_go_method_caller_keeps_lsp_join) {
/* #1909: receiver-qualifying method QNs moves the def/textual-call QN to
* package.Type.method — go_lsp's enclosing-function QN must move with it
@@ -8695,6 +8730,13 @@ TEST(pipeline_go_method_caller_keeps_lsp_join) {
cbm_store_t *s = cbm_store_open_path(db_path);
ASSERT_NOT_NULL(s);
ASSERT_TRUE(cross_file_call_exists(s, project, "Install", "helper"));
+ /* Pinned to the receiver-qualified target in svc/helper.go and to an LSP
+ * strategy: a name-only binding, or one onto other.Decoy.helper, would
+ * still satisfy the by-name check above. */
+ ASSERT_GTE(go_calls_between(s, project, ".svc.Repo.Install", ".svc.Repo.helper",
+ "\"strategy\":\"lsp_"),
+ 1);
+ ASSERT_EQ(go_calls_between(s, project, ".svc.Repo.Install", ".other.Decoy.helper", NULL), 0);
cbm_store_close(s);
cbm_pipeline_free(p);
th_rmtree(tmp);