nvim-neotest / neotest-go

MIT License
124 stars 43 forks source link

No tests found #85

Closed cehoffman closed 2 months ago

cehoffman commented 3 months ago

I've read the other closed issues #64 and #51 on this. I suspect this is a similar cause with treesitter changes, but I haven't been able to figure out a resolution.

This is the minimal reproduction configuration I have using lazy.nvim.

repro.lua

-- DO NOT change the paths and don't remove the colorscheme
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
    vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({ "git", "clone", "--filter=blob:none", "https://github.com/folke/lazy.nvim.git", lazypath })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
    "folke/tokyonight.nvim",
    "LazyVim/LazyVim",
    {
        "nvim-neotest/neotest",
        dependencies = {
            "nvim-neotest/nvim-nio",
            "nvim-lua/plenary.nvim",
            "nvim-treesitter/nvim-treesitter",
            "nvim-neotest/neotest-go",
        },
        opts = {
            adapters = {
                ["neotest-go"] = {
                    -- Here we can set options for neotest-go, e.g.
                    -- args = { "-tags=integration" }
                    recursive_run = true,
                },
            },
        },
    },
}
require("lazy").setup(plugins, {
    root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

vim.cmd([[
command! NeotestSummary lua require("neotest").summary.toggle()
command! NeotestFile lua require("neotest").run.run(vim.fn.expand("%"))
command! Neotest lua require("neotest").run.run(vim.fn.getcwd())
command! NeotestNearest lua require("neotest").run.run()
command! NeotestDebug lua require("neotest").run.run({ strategy = "dap" })
command! NeotestAttach lua require("neotest").run.attach()
command! NeotestOutput lua require("neotest").output.open()
]])

Loading this test file, I get to tests found for NeotestFile and NeotestSummary.

repro_test.go

package main

import (
    "regexp"
    "testing"
)

func Hello(msg string) (string, error) {
    return msg, nil
}

func TestFail(t *testing.T) {
    t.Error("Fail :(")
}

// TestHelloName calls greetings.Hello with a name, checking
// for a valid return value.
func TestHelloName(t *testing.T) {
    name := "Gladys"
    want := regexp.MustCompile(`\b` + name + `\b`)
    msg, err := Hello("Gladys")
    if !want.MatchString(msg) || err != nil {
        t.Fatalf(`Hello("Gladys") = %q, %v, want match for %#q, nil`, msg, err, want)
    }
}

// TestHelloEmpty calls greetings.Hello with an empty string,
// checking for an error.
func TestHelloEmpty(t *testing.T) {
    msg, err := Hello("")
    if msg != "" || err == nil {
        t.Fatalf(`Hello("") = %q, %v, want "", error`, msg, err)
    }
}
$ nvim --version
NVIM v0.9.5
Build type: Release
LuaJIT 2.1.1710088188

   system vimrc file: "$VIM/sysinit.vim"
  fall-back for $VIM: "
/Users/christopherhoffman/.homebrew/Cellar/neovim/0.9.5/share/nvim"

Run :checkhealth for more info

$ nvim -u repro.lua repro_test.go

The lockfile for lazy.

{
  "LazyVim": { "branch": "main", "commit": "70bc8803306ba26a6362d489751da48633c1164c" },
  "lazy.nvim": { "branch": "main", "commit": "83493db50a434a4c5c648faf41e2ead80f96e478" },
  "neotest": { "branch": "master", "commit": "e07fe8241112274aae9947b98d255763738a1d52" },
  "neotest-go": { "branch": "main", "commit": "6a2f996d89fe4631942e035b1c114544ee045043" },
  "nvim-nio": { "branch": "master", "commit": "ed70af8ad9d4dafdb55539ed2b4454aac2a2a0c3" },
  "nvim-treesitter": { "branch": "master", "commit": "c28396de30b92a5af049037c2bd543a932a37a78" },
  "plenary.nvim": { "branch": "master", "commit": "f7adfc4b3f4f91aab6caebf42b3682945fbc35be" },
  "tokyonight.nvim": { "branch": "main", "commit": "fbe3a27378fdd51a8ddd04f57012455436916a62" }
}
asfaltboy commented 3 months ago

I, too, had experienced the annoying 'No tests found' :anger:. And on top of that, it took me two days to just build the mental model of how the plugin, treesitter, etc work together to identify tests, and I was able to find the root cause for my issue.

In the end, for me, it was another adapter being used instead of go (neotest-zig), which meant it didn't find the test case name, but as you are showing above you're not registering the neotest-zig adapter.

What I really found useful is inspecting the debug logs that neotest emits. I will share what I did in case it helps you too:

  1. Change the neotest plugin opts to include the log level:
    "nvim-neotest/neotest",
    -- other key/vals ...
    opts = {
     level = vim.log.levels.DEBUG,
     adapters = {
       ["neotest-go"] = {
         -- Here we can set options for neotest-go, e.g.
         -- args = { "-tags=integration" }
         recursive_run = true,
       },
     },
    },
  2. Find out where neotest writes its plugins (source). I ran the command:
    :lua vim.print(pcall(vim.fn.stdpath, "log"))
    -- my output: /home/asfaltboy/.local/state/nvim/neotest.log
  3. restart neovim, and tail the log in another shell:
    tail -f /home/asfaltboy/.local/state/nvim/neotest.log
  4. In your neovim session, try and run neotest's run command.

If you can't identify the cause outright, please share the logs

cehoffman commented 2 months ago

I followed the directions to set the log level in the reproduction script and invoked a test run. I didn't get much of any logs except a loading error. Obviously neotest is available to the neovim context since it is the thing invoking the require for itself. I'm not sure where to go from this. This all started after an upgrade of neotest which had the nio update.

``` ERROR | 2024-04-23T15:51:23Z-0500 | ...an/.repro/plugins/neotest/lua/neotest/lib/subprocess.lua:76 | Failed to initialize child process Vim:Error invoking 'nvim_exec_lua' on channel 3: Error executing lua: [string ""]:1: module 'neotest' not found: no field package.preload['neotest'] no file './neotest.lua' no file '/opt/homebrew/share/luajit-2.1/neotest.lua' no file '/usr/local/share/lua/5.1/neotest.lua' no file '/usr/local/share/lua/5.1/neotest/init.lua' no file '/opt/homebrew/share/lua/5.1/neotest.lua' no file '/opt/homebrew/share/lua/5.1/neotest/init.lua' no file './neotest.so' no file '/usr/local/lib/lua/5.1/neotest.so' no file '/opt/homebrew/lib/lua/5.1/neotest.so' no file '/usr/local/lib/lua/5.1/loadall.so' stack traceback: [C]: in function 'require' [string ""]:1: in main chunk stack traceback: [C]: in function 'rpcrequest' ...an/.repro/plugins/neotest/lua/neotest/lib/subprocess.lua:61: in function <...an/.repro/plugins/neotest/lua/neotest/lib/subprocess.lua:54> [C]: in function 'xpcall' ...an/.repro/plugins/neotest/lua/neotest/lib/subprocess.lua:54: in function 'init' ...ffman/.repro/plugins/neotest/lua/neotest/client/init.lua:374: in function '_start' ...ffman/.repro/plugins/neotest/lua/neotest/client/init.lua:188: in function '_ensure_started' ...ffman/.repro/plugins/neotest/lua/neotest/client/init.lua:200: in function <...ffman/.repro/plugins/neotest/lua/neotest/client/init.lua:199> ...man/.repro/plugins/neotest/lua/neotest/consumers/run.lua:38: in function 'get_tree_from_args' ...man/.repro/plugins/neotest/lua/neotest/consumers/run.lua:68: in function 'func' ...stopherhoffman/.repro/plugins/nvim-nio/lua/nio/tasks.lua:168: in function <...stopherhoffman/.repro/plugins/nvim-nio/lua/nio/tasks.lua:167> ```
cehoffman commented 2 months ago

I was able to come back and spend some time on this. Digging through the code a bit, it looks like the suggested config for enabling the more detailed logging was wrong.

"nvim-neotest/neotest",
-- other key/vals ...
opts = {
  level = vim.log.levels.DEBUG,
  adapters = {
    ["neotest-go"] = {
      -- Here we can set options for neotest-go, e.g.
      -- args = { "-tags=integration" }
      recursive_run = true,
    },
  },
},

needed to be

"nvim-neotest/neotest",
-- other key/vals ...
opts = {
  log_level = vim.log.levels.DEBUG,
  adapters = {
    ["neotest-go"] = {
      -- Here we can set options for neotest-go, e.g.
      -- args = { "-tags=integration" }
      recursive_run = true,
    },
  },
},

After adjusting this I do see more details, but they really are not anything more enlightening than what the original logs contained. For some reason the spawned nvim subprocess isn't not getting the lua module paths setup correctly and is not able to load neotest in the subprocess neovim editor. I'll be trying to look at this more, but I haven't changed anything about this unless lazy.nvim is somehow broken in subprocess neovim.

``` INFO | 2024-05-03T13:15:20Z-0500 | ...an/.repro/plugins/neotest/lua/neotest/lib/subprocess.lua:33 | Starting child process INFO | 2024-05-03T13:15:20Z-0500 | ...an/.repro/plugins/neotest/lua/neotest/lib/subprocess.lua:36 | Parent address: localhost:59370 INFO | 2024-05-03T13:15:20Z-0500 | ...an/.repro/plugins/neotest/lua/neotest/lib/subprocess.lua:42 | Starting child process with command: /Users/christopherhoffman/.homebrew/Cellar/neovim/0.9.5/bin/nvim --embed --headless -n ERROR | 2024-05-03T13:15:20Z-0500 | ...an/.repro/plugins/neotest/lua/neotest/lib/subprocess.lua:76 | Failed to initialize child process Vim:Error invoking 'nvim_exec_lua' on channel 3: Error executing lua: [string ""]:1: module 'neotest' not found: no field package.preload['neotest'] no file './neotest.lua' no file '/opt/homebrew/share/luajit-2.1/neotest.lua' no file '/usr/local/share/lua/5.1/neotest.lua' no file '/usr/local/share/lua/5.1/neotest/init.lua' no file '/opt/homebrew/share/lua/5.1/neotest.lua' no file '/opt/homebrew/share/lua/5.1/neotest/init.lua' no file './neotest.so' no file '/usr/local/lib/lua/5.1/neotest.so' no file '/opt/homebrew/lib/lua/5.1/neotest.so' no file '/usr/local/lib/lua/5.1/loadall.so' stack traceback: [C]: in function 'require' [string ""]:1: in main chunk stack traceback: [C]: in function 'rpcrequest' ...an/.repro/plugins/neotest/lua/neotest/lib/subprocess.lua:61: in function <...an/.repro/plugins/neotest/lua/neotest/lib/subprocess.lua:54> [C]: in function 'xpcall' ...an/.repro/plugins/neotest/lua/neotest/lib/subprocess.lua:54: in function 'init' ...ffman/.repro/plugins/neotest/lua/neotest/client/init.lua:372: in function '_start' ...ffman/.repro/plugins/neotest/lua/neotest/client/init.lua:186: in function '_ensure_started' ...ffman/.repro/plugins/neotest/lua/neotest/client/init.lua:171: in function 'get_adapters' ...lugins/neotest/lua/neotest/consumers/summary/summary.lua:114: in function <...lugins/neotest/lua/neotest/consumers/summary/summary.lua:113> INFO | 2024-05-03T13:15:20Z-0500 | ...an/.repro/plugins/neotest/lua/neotest/lib/subprocess.lua:20 | Closing child channel INFO | 2024-05-03T13:15:20Z-0500 | ...ffman/.repro/plugins/neotest/lua/neotest/client/init.lua:378 | Initialising client INFO | 2024-05-03T13:15:20Z-0500 | ...repro/plugins/neotest/lua/neotest/client/events/init.lua:48 | Emitting starting event ```

Edit:

I've crafted the start command that is used for the subprocess and confirmed it should be able to load neotest.

$ ~/.homebrew/Cellar/neovim/0.9.5/bin/nvim -n --headless +"lua print(require'neotest' and 0)" +q
0

This is missing the --embed flag which as I understand it is purely there to make neovim work in an RPC mode, however that seems to impact how the library loading is done or the initialization code that sets up the path loading is triggered.

Edit 5/7: Updated to HEAD neovim incase there is something there. No change in behavior. My environment variables are very trimmed down and contain nothing lua related, so I'm currently at a loss why why the subprocess neovim is unable to load modules that the parent obviously can.

cehoffman commented 2 months ago

After getting dead ended at the rpcrequest failure to load neotest module, I decided that it maybe has something to do with the reproduction config and launching neovim with the explicit config file. Sure enough that appears to have been the problem with the reproduction config. One of the things I did trying to get this working did fix the problem in my main configuration. The main configuration has been unchanged since this problem began.

I believe this was caused by Homebrew actually. I encountered a problem trying to install neovim HEAD with homebrew where the compile couldn't find the lpeg library even though Homebrew is managing the library and explicitly setting it for the compile. After a bit of searching for why, I stumbled upon this comment which advice from another thread to try reinstalling lpeg from Homebrew.

Running brew reinstall lpeg and having it rewrite the bottle and the compile succeeded. This is basically the only tooling change that actually happened. I didn't even remove and reinstall the stable neovim. It seems this all came down to an errant issue with Homebrew and managing the lpeg linkage. I'm a bit puzzled why nothing spit out library load failures, but testing is working again. It seems the upgrade to neotest was just coicidental to probably a closely timed upgrade in Homebrew.

Edit:

Running brew remove lpeg and trying again still succeeds. I guess I'm now really at a loss. Potentially some other tool upgrade between reporting and now is related since I had been trying to debug this with the reproduction config and finally determined the reproduction config is fails to meet the rpcrequest needs.