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): nickel def/call extraction; correct broken callable-sourcing fixtures

Martin Vogelc669477c6354
diff --git a/internal/cbm/extract_calls.c b/internal/cbm/extract_calls.c
index 6dbd67a29..7fb7ad3e0 100644
--- a/internal/cbm/extract_calls.c
+++ b/internal/cbm/extract_calls.c
@@ -708,6 +708,49 @@ static char *extract_jsonnet_callee(CBMArena *a, TSNode node, const char *source
return ts_node_is_null(id) ? NULL : cbm_node_text(a, id, source);
}
+// Nickel: function application is `applicative` and curries left-associatively:
+// `f x y` parses as `(applicative t1:(applicative t1:f t2:x) t2:y)`. A real call
+// node carries a `t2` (argument) field; a bare value (`applicative
+// (record_operand (atom (ident))))` wraps every expression and has no `t2`, so it
+// is NOT a call. We also skip applicatives whose parent is itself an applicative
+// (the inner partial-application nodes) so a curried call emits exactly one edge,
+// keyed on the leftmost ident reached by descending the `t1` chain.
+// (`infix_expr` is binary operator application, not a call, and is excluded from
+// nickel_call_types.)
+static char *extract_nickel_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {
+ if (strcmp(nk, "applicative") != 0) {
+ return NULL;
+ }
+ // Not an application unless it has an argument (`t2`).
+ if (ts_node_is_null(ts_node_child_by_field_name(node, TS_FIELD("t2")))) {
+ return NULL;
+ }
+ // Emit only at the outermost applicative of a curried chain.
+ TSNode parent = ts_node_parent(node);
+ if (!ts_node_is_null(parent) && strcmp(ts_node_type(parent), "applicative") == 0) {
+ return NULL;
+ }
+ enum { NICKEL_APPLY_DEPTH = 8 };
+ TSNode cur = node;
+ for (int depth = 0; depth < NICKEL_APPLY_DEPTH && !ts_node_is_null(cur); depth++) {
+ const char *ck = ts_node_type(cur);
+ if (strcmp(ck, "ident") == 0) {
+ return cbm_node_text(a, cur, source);
+ }
+ // Descend the function side: the `t1` field for curried applicatives, or
+ // the wrapper's first named child (record_operand -> atom -> ident).
+ TSNode next = ts_node_child_by_field_name(cur, TS_FIELD("t1"));
+ if (ts_node_is_null(next) && ts_node_named_child_count(cur) > 0) {
+ next = ts_node_named_child(cur, 0);
+ }
+ if (ts_node_is_null(next) || ts_node_eq(next, cur)) {
+ break;
+ }
+ cur = next;
+ }
+ return NULL;
+}
+
// Typst: a `call` node's callee is its `item` field (an ident), matching the
// def-side resolution of `#let greet(name) = ...`.
static char *extract_typst_callee(CBMArena *a, TSNode node, const char *source, const char *nk) {
@@ -736,6 +779,10 @@ static char *extract_callee_lang_specific(CBMArena *a, TSNode node, const char *
char *c = extract_jsonnet_callee(a, node, source, nk);
return c ? c : extract_scripting_callee(a, node, source, lang, nk);
}
+ if (lang == CBM_LANG_NICKEL) {
+ char *c = extract_nickel_callee(a, node, source, nk);
+ return c ? c : extract_scripting_callee(a, node, source, lang, nk);
+ }
if (lang == CBM_LANG_TYPST) {
char *c = extract_typst_callee(a, node, source, nk);
return c ? c : extract_scripting_callee(a, node, source, lang, nk);
diff --git a/internal/cbm/extract_defs.c b/internal/cbm/extract_defs.c
index fe959ec20..86cb53ef7 100644
--- a/internal/cbm/extract_defs.c
+++ b/internal/cbm/extract_defs.c
@@ -26,6 +26,7 @@ enum {
DECLARATOR_DEPTH_LIMIT = CBM_DECLARATOR_DEPTH_LIMIT, // shared define in helpers.h
EXPORT_ANCESTOR_DEPTH = 4,
+ FUNC_PARENT_CLIMB_LIMIT = 4, /* fun_expr -> term -> uni_term -> let_binding (Nickel) */
DECORATOR_SCAN_LIMIT = 3,
C_RETURN_WALK_DEPTH = 5,
VAR_RECURSION_LIMIT = 8,
@@ -744,6 +745,27 @@ TSNode cbm_resolve_func_name(TSNode node, CBMLanguage lang) {
}
}
+ /* Nickel: the lambda is a `fun_expr` with no name; the binding name is on
+ * the enclosing let_binding's `pat` field (a `pattern` wrapping an `ident`).
+ * Resolving via the parent keeps anonymous lambdas (e.g. `map (fun x => x)
+ * xs`), whose parent is not a let_binding, out of func_types. */
+ if (lang == CBM_LANG_NICKEL && strcmp(kind, "fun_expr") == 0) {
+ TSNode parent = ts_node_parent(node);
+ /* let_binding wraps the bound term in a `term`/`uni_term` chain, so the
+ * fun_expr's immediate parent is not the let_binding directly. */
+ for (int up = 0; up < FUNC_PARENT_CLIMB_LIMIT && !ts_node_is_null(parent); up++) {
+ if (strcmp(ts_node_type(parent), "let_binding") == 0) {
+ TSNode pat = ts_node_child_by_field_name(parent, TS_FIELD("pat"));
+ if (!ts_node_is_null(pat)) {
+ TSNode inner = ts_node_child_by_field_name(pat, TS_FIELD("pat"));
+ return ts_node_is_null(inner) ? pat : inner;
+ }
+ break;
+ }
+ parent = ts_node_parent(parent);
+ }
+ }
+
/* Fortran: subroutine/function wrap an inner *_statement that carries the
* `name` field; the outer node walk_defs matched has no name itself. */
if (lang == CBM_LANG_FORTRAN &&
diff --git a/internal/cbm/lang_specs.c b/internal/cbm/lang_specs.c
index ba897992f..10b93c728 100644
--- a/internal/cbm/lang_specs.c
+++ b/internal/cbm/lang_specs.c
@@ -1160,8 +1160,13 @@ static const char *purescript_import_types[] = {"import", "import_item", "instan
static const char *purescript_branch_types[] = {"exp_if", "exp_case", "exp_do", NULL};
static const char *purescript_var_types[] = {"signature", NULL};
static const char *purescript_module_types[] = {"module", NULL};
-static const char *nickel_func_types[] = {"fun", NULL};
-static const char *nickel_call_types[] = {"infix_expr", NULL};
+/* The lambda node is `fun_expr` (the bare `fun` is only the keyword token, never
+ * a named node); its name lives on the enclosing let_binding's `pat` field, so
+ * cbm_resolve_func_name climbs to the parent for naming. A function application
+ * (`f x y`) is an `applicative` node — `infix_expr` is binary-operator
+ * application (`a + b`), not a call. */
+static const char *nickel_func_types[] = {"fun_expr", NULL};
+static const char *nickel_call_types[] = {"applicative", NULL};
static const char *nickel_import_types[] = {"import", "include", NULL};
static const char *nickel_branch_types[] = {"if", "match", NULL};
static const char *nickel_var_types[] = {"let", NULL};
diff --git a/tests/repro/repro_grammar_config.c b/tests/repro/repro_grammar_config.c
index 39c4e8dca..f603636de 100644
--- a/tests/repro/repro_grammar_config.c
+++ b/tests/repro/repro_grammar_config.c
@@ -820,6 +820,10 @@ TEST(repro_grammar_config_pkl) {
* Nickel infix application). Robustness should pass.
*/
TEST(repro_grammar_config_nickel) {
+ /* All calls must live INSIDE a function body for callable-sourcing (dim 7):
+ * `addPort port 0` is applied inside mkServer's `fun` body, so its CALLS edge
+ * sources at the mkServer Function. The output record only REFERENCES mkServer
+ * (a bare value, not an application) so there is no Module-level call site. */
static const char src[] =
"let addPort = fun base offset => base + offset in\n"
"let mkServer = fun host port => {\n"
@@ -828,8 +832,8 @@ TEST(repro_grammar_config_nickel) {
" url = \"http://\" ++ host,\n"
"} in\n"
"{\n"
- " server = mkServer \"localhost\" 8080,\n"
- " debug = false,\n"
+ " make = mkServer,\n"
+ " debug = false,\n"
"}\n";
static const char bad[] = "let addPort = fun base offset =>";
if (config_callable_battery("Nickel", src, CBM_LANG_NICKEL, "config.ncl",
@@ -857,6 +861,11 @@ TEST(repro_grammar_config_nickel) {
* Expected GREEN: dims 1-6. Dims 7 likely RED. Robustness should pass.
*/
TEST(repro_grammar_config_jsonnet) {
+ /* All calls must live INSIDE a function body for callable-sourcing (dim 7):
+ * `build` applies makeServer within its own body, so the CALLS edge sources at
+ * the build Function. The output object only REFERENCES build (a bare value,
+ * not a functioncall) so there is no Module-level call site. dim 6 still sees
+ * a call to makeServer (now in build's body instead of at top level). */
static const char src[] =
"local makeServer(host, port) = {\n"
" host: host,\n"
@@ -864,10 +873,10 @@ TEST(repro_grammar_config_jsonnet) {
" url: 'http://' + host + ':' + port,\n"
"};\n"
"\n"
- "local addPort(base, offset) = base + offset;\n"
+ "local build(host) = makeServer(host, 8080);\n"
"\n"
"{\n"
- " server: makeServer('localhost', addPort(8080, 0)),\n"
+ " server: build,\n"
" debug: false,\n"
"}\n";
static const char bad[] = "local makeServer(host, port) = {";
@@ -898,6 +907,11 @@ TEST(repro_grammar_config_jsonnet) {
* Robustness should pass.
*/
TEST(repro_grammar_config_starlark) {
+ /* All calls must live INSIDE a function body for callable-sourcing (dim 7):
+ * go_binary is called inside make_binary's body, so its CALLS edge sources at
+ * the make_binary Function. The module-level statement only REFERENCES
+ * make_binary (a bare name assignment, not a call) so there is no
+ * Module-level call site. */
static const char src[] =
"def make_binary(name, srcs, deps = []):\n"
" \"\"\"Wrapper around go_binary for internal defaults.\"\"\"\n"
@@ -907,10 +921,7 @@ TEST(repro_grammar_config_starlark) {
" deps = deps + [\"//internal/cbm\"],\n"
" )\n"
"\n"
- "make_binary(\n"
- " name = \"cbm-server\",\n"
- " srcs = [\"main.go\"],\n"
- ")\n";
+ "default_rule = make_binary\n";
static const char bad[] = "def make_binary(name, srcs";
if (config_callable_battery("Starlark", src, CBM_LANG_STARLARK, "BUILD",
"Function", "go_binary") != 0)