Skip to content

Commit 0c25a96

Browse files
committed
fix(ruby-lsp): keep the ActiveRecord finder rule same-file for now
Turn ruby_ar_finder_cross_file off by default, as agreed in review for the case where the cross-file finder stratum misses the 95% precision bar. A fixed-seed sample of 48 edges that only the cross-file half produces, on rails, redmine, mastodon and loomio, was 38/48 correct. Nine of the ten misses are relation chains (Model.where(...).m) typed as a model instance, so m lands on an instance method; in Ruby a relation answers m itself or delegates it to the model's class methods. The tenth is a same-named test model from another gem. The test seam now returns the previous setting, so a test restores what it found instead of a fixed value; with a fixed reset the default-mode row could not fail. The two rows that need the cross-file half switch it on explicitly, the default row asserts same-file only, and the UsersController row matches its caller exactly. Signed-off-by: Martin Vogel <[email protected]>
1 parent 18898aa commit 0c25a96

3 files changed

Lines changed: 37 additions & 19 deletions

File tree

‎internal/cbm/lsp/ruby_lsp.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,13 +829,19 @@ const CBMRegisteredFunc *ruby_lookup_singleton_method(RubyLSPContext *ctx, const
829829

830830
/* Whether the finder rule (`User.find` -> User, relation chains keep the
831831
* model) also fires for a model declared in ANOTHER file, which is the usual
832-
* Rails layout. Set to false to keep the rule to models declared in the file
833-
* being resolved; nothing else changes. */
834-
static bool ruby_ar_finder_cross_file = true;
832+
* Rails layout. Off for now, so the rule keeps to models declared in the file
833+
* being resolved and nothing else changes. Across files it mostly meets
834+
* relation chains, and `Model.where(...).m` reaches the relation or the
835+
* model's class methods in Ruby, never an instance method `m`; a judged sample
836+
* of the cross-file edges came in below the 95% precision bar. Turn it on once
837+
* relations are typed apart from records. */
838+
static bool ruby_ar_finder_cross_file = false;
835839

836840
#ifdef CBM_ENABLE_TEST_SEAMS
837-
void cbm_ruby_set_ar_finder_cross_file(bool on) {
841+
bool cbm_ruby_set_ar_finder_cross_file(bool on) {
842+
bool prev = ruby_ar_finder_cross_file;
838843
ruby_ar_finder_cross_file = on;
844+
return prev;
839845
}
840846
#endif
841847

‎internal/cbm/lsp/ruby_lsp.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,9 @@ typedef struct CBMRubyCrossRegistry {
225225

226226
#ifdef CBM_ENABLE_TEST_SEAMS
227227
/* Test seam: turn the cross-file half of the ActiveRecord finder rule on or
228-
* off (see ruby_ar_finder_cross_file in ruby_lsp.c). */
229-
void cbm_ruby_set_ar_finder_cross_file(bool on);
228+
* off (see ruby_ar_finder_cross_file in ruby_lsp.c). Returns the previous
229+
* setting, so a test restores what it found rather than a fixed value. */
230+
bool cbm_ruby_set_ar_finder_cross_file(bool on);
230231
#endif
231232

232233
/* Build the shared registry from every project def. Strings are borrowed from

‎tests/test_ruby_lsp.c‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,16 +1570,20 @@ TEST(rubylsp_cross_file_defs) {
15701570
cbm_arena_init(&arena);
15711571
CBMResolvedCallArray out;
15721572
memset(&out, 0, sizeof(out));
1573+
/* `f = User.find(3)` with User in another file needs the cross-file half
1574+
* of the finder rule, which is off by default (ruby_ar_finder_cross_file). */
1575+
bool prev = cbm_ruby_set_ar_finder_cross_file(true);
15731576
cbm_run_ruby_lsp_cross(&arena, src, (int)strlen(src), "test.app.controller", defs, 3, NULL,
15741577
NULL, 0, NULL, &out);
1578+
(void)cbm_ruby_set_ar_finder_cross_file(prev);
15751579

15761580
bool ctor = false;
15771581
bool full_name = false;
15781582
bool admin = false;
15791583
for (int i = 0; i < out.count; i++) {
15801584
if (!out.items[i].callee_qn || !out.items[i].caller_qn)
15811585
continue;
1582-
if (!strstr(out.items[i].caller_qn, "UsersController.show"))
1586+
if (strcmp(out.items[i].caller_qn, "test.app.controller.UsersController.show") != 0)
15831587
continue;
15841588
if (strcmp(out.items[i].callee_qn, "test.models.user.User") == 0)
15851589
ctor = true;
@@ -1657,8 +1661,11 @@ TEST(rubylsp_cross_file_ar_repro) {
16571661
cbm_arena_init(&arena);
16581662
CBMResolvedCallArray out;
16591663
memset(&out, 0, sizeof(out));
1664+
/* `user = User.find(id)` with User in another file: cross-file finder rule. */
1665+
bool prev = cbm_ruby_set_ar_finder_cross_file(true);
16601666
cbm_run_ruby_lsp_cross(&arena, src, (int)strlen(src), "e2e.app.services.report", defs, 6, NULL,
16611667
NULL, 0, NULL, &out);
1668+
(void)cbm_ruby_set_ar_finder_cross_file(prev);
16621669

16631670
bool full_name = false;
16641671
bool deact = false;
@@ -2147,8 +2154,9 @@ TEST(rubylsp_cross_unseen_mixin_blocks_superclass) {
21472154

21482155
/* The finder rule (`User.find` -> User, relation chains keep the model) for a
21492156
* model declared in ANOTHER file, the usual Rails layout. It is one switch
2150-
* (ruby_ar_finder_cross_file): on by default; off keeps the rule to models
2151-
* declared in the file being resolved, with nothing else changed. */
2157+
* (ruby_ar_finder_cross_file): off by default, which keeps the rule to models
2158+
* declared in the file being resolved, with nothing else changed; on, it also
2159+
* types models from other files. */
21522160
static bool rubylsp_ar_finder_cross_edge(void) {
21532161
CBMLSPDef defs[2];
21542162
cross_def(&defs[0], "test.models.user.User", "User", "Class", "test.models.user", NULL,
@@ -2168,13 +2176,9 @@ static bool rubylsp_ar_finder_cross_edge(void) {
21682176
return edge;
21692177
}
21702178

2171-
TEST(rubylsp_cross_ar_finder_crosses_files_by_default) {
2172-
ASSERT(rubylsp_ar_finder_cross_edge());
2173-
PASS();
2174-
}
2175-
2176-
TEST(rubylsp_cross_ar_finder_same_file_only_when_switched_off) {
2177-
cbm_ruby_set_ar_finder_cross_file(false);
2179+
TEST(rubylsp_cross_ar_finder_same_file_only_by_default) {
2180+
/* Default: a model from another file gets no finder typing, while a model
2181+
* declared in the file being resolved still does. */
21782182
bool cross = rubylsp_ar_finder_cross_edge();
21792183
const char *src = "class User < ApplicationRecord\n def full_name\n 'x'\n end\nend\n"
21802184
"class UsersController < ApplicationController\n def show\n"
@@ -2183,12 +2187,19 @@ TEST(rubylsp_cross_ar_finder_same_file_only_when_switched_off) {
21832187
bool same = r && find_resolved(r, "UsersController.show", "User.full_name") >= 0;
21842188
if (r)
21852189
cbm_free_result(r);
2186-
cbm_ruby_set_ar_finder_cross_file(true);
21872190
ASSERT(!cross);
21882191
ASSERT(same);
21892192
PASS();
21902193
}
21912194

2195+
TEST(rubylsp_cross_ar_finder_crosses_files_when_switched_on) {
2196+
bool prev = cbm_ruby_set_ar_finder_cross_file(true);
2197+
bool cross = rubylsp_ar_finder_cross_edge();
2198+
(void)cbm_ruby_set_ar_finder_cross_file(prev);
2199+
ASSERT(cross);
2200+
PASS();
2201+
}
2202+
21922203
/* ── suite ──────────────────────────────────────────────────────── */
21932204

21942205
void suite_ruby_lsp(void) {
@@ -2240,6 +2251,6 @@ void suite_ruby_lsp(void) {
22402251
RUN_TEST(rubylsp_cross_mixin_shadows_superclass_across_files);
22412252
RUN_TEST(rubylsp_cross_mixin_in_reopening_shadows_superclass);
22422253
RUN_TEST(rubylsp_cross_unseen_mixin_blocks_superclass);
2243-
RUN_TEST(rubylsp_cross_ar_finder_crosses_files_by_default);
2244-
RUN_TEST(rubylsp_cross_ar_finder_same_file_only_when_switched_off);
2254+
RUN_TEST(rubylsp_cross_ar_finder_same_file_only_by_default);
2255+
RUN_TEST(rubylsp_cross_ar_finder_crosses_files_when_switched_on);
22452256
}

0 commit comments

Comments
 (0)