nvim-neotest / neotest

An extensible framework for interacting with tests within NeoVim.
MIT License
2.22k stars 107 forks source link

[BUG] Lags and times out on first 1-2 runs on MacOS #207

Open roginfarrer opened 1 year ago

roginfarrer commented 1 year ago

New issue from this comment: https://github.com/nvim-neotest/neotest/issues/198#issuecomment-1431991497

NeoVim Version NVIM v0.9.0-dev-3545+g6c39edaa7-dirty Build type: Release LuaJIT 2.1.0-beta3

Describe the bug When I run test_file, I get hit with some lag in my editor, and eventually the lag recedes but no test has been run or feedback given. I hit it again, and sometimes I get nothing, sometimes I get "No test found", or sometimes the test runs.

I tried setting discovery.concurrent = 1, but then I get this error:

Error executing vim.schedule lua callback: ...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:18: The coroutine failed with this message: ...local/share/nvim/lazy/neotest/lua/neotest/types/tree.lua:179: attempt to index a nil value
stack traceback:
    [C]: in function 'error'
    ...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:18: in function 'callback_or_next'
    ...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:45: in function <...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:44>

I also sometimes get this error:

Error detected while processing BufEnter Autocommands for "*":
Error executing lua callback: ...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:18: The coroutine failed with this message: ...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:18: The coroutine failed with this message: ...local/share/nvim/lazy/neotest/lua/neotest/types/tree.lua:179: attempt to index a nil value
stack traceback:
    [C]: in function 'error'
    ...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:18: in function 'callback_or_next'
    ...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:45: in function 'step'
    ...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:48: in function 'execute'
    ...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:108: in function 'run'
    ...ocal/share/nvim/lazy/neotest/lua/neotest/client/init.lua:461: in function <...ocal/share/nvim/lazy/neotest/lua/neotest/client/init.lua:459>

To Reproduce Please provide a minimal init.lua to reproduce which can be run as the following:

nvim --clean -u minimal.lua

You can edit the following example file to include your adapters and other required setup.

-- ignore default config and plugins
vim.opt.runtimepath:remove(vim.fn.expand '~/.config/nvim')
vim.opt.packpath:remove(vim.fn.expand '~/.local/share/nvim/site')
vim.opt.termguicolors = true

-- append test directory
local test_dir = '/tmp/nvim-config'
vim.opt.runtimepath:append(vim.fn.expand(test_dir))
vim.opt.packpath:append(vim.fn.expand(test_dir))

-- install packer
local install_path = test_dir .. '/pack/packer/start/packer.nvim'
local install_plugins = false

if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
  vim.cmd('!git clone https://github.com/wbthomason/packer.nvim ' .. install_path)
  vim.cmd 'packadd packer.nvim'
  install_plugins = true
end

local packer = require 'packer'

packer.init {
  package_root = test_dir .. '/pack',
  compile_path = test_dir .. '/plugin/packer_compiled.lua',
}

packer.startup(function(use)
  use 'wbthomason/packer.nvim'

  use {
    'nvim-neotest/neotest',
    requires = {
      'nvim-lua/plenary.nvim',
      'nvim-treesitter/nvim-treesitter',
      'antoinemadec/FixCursorHold.nvim',
      'haydenmeade/neotest-jest',
    },
    config = function()
      require('neotest').setup {
        adapters = {
          require 'neotest-jest' {
            cwd = function(path)
              local cwd = require('neotest-jest.util').find_package_json_ancestor(path)
              return cwd
            end,
          },
        },
      }
    end,
  }

  if install_plugins then
    packer.sync()
  end
end)

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()
]]

Here's an example test:

import React from 'react';
import {render, screen} from '@testing-library/react';

function Button(props) {
  return <button {...props} />
}

describe('Button', () => {
  it('renders a button', () => {
    render(<Button>hey</Button>);
    expect(screen.getByRole('button')).toBeInTheDocument();
  });
});

Logs

