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(elixir): Phoenix channel extraction was unreachable in both branches

Henry Hazanbd640caa918d
diff --git a/internal/cbm/extract_channels.c b/internal/cbm/extract_channels.c
index c12d4ebeb..17ef8f802 100644
--- a/internal/cbm/extract_channels.c
+++ b/internal/cbm/extract_channels.c
@@ -901,8 +901,42 @@ static void elixir_emit_second_arg(CBMExtractCtx *ctx, TSNode call, TSNode args,
}
}
-static void elixir_process_call(CBMExtractCtx *ctx, TSNode call) {
+/* tree-sitter-elixir gives a call's arguments node NO field name — its whole
+ * field set is key, left, operand, operator, quoted_start, quoted_end, right,
+ * target and value — so the generic field lookup is always null here and every
+ * channel below was silently skipped. extract_defs.c has carried the same
+ * positional fallback for the definition side since Elixir was added. */
+static TSNode elixir_channel_call_args(TSNode call) {
+ TSNode args = ts_node_child_by_field_name(call, TS_FIELD("arguments"));
+ if (ts_node_is_null(args) && ts_node_child_count(call) > 1) {
+ args = ts_node_child(call, 1);
+ }
+ return args;
+}
+
+/* The callee of an Elixir `call`. The grammar labels it `target`; child(0) is
+ * the same node and is what extract_defs.c reads, so accept either. */
+static TSNode elixir_channel_call_target(TSNode call) {
TSNode target = ts_node_child_by_field_name(call, TS_FIELD("target"));
+ if (ts_node_is_null(target) && ts_node_child_count(call) > 0) {
+ target = ts_node_child(call, 0);
+ }
+ return target;
+}
+
+/* Elixir def* macros. `def`/`defp` cover Phoenix channel callbacks; the macro
+ * forms are included because `handle_in` is generated by macros in some
+ * libraries. */
+static bool elixir_channel_is_def_macro(const char *macro) {
+ if (!macro) {
+ return false;
+ }
+ return strcmp(macro, "def") == 0 || strcmp(macro, "defp") == 0 ||
+ strcmp(macro, "defmacro") == 0 || strcmp(macro, "defmacrop") == 0;
+}
+
+static void elixir_process_call(CBMExtractCtx *ctx, TSNode call) {
+ TSNode target = elixir_channel_call_target(call);
if (ts_node_is_null(target)) {
return;
}
@@ -911,7 +945,7 @@ static void elixir_process_call(CBMExtractCtx *ctx, TSNode call) {
return;
}
- TSNode args = ts_node_child_by_field_name(call, TS_FIELD("arguments"));
+ TSNode args = elixir_channel_call_args(call);
if (strstr(target_text, "PubSub.broadcast") || strstr(target_text, "PubSub.local_broadcast")) {
elixir_emit_second_arg(ctx, call, args, "phoenix_pubsub", CBM_CHANNEL_EMIT);
@@ -927,10 +961,31 @@ static void elixir_process_call(CBMExtractCtx *ctx, TSNode call) {
}
}
-/* Detect handle_in("event", payload, socket) — Phoenix Channel listener */
-static void elixir_process_function_def(CBMExtractCtx *ctx, TSNode func_def) {
- TSNode name_node = ts_node_child_by_field_name(func_def, TS_FIELD("name"));
- if (ts_node_is_null(name_node)) {
+/* Detect handle_in("event", payload, socket) — Phoenix Channel listener.
+ * `def_call` is the `call` node of a def* macro. Elixir has no `def` node type
+ * and no `name`/`parameters` fields: a definition is a call whose first
+ * argument is the head, itself a call of name(params) — wrapped in a `when`
+ * binary_operator when the clause carries a guard. */
+static void elixir_process_function_def(CBMExtractCtx *ctx, TSNode def_call) {
+ TSNode args = elixir_channel_call_args(def_call);
+ if (ts_node_is_null(args) || ts_node_named_child_count(args) == 0) {
+ return;
+ }
+ TSNode head = ts_node_named_child(args, 0);
+ if (strcmp(ts_node_type(head), "binary_operator") == 0) {
+ TSNode op = ts_node_child_by_field_name(head, TS_FIELD("operator"));
+ TSNode left = ts_node_child_by_field_name(head, TS_FIELD("left"));
+ if (!ts_node_is_null(op) && !ts_node_is_null(left) &&
+ ts_node_end_byte(op) - ts_node_start_byte(op) == 4 &&
+ strncmp(ctx->source + ts_node_start_byte(op), "when", 4) == 0) {
+ head = left;
+ }
+ }
+ if (strcmp(ts_node_type(head), "call") != 0 || ts_node_child_count(head) == 0) {
+ return;
+ }
+ TSNode name_node = ts_node_child(head, 0);
+ if (ts_node_is_null(name_node) || strcmp(ts_node_type(name_node), "identifier") != 0) {
return;
}
char *name = cbm_node_text(ctx->arena, name_node, ctx->source);
@@ -938,7 +993,7 @@ static void elixir_process_function_def(CBMExtractCtx *ctx, TSNode func_def) {
return;
}
/* First parameter is the event name pattern */
- TSNode params = ts_node_child_by_field_name(func_def, TS_FIELD("parameters"));
+ TSNode params = elixir_channel_call_args(head);
if (ts_node_is_null(params)) {
return;
}
@@ -952,7 +1007,7 @@ static void elixir_process_function_def(CBMExtractCtx *ctx, TSNode func_def) {
val = literal_from_first_child(ctx, first_param);
}
if (val) {
- push_channel(ctx, val, "phoenix_channel", CBM_CHANNEL_LISTEN, func_def);
+ push_channel(ctx, val, "phoenix_channel", CBM_CHANNEL_LISTEN, def_call);
}
}
@@ -964,10 +1019,18 @@ static void extract_channels_elixir(CBMExtractCtx *ctx) {
while (stack.count > 0) {
TSNode node = ts_nstack_pop(&stack);
const char *kind = ts_node_type(node);
+ /* Everything in Elixir is a `call`, including `def`. The old dispatch
+ * tested for a "def" node type the grammar does not have, so the
+ * handle_in listener branch was unreachable. */
if (strcmp(kind, "call") == 0) {
- elixir_process_call(ctx, node);
- } else if (strcmp(kind, "def") == 0) {
- elixir_process_function_def(ctx, node);
+ TSNode target = elixir_channel_call_target(node);
+ char *macro =
+ ts_node_is_null(target) ? NULL : cbm_node_text(ctx->arena, target, ctx->source);
+ if (elixir_channel_is_def_macro(macro)) {
+ elixir_process_function_def(ctx, node);
+ } else {
+ elixir_process_call(ctx, node);
+ }
}
uint32_t count = ts_node_child_count(node);
for (int i = (int)count - SKIP_ONE; i >= 0; i--) {
diff --git a/tests/test_extraction.c b/tests/test_extraction.c
index 447e7c104..171f1f08f 100644
--- a/tests/test_extraction.c
+++ b/tests/test_extraction.c
@@ -1005,6 +1005,54 @@ TEST(elixir_function) {
PASS();
}
+/* True when a channel with this name/transport/direction was extracted. */
+static int has_channel(CBMFileResult *r, const char *name, const char *transport,
+ CBMChannelDirection dir) {
+ for (int i = 0; i < r->channels.count; i++) {
+ const CBMChannel *c = &r->channels.items[i];
+ if (c->channel_name && strcmp(c->channel_name, name) == 0 && c->transport &&
+ strcmp(c->transport, transport) == 0 && c->direction == dir)
+ return 1;
+ }
+ return 0;
+}
+
+/* Phoenix pub/sub and channel listeners. Both branches were unreachable: the
+ * emit side looked up an "arguments" FIELD that tree-sitter-elixir does not
+ * define, and the listener side dispatched on a "def" NODE TYPE that does not
+ * exist (Elixir defs are `call` nodes), so a Phoenix app produced no channels
+ * at all. */
+TEST(elixir_channels_pubsub_and_handle_in) {
+ CBMFileResult *r =
+ extract("defmodule Acme.Notifier do\n"
+ " def announce(p) do\n"
+ " Phoenix.PubSub.broadcast(Acme.PubSub, \"user:created\", p)\n"
+ " end\n"
+ " def watch, do: Phoenix.PubSub.subscribe(Acme.PubSub, \"user:updated\")\n"
+ "end\n"
+ "defmodule AcmeWeb.RoomChannel do\n"
+ " def handle_in(\"new_msg\", payload, socket) do\n"
+ " push(socket, \"pong\", %{})\n"
+ " {:noreply, socket}\n"
+ " end\n"
+ " def handle_in(\"ping\", _p, socket) when is_map(socket) do\n"
+ " {:noreply, socket}\n"
+ " end\n"
+ "end\n",
+ CBM_LANG_ELIXIR, "t", "chan.ex");
+ ASSERT_NOT_NULL(r);
+ ASSERT_FALSE(r->has_error);
+ ASSERT(has_channel(r, "user:created", "phoenix_pubsub", CBM_CHANNEL_EMIT));
+ ASSERT(has_channel(r, "user:updated", "phoenix_pubsub", CBM_CHANNEL_LISTEN));
+ /* handle_in clauses are listeners — including the guarded one. */
+ ASSERT(has_channel(r, "new_msg", "phoenix_channel", CBM_CHANNEL_LISTEN));
+ ASSERT(has_channel(r, "ping", "phoenix_channel", CBM_CHANNEL_LISTEN));
+ /* A bare push/broadcast inside the channel module is an emit. */
+ ASSERT(has_channel(r, "pong", "phoenix_channel", CBM_CHANNEL_EMIT));
+ cbm_free_result(r);
+ PASS();
+}
+
/* --- Haskell --- */
TEST(haskell_function) {
CBMFileResult *r = extract("add :: Int -> Int -> Int\nadd x y = x + y\n\nmultiply :: Int -> "
@@ -5432,6 +5480,7 @@ SUITE(extraction) {
/* Functional */
RUN_TEST(elixir_function);
+ RUN_TEST(elixir_channels_pubsub_and_handle_in);
RUN_TEST(haskell_function);
RUN_TEST(ocaml_function);
RUN_TEST(erlang_function);