Skip to content

Conversation

@zyaga
Copy link

@zyaga zyaga commented Oct 20, 2025

DISCLAIMER: Claude AI helped put together this PR.

Original Problem

When you opened a test file that belonged to a different project/directory than your current working directory (cwd), neotest would fail to find an adapter for that file. This happened because:

  • On startup, neotest only registered adapters for projects in the current working directory
  • The _get_adapter method would search through already-registered adapters
  • If no adapter was found, it would just return nil - it never attempted to discover new adapters

Real-World Scenario

Imagine you have:

  • /home/user/project-a/ (your cwd when neotest starts)
  • /home/user/project-b/test_file.py (a file you open later)

When you opened project-b/test_file.py, neotest would fail to recognize it as a test file because it never registered an adapter for project-b. The adapter discovery only happened:

  • At startup for your cwd
  • When you changed directories (DirChanged event)

The Fix

We modified _get_adapter to implement lazy adapter discovery:

  • First attempt: Search existing registered adapters (original behavior)
  • If not found: Call _update_adapters(dir) for the file's parent directory
  • Second attempt: Search again with newly registered adapters
-- First attempt to find adapter
local found_id, found_adapter = find_adapter()
if found_id then
  return found_id, found_adapter
end

-- If no adapter is found and client is started, update adapters and try again
if self._started then
  local dir = lib.files.is_dir(position_id) and position_id or lib.files.parent(position_id)
  self:_update_adapters(dir)  -- Discover and register adapters for this directory
  return find_adapter()        -- Try again
end

Benefits

On-demand adapter discovery: Adapters are now discovered when needed, not just at startup
✅ Multi-project support: You can work with test files from multiple projects without restarting
✅ Reuses existing infrastructure: Leverages the existing _update_adapters method instead of duplicating logic
✅ Minimal performance impact: Only triggers when an adapter isn't found
✅ Safe: Only runs after client has started to avoid initialization issues

What This Enables

Users can now:

  • Open test files from any project directory
  • Work across multiple project roots simultaneously
  • Have neotest automatically discover and register the appropriate adapter for each file
  • Not worry about their current working directory when opening test files

Fixes #353

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] adapter for directory takes precedence over adapter for file, incorrectly resulting in "No tests found"

2 participants