WARN | 2023-02-17T10:12:28Z-0800 | ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:156 | CHILD | Error in remote call ...39eda/share/nvim/runtime/lua/vim/treesitter/language.lua:23: '' is not a valid language name
stack traceback:
    [C]: in function 'error'
    ...39eda/share/nvim/runtime/lua/vim/treesitter/language.lua:23: in function 'require_language'
    ...m/HEAD-6c39eda/share/nvim/runtime/lua/vim/treesitter.lua:124: in function 'get_string_parser'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:118: in function 'get_parse_root'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:159: in function 'parse_positions_from_string'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:206: in function 'func'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:148: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147>
    [C]: in function 'xpcall'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:146>
WARN | 2023-02-17T10:12:28Z-0800 | ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:156 | CHILD | Error in remote call ...39eda/share/nvim/runtime/lua/vim/treesitter/language.lua:23: '' is not a valid language name
stack traceback:
    [C]: in function 'error'
    ...39eda/share/nvim/runtime/lua/vim/treesitter/language.lua:23: in function 'require_language'
    ...m/HEAD-6c39eda/share/nvim/runtime/lua/vim/treesitter.lua:124: in function 'get_string_parser'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:118: in function 'get_parse_root'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:159: in function 'parse_positions_from_string'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:206: in function 'func'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:148: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147>
    [C]: in function 'xpcall'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:146>
WARN | 2023-02-17T10:12:30Z-0800 | ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:156 | CHILD | Error in remote call ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: query: invalid node type at position 66 for language json
stack traceback:
    [C]: in function '_ts_parse_query'
    ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: in function 'parse_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:103: in function 'normalise_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:160: in function 'parse_positions_from_string'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:206: in function 'func'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:148: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147>
    [C]: in function 'xpcall'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:146>
WARN | 2023-02-17T10:12:31Z-0800 | ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:156 | CHILD | Error in remote call ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: query: invalid node type at position 66 for language json
stack traceback:
    [C]: in function '_ts_parse_query'
    ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: in function 'parse_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:103: in function 'normalise_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:160: in function 'parse_positions_from_string'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:206: in function 'func'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:148: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147>
    [C]: in function 'xpcall'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:146>
WARN | 2023-02-17T10:12:32Z-0800 | ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:156 | CHILD | Error in remote call ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: query: invalid node type at position 66 for language json
stack traceback:
    [C]: in function '_ts_parse_query'
    ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: in function 'parse_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:103: in function 'normalise_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:160: in function 'parse_positions_from_string'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:206: in function 'func'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:148: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147>
    [C]: in function 'xpcall'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:146>
WARN | 2023-02-17T10:12:33Z-0800 | ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:156 | CHILD | Error in remote call ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: query: invalid node type at position 66 for language json
stack traceback:
    [C]: in function '_ts_parse_query'
    ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: in function 'parse_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:103: in function 'normalise_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:160: in function 'parse_positions_from_string'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:206: in function 'func'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:148: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147>
    [C]: in function 'xpcall'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:146>
WARN | 2023-02-17T10:12:34Z-0800 | ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:156 | CHILD | Error in remote call ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: query: invalid node type at position 66 for language json
stack traceback:
    [C]: in function '_ts_parse_query'
    ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: in function 'parse_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:103: in function 'normalise_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:160: in function 'parse_positions_from_string'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:206: in function 'func'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:148: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147>
    [C]: in function 'xpcall'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:146>
WARN | 2023-02-17T10:12:35Z-0800 | ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:156 | CHILD | Error in remote call ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: query: invalid node type at position 66 for language json
stack traceback:
    [C]: in function '_ts_parse_query'
    ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: in function 'parse_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:103: in function 'normalise_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:160: in function 'parse_positions_from_string'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:206: in function 'func'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:148: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147>
    [C]: in function 'xpcall'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:146>
