From 89886f1554c219262f6b98a4cd8e79783c67ade4 Mon Sep 17 00:00:00 2001 From: Wassim Metallaoui Date: Sun, 1 Nov 2026 20:28:14 -0600 Subject: [PATCH 1/3] fix: stop a file spinning forever when its tests aren't discovered yet Running the nearest test before neotest had discovered a file's tests made neotest run the file itself. With no test items, build_spec returned nil, neotest found nothing to split the run into, and the file never got a result, so it stayed marked as running until the whole file was run. build_spec now discovers the file on the spot and runs the test at the cursor, or every test when the file isn't in the current window. It errors when the file has no tests, which neotest reports and clears. results also always reports the position that was run. --- lua/ruby-lsp/neotest/init.lua | 60 ++++++++++++++++++++-- tests/neotest_spec.lua | 94 +++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+), 4 deletions(-) diff --git a/lua/ruby-lsp/neotest/init.lua b/lua/ruby-lsp/neotest/init.lua index 62cc306..556901f 100644 --- a/lua/ruby-lsp/neotest/init.lua +++ b/lua/ruby-lsp/neotest/init.lua @@ -173,11 +173,10 @@ function NeotestAdapter.is_test_file(file_path) return name:match('_test%.rb$') ~= nil or name:match('_spec%.rb$') ~= nil or name:match('^test_.*%.rb$') ~= nil end ----Discover test positions via the ruby-lsp language server. ----Waits for the server to start and finish indexing before making requests. +---Ask Ruby LSP for a file's test items, after it starts and finishes indexing. ---@param file_path string ----@return neotest.Tree|nil -function NeotestAdapter.discover_positions(file_path) +---@return table[]|nil +local function discover_items(file_path) local file_name = nio.fn.fnamemodify(file_path, ':t') discovery_state.active_count = discovery_state.active_count + 1 @@ -226,6 +225,18 @@ function NeotestAdapter.discover_positions(file_path) if err or not result or type(result) ~= 'table' or #result == 0 then return nil end + return result +end + +---Discover test positions via the ruby-lsp language server. +---@param file_path string +---@return neotest.Tree|nil +function NeotestAdapter.discover_positions(file_path) + local result = discover_items(file_path) + if not result then + return nil + end + local file_name = nio.fn.fnamemodify(file_path, ':t') local last_line = 0 for _, item in ipairs(result) do @@ -256,6 +267,35 @@ function NeotestAdapter.discover_positions(file_path) end) end +---Collect the leaf test items under the given LSP items. +---@param items table[] +---@param out table[] +local function collect_leaf_items(items, out) + for _, item in ipairs(items) do + if item.children and #item.children > 0 then + collect_leaf_items(item.children, out) + else + table.insert(out, utils.wrap_test_item(item)) + end + end + return out +end + +---Discover a file's tests for a run that started before neotest had them. Picks +---the test at the cursor when the file is in the current window, else them all. +---@param file_path string +---@return table[] +local function undiscovered_file_items(file_path) + local discovered = discover_items(file_path) or {} + if nio.fn.expand('%:p') == file_path then + local nearest = utils.find_nearest_test(discovered, nio.fn.line('.') - 1) + if nearest then + return collect_leaf_items({ nearest }, {}) + end + end + return collect_leaf_items(discovered, {}) +end + ---Build a test command spec from a neotest position. ---Resolves the actual shell command via rubyLsp/resolveTestCommands. ---@param args neotest.RunArgs @@ -273,6 +313,15 @@ function NeotestAdapter.build_spec(args) collect_test_items(tree, items) end + -- neotest runs the file when the nearest test isn't discovered yet. Returning + -- nil there leaves the file marked as running, so discover it now instead. + if #items == 0 and position.type == 'file' then + items = undiscovered_file_items(position.path) + if #items == 0 then + error('no tests found in ' .. nio.fn.fnamemodify(position.path, ':t'), 0) + end + end + if #items == 0 then return nil end @@ -431,6 +480,9 @@ function NeotestAdapter.results(spec, result, tree) end end + -- neotest only stops showing a position as running once it has a result + results[pos_id] = results[pos_id] or { status = result.code == 0 and 'passed' or 'failed', output = result.output } + return results end diff --git a/tests/neotest_spec.lua b/tests/neotest_spec.lua index ff5d7ce..a04a8f2 100644 --- a/tests/neotest_spec.lua +++ b/tests/neotest_spec.lua @@ -168,9 +168,103 @@ describe('neotest adapter', function() "/^CalculatorTest#(''|test_add|test_average)$/", }, spec.command) end) + + -- neotest falls back to the file when its tests aren't discovered yet. Returning nil + -- there left the file marked as running forever. + describe('for a file whose tests are not discovered yet', function() + local file = '/path/test/foo_test.rb' + local resolved_items, original_fn + + local function range(first, last) + return { start = { line = first, character = 0 }, ['end'] = { line = last, character = 3 } } + end + + local function client_with(discovered) + return helpers.mock_client({ + id = 201, + request = function(_, method, params, handler) + if method == 'rubyLsp/discoverTests' then + handler(nil, discovered) + else + resolved_items = params.items + handler(nil, { commands = { 'bin/rails test ' .. file } }) + end + end, + }) + end + + local discovered = { + { + id = 'FooTest', + label = 'FooTest', + range = range(0, 9), + tags = { 'framework:rails' }, + children = { + { id = 'FooTest#test_one', label = 'test_one', range = range(1, 3), tags = { 'framework:rails' } }, + { id = 'FooTest#test_two', label = 'test_two', range = range(5, 7), tags = { 'framework:rails' } }, + }, + }, + } + + before_each(function() + resolved_items = nil + original_fn = + { expand = rawget(package.loaded['nio'].fn, 'expand'), line = rawget(package.loaded['nio'].fn, 'line') } + require('ruby-lsp.utils').on_indexing_complete(201) + end) + + after_each(function() + package.loaded['nio'].fn.expand = original_fn.expand + package.loaded['nio'].fn.line = original_fn.line + end) + + local function ids(items) + return vim.tbl_map(function(item) + return item.id + end, items or {}) + end + + it('discovers the file and runs the test at the cursor', function() + helpers.lsp_clients = { client_with(discovered) } + package.loaded['nio'].fn.expand = function() + return file + end + package.loaded['nio'].fn.line = function() + return 7 + end + + local spec = adapter.build_spec({ tree = make_node({ type = 'file', id = file, path = file }) }) + + assert.same({ 'FooTest#test_two' }, ids(resolved_items)) + assert.equals(file, spec.context.pos_id) + end) + + it('runs every test in the file when the cursor is elsewhere', function() + helpers.lsp_clients = { client_with(discovered) } + + adapter.build_spec({ tree = make_node({ type = 'file', id = file, path = file }) }) + + assert.same({ 'FooTest#test_one', 'FooTest#test_two' }, ids(resolved_items)) + end) + + it('errors when the file has no tests, so neotest stops showing it as running', function() + helpers.lsp_clients = { client_with({}) } + + assert.has_error(function() + adapter.build_spec({ tree = make_node({ type = 'file', id = file, path = file }) }) + end) + end) + end) end) describe('results', function() + it('reports the run position even when it has no discovered tests', function() + local tree = make_node({ type = 'file', id = '/path/test.rb' }) + local spec = { context = { pos_id = '/path/test.rb' } } + + assert.equals('passed', adapter.results(spec, { code = 0, output = '/dev/null' }, tree)['/path/test.rb'].status) + end) + it('marks all tests as passed when exit code is 0', function() local node1 = make_node({ type = 'test', From 55814d8724c601be492a527c9d7479cd8a6123b5 Mon Sep 17 00:00:00 2001 From: Wassim Metallaoui Date: Sun, 1 Nov 2026 20:36:47 -0600 Subject: [PATCH 2/3] refactor: tighten the undiscovered-file fallback and the results fallback collect_leaf_items returns a fresh list, the no-tests error lives with the discovery it checks, and results keeps one fallback for the run position instead of two copies of the failed result. The cursor fixture in the spec uses luassert stubs. --- lua/ruby-lsp/neotest/init.lua | 32 ++++++++++++++------------------ tests/neotest_spec.lua | 18 +++++++----------- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/lua/ruby-lsp/neotest/init.lua b/lua/ruby-lsp/neotest/init.lua index 556901f..fa8a083 100644 --- a/lua/ruby-lsp/neotest/init.lua +++ b/lua/ruby-lsp/neotest/init.lua @@ -269,31 +269,32 @@ end ---Collect the leaf test items under the given LSP items. ---@param items table[] ----@param out table[] -local function collect_leaf_items(items, out) +---@return table[] +local function collect_leaf_items(items) + local leaves = {} for _, item in ipairs(items) do if item.children and #item.children > 0 then - collect_leaf_items(item.children, out) + vim.list_extend(leaves, collect_leaf_items(item.children)) else - table.insert(out, utils.wrap_test_item(item)) + table.insert(leaves, utils.wrap_test_item(item)) end end - return out + return leaves end ---Discover a file's tests for a run that started before neotest had them. Picks ---the test at the cursor when the file is in the current window, else them all. +---Errors when the file has no tests, which neotest reports and stops the run. ---@param file_path string ---@return table[] local function undiscovered_file_items(file_path) local discovered = discover_items(file_path) or {} - if nio.fn.expand('%:p') == file_path then - local nearest = utils.find_nearest_test(discovered, nio.fn.line('.') - 1) - if nearest then - return collect_leaf_items({ nearest }, {}) - end + local nearest = nio.fn.expand('%:p') == file_path and utils.find_nearest_test(discovered, nio.fn.line('.') - 1) + local items = collect_leaf_items(nearest and { nearest } or discovered) + if #items == 0 then + error('no tests found in ' .. nio.fn.fnamemodify(file_path, ':t'), 0) end - return collect_leaf_items(discovered, {}) + return items end ---Build a test command spec from a neotest position. @@ -317,9 +318,6 @@ function NeotestAdapter.build_spec(args) -- nil there leaves the file marked as running, so discover it now instead. if #items == 0 and position.type == 'file' then items = undiscovered_file_items(position.path) - if #items == 0 then - error('no tests found in ' .. nio.fn.fnamemodify(position.path, ':t'), 0) - end end if #items == 0 then @@ -474,13 +472,11 @@ function NeotestAdapter.results(spec, result, tree) end if next(failed) then assign_test_results(tree, failed, framework, result.output, results) - else - -- Could not parse individual failures — fall back to marking parent - results[pos_id] = { status = 'failed', output = result.output } end end - -- neotest only stops showing a position as running once it has a result + -- Covers runs whose failures couldn't be parsed and files whose tests weren't + -- discovered. neotest only stops showing a position as running once it has a result. results[pos_id] = results[pos_id] or { status = result.code == 0 and 'passed' or 'failed', output = result.output } return results diff --git a/tests/neotest_spec.lua b/tests/neotest_spec.lua index a04a8f2..a1da7a6 100644 --- a/tests/neotest_spec.lua +++ b/tests/neotest_spec.lua @@ -173,7 +173,7 @@ describe('neotest adapter', function() -- there left the file marked as running forever. describe('for a file whose tests are not discovered yet', function() local file = '/path/test/foo_test.rb' - local resolved_items, original_fn + local resolved_items, stubs local function range(first, last) return { start = { line = first, character = 0 }, ['end'] = { line = last, character = 3 } } @@ -208,14 +208,14 @@ describe('neotest adapter', function() before_each(function() resolved_items = nil - original_fn = - { expand = rawget(package.loaded['nio'].fn, 'expand'), line = rawget(package.loaded['nio'].fn, 'line') } + stubs = {} require('ruby-lsp.utils').on_indexing_complete(201) end) after_each(function() - package.loaded['nio'].fn.expand = original_fn.expand - package.loaded['nio'].fn.line = original_fn.line + for _, s in ipairs(stubs) do + s:revert() + end end) local function ids(items) @@ -226,12 +226,8 @@ describe('neotest adapter', function() it('discovers the file and runs the test at the cursor', function() helpers.lsp_clients = { client_with(discovered) } - package.loaded['nio'].fn.expand = function() - return file - end - package.loaded['nio'].fn.line = function() - return 7 - end + local stub = require('luassert.stub') + stubs = { stub(package.loaded['nio'].fn, 'expand', file), stub(package.loaded['nio'].fn, 'line', 7) } local spec = adapter.build_spec({ tree = make_node({ type = 'file', id = file, path = file }) }) From 077627168bdf4426171b110f460c86ca1586b11d Mon Sep 17 00:00:00 2001 From: Wassim Metallaoui Date: Sun, 1 Nov 2026 20:40:57 -0600 Subject: [PATCH 3/3] fix: skip an undiscovered file cleanly instead of raising Raising from build_spec skipped neotest's runner cleanup, so the file stayed in its running table, and the message arrived wrapped in a stack trace. Now the fallback notifies with a short reason and returns a spec that exits at once, which results marks as skipped. A failed discovery says so instead of reporting no tests, and the current-buffer check compares resolved paths so symlinked projects still pick the test at the cursor. --- lua/ruby-lsp/neotest/init.lua | 48 +++++++++++++++++++++++++++-------- tests/neotest_spec.lua | 27 +++++++++++++++++--- 2 files changed, 60 insertions(+), 15 deletions(-) diff --git a/lua/ruby-lsp/neotest/init.lua b/lua/ruby-lsp/neotest/init.lua index fa8a083..02fdd52 100644 --- a/lua/ruby-lsp/neotest/init.lua +++ b/lua/ruby-lsp/neotest/init.lua @@ -175,7 +175,8 @@ end ---Ask Ruby LSP for a file's test items, after it starts and finishes indexing. ---@param file_path string ----@return table[]|nil +---@return table[]|nil items +---@return string|nil err local function discover_items(file_path) local file_name = nio.fn.fnamemodify(file_path, ':t') @@ -222,10 +223,10 @@ local function discover_items(file_path) discovery_state.warned = false end - if err or not result or type(result) ~= 'table' or #result == 0 then - return nil + if err then + return nil, type(err) == 'table' and err.message or tostring(err) end - return result + return type(result) == 'table' and result or {} end ---Discover test positions via the ruby-lsp language server. @@ -233,7 +234,7 @@ end ---@return neotest.Tree|nil function NeotestAdapter.discover_positions(file_path) local result = discover_items(file_path) - if not result then + if not result or #result == 0 then return nil end local file_name = nio.fn.fnamemodify(file_path, ':t') @@ -282,17 +283,27 @@ local function collect_leaf_items(items) return leaves end +---@param path string|nil +---@return string|nil +local function realpath(path) + return path and vim.uv.fs_realpath(path) or path +end + ---Discover a file's tests for a run that started before neotest had them. Picks ---the test at the cursor when the file is in the current window, else them all. ----Errors when the file has no tests, which neotest reports and stops the run. ---@param file_path string ----@return table[] +---@return table[] items +---@return string|nil reason why there is nothing to run local function undiscovered_file_items(file_path) - local discovered = discover_items(file_path) or {} - local nearest = nio.fn.expand('%:p') == file_path and utils.find_nearest_test(discovered, nio.fn.line('.') - 1) + local discovered, err = discover_items(file_path) + if not discovered then + return {}, 'could not discover tests: ' .. err + end + local in_window = realpath(nio.fn.expand('%:p')) == realpath(file_path) + local nearest = in_window and utils.find_nearest_test(discovered, nio.fn.line('.') - 1) local items = collect_leaf_items(nearest and { nearest } or discovered) if #items == 0 then - error('no tests found in ' .. nio.fn.fnamemodify(file_path, ':t'), 0) + return {}, 'no tests found in ' .. nio.fn.fnamemodify(file_path, ':t') end return items end @@ -317,7 +328,18 @@ function NeotestAdapter.build_spec(args) -- neotest runs the file when the nearest test isn't discovered yet. Returning -- nil there leaves the file marked as running, so discover it now instead. if #items == 0 and position.type == 'file' then - items = undiscovered_file_items(position.path) + local reason + items, reason = undiscovered_file_items(position.path) + if reason then + -- A spec that exits at once, so neotest finishes the run and the file + -- gets a skipped result. Raising here would leave neotest's runner + -- tracking the file as running and show a stack trace. + vim.notify('ruby-lsp: ' .. reason, vim.log.levels.WARN) + return { + command = { vim.v.progpath, '--clean', '--headless', '+qa!' }, + context = { pos_id = position.id, skipped = true }, + } + end end if #items == 0 then @@ -460,6 +482,10 @@ function NeotestAdapter.results(spec, result, tree) local pos_id = spec.context.pos_id local framework = spec.context.framework + if spec.context.skipped then + return { [pos_id] = { status = 'skipped' } } + end + if result.code == 0 then -- All tests passed — mark every leaf assign_test_results(tree, {}, framework, result.output, results) diff --git a/tests/neotest_spec.lua b/tests/neotest_spec.lua index a1da7a6..2baa50e 100644 --- a/tests/neotest_spec.lua +++ b/tests/neotest_spec.lua @@ -237,18 +237,37 @@ describe('neotest adapter', function() it('runs every test in the file when the cursor is elsewhere', function() helpers.lsp_clients = { client_with(discovered) } + local stub = require('luassert.stub') + stubs = { stub(package.loaded['nio'].fn, 'expand', '/path/test/other_test.rb') } adapter.build_spec({ tree = make_node({ type = 'file', id = file, path = file }) }) assert.same({ 'FooTest#test_one', 'FooTest#test_two' }, ids(resolved_items)) end) - it('errors when the file has no tests, so neotest stops showing it as running', function() + it('skips the file with a message when it has no tests', function() helpers.lsp_clients = { client_with({}) } - assert.has_error(function() - adapter.build_spec({ tree = make_node({ type = 'file', id = file, path = file }) }) - end) + local spec = adapter.build_spec({ tree = make_node({ type = 'file', id = file, path = file }) }) + + assert.is_nil(resolved_items) + assert.equals('skipped', adapter.results(spec, { code = 0 }, nil)[file].status) + assert.truthy(helpers.notifications[1].msg:find('no tests found in foo_test.rb', 1, true)) + end) + + it('says why when discovery fails', function() + helpers.lsp_clients = { + helpers.mock_client({ + id = 201, + request = function(_, _, _, handler) + handler({ message = 'boom' }, nil) + end, + }), + } + + adapter.build_spec({ tree = make_node({ type = 'file', id = file, path = file }) }) + + assert.truthy(helpers.notifications[1].msg:find('could not discover tests: boom', 1, true)) end) end) end)