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(ruby-lsp): do not register block-nested defs as class members

Ben Fairless8bc99a40587b
diff --git a/internal/cbm/lsp/ruby_lsp.c b/internal/cbm/lsp/ruby_lsp.c
index 1411c3649..c0063212f 100644
--- a/internal/cbm/lsp/ruby_lsp.c
+++ b/internal/cbm/lsp/ruby_lsp.c
@@ -161,6 +161,7 @@ typedef struct {
const char *saved_class;
bool saved_singleton;
bool saved_self_unknown;
+ int saved_block_depth;
} RubyClassFrame;
/* Enter a class/module node: compute names, push nesting + enclosing class.
@@ -178,6 +179,7 @@ static bool ruby_class_enter(RubyLSPContext *ctx, TSNode node, RubyClassFrame *f
fr->saved_class = ctx->enclosing_class_qn;
fr->saved_singleton = ctx->in_singleton_method;
fr->saved_self_unknown = ctx->self_unknown;
+ fr->saved_block_depth = ctx->block_depth;
fr->path = ruby_path_join(ctx->arena, ruby_dotted(ctx->arena, fr->saved_nesting), cpath);
if (fr->saved_class)
@@ -191,6 +193,7 @@ static bool ruby_class_enter(RubyLSPContext *ctx, TSNode node, RubyClassFrame *f
ctx->enclosing_class_qn = fr->qn;
ctx->in_singleton_method = false;
ctx->self_unknown = false; /* a class body names its self again */
+ ctx->block_depth = 0;
return true;
}
@@ -199,6 +202,7 @@ static void ruby_class_leave(RubyLSPContext *ctx, const RubyClassFrame *fr) {
ctx->enclosing_class_qn = fr->saved_class;
ctx->in_singleton_method = fr->saved_singleton;
ctx->self_unknown = fr->saved_self_unknown;
+ ctx->block_depth = fr->saved_block_depth;
}
/* True when a QN belongs to the indexed project (module_qn's first segment
@@ -1192,16 +1196,29 @@ static void ruby_resolve_calls_in_node_inner(RubyLSPContext *ctx, TSNode node) {
ctx->in_singleton_method = saved;
return;
}
- if (strcmp(k, "method") == 0) {
- ruby_process_method(ctx, node, ctx->in_singleton_method);
- return;
- }
- if (strcmp(k, "singleton_method") == 0) {
- ruby_process_method(ctx, node, true);
+ if (strcmp(k, "method") == 0 || strcmp(k, "singleton_method") == 0) {
+ bool singleton = ctx->in_singleton_method || strcmp(k, "singleton_method") == 0;
+ if (ctx->block_depth > 0) {
+ /* A def inside a block. Inside a class body the extractor emits
+ * no def for it, so no row could ever join: skip it. At top
+ * level the extractor emits a plain Function, so typed-receiver
+ * calls in its body may resolve — but its `self` is whatever
+ * the block's DSL made it, so bare and self. calls emit nothing. */
+ if (ctx->enclosing_class_qn)
+ return;
+ bool saved = ctx->self_unknown;
+ ctx->self_unknown = true;
+ ruby_process_method(ctx, node, singleton);
+ ctx->self_unknown = saved;
+ return;
+ }
+ ruby_process_method(ctx, node, singleton);
return;
}
if (strcmp(k, "block") == 0 || strcmp(k, "do_block") == 0 || strcmp(k, "lambda") == 0) {
+ ctx->block_depth++;
ruby_process_block(ctx, node);
+ ctx->block_depth--;
return;
}
@@ -1418,6 +1435,11 @@ static void ruby_pass1_scan_inner(RubyLSPContext *ctx, TSNode node) {
return;
}
if (strcmp(k, "method") == 0 || strcmp(k, "singleton_method") == 0) {
+ /* A def inside a block (`class_methods do … end`, `included do …
+ * end`, a describe group) is not a method of the enclosing
+ * class/module — and not a file-level function either. */
+ if (ctx->block_depth > 0)
+ return;
bool singleton = ctx->in_singleton_method || strcmp(k, "singleton_method") == 0;
TSNode name = ts_node_child_by_field_name(node, "name", 4);
char *mname = ts_node_is_null(name) ? NULL : ruby_node_text(ctx, name);
@@ -1425,7 +1447,9 @@ static void ruby_pass1_scan_inner(RubyLSPContext *ctx, TSNode node) {
ruby_register_method(ctx, ctx->build_reg, ctx->enclosing_class_qn, mname, singleton);
return; /* method bodies are PASS-2 territory */
}
- if (strcmp(k, "call") == 0) {
+ if (strcmp(k, "call") == 0 && ctx->block_depth == 0) {
+ /* `include X` inside a block (`included do include X end`,
+ * `Foo.class_eval { include X }`) applies to some other module. */
TSNode meth = ts_node_child_by_field_name(node, "method", 6);
if (!ts_node_is_null(meth)) {
char *mn = ruby_node_text(ctx, meth);
@@ -1437,12 +1461,18 @@ static void ruby_pass1_scan_inner(RubyLSPContext *ctx, TSNode node) {
}
}
+ bool is_block =
+ strcmp(k, "block") == 0 || strcmp(k, "do_block") == 0 || strcmp(k, "lambda") == 0;
+ if (is_block)
+ ctx->block_depth++;
uint32_t nc = ts_node_child_count(node);
for (uint32_t i = 0; i < nc; i++) {
TSNode c = ts_node_child(node, i);
if (!ts_node_is_null(c))
ruby_pass1_scan(ctx, c);
}
+ if (is_block)
+ ctx->block_depth--;
}
/* Finalize PASS 1: resolve superclass + mixin references now that the class
diff --git a/internal/cbm/lsp/ruby_lsp.h b/internal/cbm/lsp/ruby_lsp.h
index dc9271d5b..ad2d81de3 100644
--- a/internal/cbm/lsp/ruby_lsp.h
+++ b/internal/cbm/lsp/ruby_lsp.h
@@ -114,6 +114,12 @@ typedef struct {
* receiver of bare and `self.` calls is NOT the enclosing class, and
* the resolver cannot know what it is — so they emit nothing. */
bool self_unknown;
+ /* Number of enclosing blocks (`{ }`, `do end`, `-> { }`) since the
+ * nearest class/module body. A `def` met here is NOT a method of the
+ * enclosing class — ActiveSupport's `class_methods do … end`,
+ * `included do … end`, RSpec groups — so PASS 1 registers nothing for
+ * it and collects no mixins from it. */
+ int block_depth;
const char *module_qn;
/* Output: resolved calls accumulate here. */
diff --git a/tests/test_ruby_lsp.c b/tests/test_ruby_lsp.c
index 1ad45e314..63f764f34 100644
--- a/tests/test_ruby_lsp.c
+++ b/tests/test_ruby_lsp.c
@@ -498,6 +498,85 @@ TEST(rubylsp_resolved_ancestry_still_resolves) {
PASS();
}
+/* ── 7d. Methods defined inside blocks are not members of the module ── */
+
+/* ActiveSupport's `class_methods do … end` / `included do … end` define
+ * methods on whatever class includes the concern (class-side, for
+ * class_methods), never instance methods of the concern module itself. A
+ * resolver that registered them on the module would then find them through
+ * `include Concern` and emit `Foo.new.build` -> Concern.build — an edge to a
+ * def the extractor does not even emit. Same for `include X` written INSIDE
+ * such a block: it applies to the includer, not the concern. */
+TEST(rubylsp_block_defs_not_module_members) {
+ const char *src = "module Other\n"
+ " def other_m\n"
+ " 1\n"
+ " end\n"
+ "end\n"
+ "module Concern\n"
+ " class_methods do\n"
+ " def build\n"
+ " 1\n"
+ " end\n"
+ " end\n"
+ " included do\n"
+ " include Other\n"
+ " def injected\n"
+ " 2\n"
+ " end\n"
+ " end\n"
+ " def real\n"
+ " 3\n"
+ " end\n"
+ "end\n"
+ "class Foo\n"
+ " include Concern\n"
+ " def initialize\n"
+ " end\n"
+ "end\n"
+ "class User\n"
+ " def run\n"
+ " f = Foo.new\n"
+ " f.build\n"
+ " f.injected\n"
+ " f.other_m\n"
+ " f.real\n"
+ " end\n"
+ "end\n";
+ CBMFileResult *r = extract_ruby(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "User.run", "build") < 0);
+ ASSERT(find_resolved(r, "User.run", "injected") < 0);
+ ASSERT(find_resolved(r, "User.run", "other_m") < 0);
+ /* Positive control: a def directly in the module body IS a member. */
+ ASSERT(require_resolved_exact(r, "test.main.User.run", "test.main.Concern.real") >= 0);
+ cbm_free_result(r);
+ PASS();
+}
+
+/* A def inside a top-level block (an RSpec group) has whatever `self` the
+ * DSL gave it: bare calls in its body emit nothing, and it is not a
+ * file-level function other top-level code can reach. */
+TEST(rubylsp_top_level_block_defs_fail_closed) {
+ const char *src = "def util\n"
+ " 1\n"
+ "end\n"
+ "describe 'x' do\n"
+ " def helper\n"
+ " util()\n"
+ " end\n"
+ "end\n"
+ "def caller_fn\n"
+ " helper()\n"
+ "end\n";
+ CBMFileResult *r = extract_ruby(src);
+ ASSERT(r);
+ ASSERT(find_resolved(r, "helper", "util") < 0);
+ ASSERT(find_resolved(r, "caller_fn", "helper") < 0);
+ cbm_free_result(r);
+ PASS();
+}
+
/* ── 8. extend mixin (class-side) ───────────────────────────────── */
TEST(rubylsp_extend_mixin) {
@@ -1240,6 +1319,8 @@ void suite_ruby_lsp(void) {
RUN_TEST(rubylsp_prepend_mixin);
RUN_TEST(rubylsp_unresolved_ancestry_fails_closed);
RUN_TEST(rubylsp_resolved_ancestry_still_resolves);
+ RUN_TEST(rubylsp_block_defs_not_module_members);
+ RUN_TEST(rubylsp_top_level_block_defs_fail_closed);
RUN_TEST(rubylsp_extend_mixin);
RUN_TEST(rubylsp_super_dispatch);
RUN_TEST(rubylsp_ivar_typing);