WARN | 2023-02-17T10:12:36Z-0800 | ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:156 | CHILD | Error in remote call ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: query: invalid node type at position 66 for language json
stack traceback:
    [C]: in function '_ts_parse_query'
    ...-6c39eda/share/nvim/runtime/lua/vim/treesitter/query.lua:261: in function 'parse_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:103: in function 'normalise_query'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:160: in function 'parse_positions_from_string'
    ...re/nvim/lazy/neotest/lua/neotest/lib/treesitter/init.lua:206: in function 'func'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:148: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147>
    [C]: in function 'xpcall'
    ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:147: in function <...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:146>

Additional context

rcarriga commented 1 year ago

Unfortunately I can't reproduce. I'm not sure if this is a treesitter issue or an issue with the subprocess loading. Can you try reinstall your treesitter parsers? If that doesn't work, can you comment out this line https://github.com/nvim-neotest/neotest/blob/b06b0ccbddc61acfe781689da969d2e0117b3dfe/lua/neotest/client/init.lua#L373 to disable the subprocess starting and see if the behaviour is the same?

roginfarrer commented 1 year ago

That does seem to help! Both "nearest test" and the "file tests" kick-off on the first try.

Reinstalling the parsers didn't have an effect, but commenting out that line did.

rcarriga commented 1 year ago

Is this happening for all jest projects? Can you point to a public repo I can test?

Right now I believe the issues lies with plenary's filetype detection https://github.com/nvim-neotest/neotest/blob/b06b0ccbddc61acfe781689da969d2e0117b3dfe/lua/neotest/lib/treesitter/init.lua#L115 but I'm not sure what would cause the issue. If you can't provide a public repo to reproduce, the only thing I can suggest is manually logging such as after that line adding logger.info(file_path, "detected as", ft) and seeing if the values are valid

lukesarnacki commented 1 year ago

I have a similar issue on fedora. I have the same setup (via dotfiles repo) on my Mac as well. For some reason on Mac it works fine but on linux it works only if I comment the following line you mentioned in the other comment:

lib.subprocess.init() 

Stacktrace from neotest.log:

ERROR | 2023-03-16T18:54:42Z+0100 | ...te/pack/packer/start/neotest/lua/neotest/client/init.lua:310 | Couldn't find positions in path /home/luke/projects/diva/store-bridge/dist/__tests__/server.js.map ...0.8.3/share/nvim/runtime/lua/vim/treesitter/language.lua:23: '' is not a valid language name
stack traceback:
    [C]: in function 'error'
    ...0.8.3/share/nvim/runtime/lua/vim/treesitter/language.lua:23: in function 'require_language'
    ...r/neovim/0.8.3/share/nvim/runtime/lua/vim/treesitter.lua:116: in function 'get_string_parser'
    ...packer/start/neotest/lua/neotest/lib/treesitter/init.lua:118: in function 'get_parse_root'
    ...packer/start/neotest/lua/neotest/lib/treesitter/init.lua:159: in function 'parse_positions_from_string'
    ...packer/start/neotest/lua/neotest/lib/treesitter/init.lua:206: in function 'discover_positions'
    ...te/pack/packer/start/neotest/lua/neotest/client/init.lua:301: in function <...te/pack/packer/start/neotest/lua/neotest/client/init.lua:265>
    [C]: in function 'xpcall'
    ...te/pack/packer/start/neotest/lua/neotest/client/init.lua:265: in function '_update_positions'
    ...te/pack/packer/start/neotest/lua/neotest/client/init.lua:318: in function <...te/pack/packer/start/neotest/lua/neotest/client/init.lua:316>

Are there any other details I could provide?

rcarriga commented 1 year ago

Can you enable debug logging, add the log line as I detailed here https://github.com/nvim-neotest/neotest/issues/207#issuecomment-1447823605 and provide the full log?

bjornsnoen commented 1 year ago

So I'm running on arch linux and for me it just freezes completely, no lag, but commenting out the lib.subprocess.init line works for me as well. I added the log line, but it freezes before it can log anything. Doesn't matter how long I wait, only way out is to kill the neovim process. My config is at https://github.com/bjornsnoen/dotfiles/tree/master/nvim for reproducing.

I've been trying to create a solid set of steps to reproduce, but this is the best I've got.

  1. Install neotest and neotest-vitest
  2. Create a new nextjs app with npx create-next-app all defaults, default app name is my-app
  3. Install vitest with npm i vitest inside the new app
  4. Create the file src/tests/something.test.ts
  5. Write an empty test
    • import { test } from "vitest"; test("something", () => {});
    • If using my dotfiles, add scripts.test: "vitest" to package.json
  6. Run the test. The following error pops up. It seems to always happen right away.
Error executing vim.schedule lua callback: ...e/bjorn/.local/share/nvim/lazy/neotest/lua/nio/tasks.lua:95: Async task failed without callback: The coroutine failed with this message:          
...e/bjorn/.local/share/nvim/lazy/neotest/lua/nio/tasks.lua:95: Async task failed without callback: The coroutine failed with this message:                                                     
...vim/lazy/neotest/lua/neotest/consumers/state/tracker.lua:47: attempt to index local 'tree' (a nil value)                                                                                     
stack traceback:                                                                                                                                                                                
        ...vim/lazy/neotest/lua/neotest/consumers/state/tracker.lua: in function 'is_test'                                                                                                      
        ...vim/lazy/neotest/lua/neotest/consumers/state/tracker.lua:155: in function 'update_running'                                                                                           
        ...e/nvim/lazy/neotest/lua/neotest/consumers/state/init.lua:49: in function 'listener'                                                                                                  
        ...are/nvim/lazy/neotest/lua/neotest/client/events/init.lua:51: in function <...are/nvim/lazy/neotest/lua/neotest/client/events/init.lua:47>                                            
stack traceback:                                                                                                                                                                                
        [C]: in function 'error'                                                                                                                                                                
        ...e/bjorn/.local/share/nvim/lazy/neotest/lua/nio/tasks.lua:95: in function 'close_task'                                                                                                
        ...e/bjorn/.local/share/nvim/lazy/neotest/lua/nio/tasks.lua:117: in function 'step'                                                                                                     
        ...e/bjorn/.local/share/nvim/lazy/neotest/lua/nio/tasks.lua:143: in function 'run'                                                                                                      
        ...are/nvim/lazy/neotest/lua/neotest/client/events/init.lua:47: in function 'emit'                                                                                                      
        ...hare/nvim/lazy/neotest/lua/neotest/client/state/init.lua:96: in function 'update_running'                                                                                            
        ...ocal/share/nvim/lazy/neotest/lua/neotest/client/init.lua:82: in function 'run_tree'                                                                                                  
        ...al/share/nvim/lazy/neotest/lua/neotest/consumers/run.lua:72: in function 'func'                                                                                                      
        ...e/bjorn/.local/share/nvim/lazy/neotest/lua/nio/tasks.lua:166: in function <...e/bjorn/.local/share/nvim/lazy/neotest/lua/nio/tasks.lua:165>                                          
stack traceback:                                                                                                                                                                                
        [C]: in function 'error'                                                                                                                                                                
        ...e/bjorn/.local/share/nvim/lazy/neotest/lua/nio/tasks.lua:95: in function 'close_task'                                                                                                
        ...e/bjorn/.local/share/nvim/lazy/neotest/lua/nio/tasks.lua:117: in function 'cb'                                                                                                       
        ...e/bjorn/.local/share/nvim/lazy/neotest/lua/nio/tasks.lua:179: in function <...e/bjorn/.local/share/nvim/lazy/neotest/lua/nio/tasks.lua:178> 
  1. Ignore the error, go about your merry day. Running the test again works just fine.
  2. Exit vim
  3. Reopen vim, attempt to run the test
  4. Observe the freeze

Note that this fs path is now tainted somehow. If you rm the whole app directory and start over reusing the same app name, it will skip the error in step 6 and go straight to step 10. Again, commenting out the subrocess.init line does fix this. It also brings back the error in step 6 so it happens every time.

Hope this helps a bit!