Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Neovim freezes forever when saving test file #468

Open
Skyppex opened this issue Nov 7, 2024 · 12 comments
Open

[BUG] Neovim freezes forever when saving test file #468

Skyppex opened this issue Nov 7, 2024 · 12 comments
Assignees

Comments

@Skyppex
Copy link

Skyppex commented Nov 7, 2024

NeoVim Version
Output of nvim --version
NVIM v0.10.2
Build type: Release
LuaJIT 2.1.1713484068
Run "nvim -V1 -v" for more info

System info
Windows 11
Neovim is running on wezterm 20240203-110809-5046fc22

Describe the bug
Neovim freezes when saving a file which contains tests that neotest can discover.

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.

vim.opt.runtimepath:remove(vim.fn.expand("~/.config/nvim"))
vim.opt.packpath:remove(vim.fn.expand("~/.local/share/nvim/site"))

local lazypath = "/tmp/lazy/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",
		"--branch=stable", -- latest stable release
		lazypath,
	})
end

vim.g.mapleader = " "
vim.g.maplocalleader = " "

vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
	"nvim-neotest/neotest",
	dependencies = {
		"nvim-lua/plenary.nvim",
		"nvim-treesitter/nvim-treesitter",
		"nvim-neotest/nvim-nio",
		-- Install adapters here
		"Issafalcon/neotest-dotnet",
	},
	config = function()
		-- Install any required parsers
		require("nvim-treesitter.configs").setup({
			ensure_installed = {},
		})

		require("neotest").setup({
			-- Add adapters to the list
			adapters = {
				require("neotest-dotnet"),
			},
		})
	end,
})

local nmap = function(l, r, d)
	vim.keymap.set("n", l, r, {
		noremap = true,
		silent = true,
		desc = d,
	})
end

local neotest = require("neotest")

nmap("<leader>nr", function()
	neotest.run.run()
end, "Run test")

nmap("<leader>nf", function()
	neotest.run.run(vim.fn.expand("%"))
end, "Run tests in file")

nmap("<leader>nd", function()
	neotest.run.run({ strategy = "dap" })
end, "Debug test")

nmap("<leader>ns", function()
	neotest.run.stop()
end, "Stop test")

nmap("<leader>nsa", function()
	neotest.run.stop(vim.fn.expand("%"))
end, "Stop all tests")

nmap("<leader>na", function()
	neotest.run.attach()
end, "Attach to test")

nmap("<leader>no", function()
	neotest.output.open()
end, "Open test output")

nmap("<leader>tn", function()
	neotest.summary.toggle()
end, "Toggle test summary")

Steps to reproduce the behavior:

  1. Go to any test file in a dotnet project.
  2. Add some new-lines
  3. Wait a few seconds until it's frozen

Please provide example test files to reproduce.

Expected behavior
I expect neovim to not freeze forever when saving a file with tests.
Perhaps a second of freezing is acceptable, but making the editor unusable is unhelpful.

Logs
No logs are produced

Additional context
It happend very consistently, but in some odd cases it seemed to be fine. If i created a file and wrote a test, then saved and ran the test, it worked. However even then if i navigated to existing tests, made some edits, and saved, it would freeze again.
Here is what the Windows Task Manager looks like. This is the only instance on nvim.exe in the task manager.
image
I'm not sure what to make of it except some process has been marked as suspended

@Skyppex
Copy link
Author

Skyppex commented Nov 8, 2024

I just tried for a rust project i have and i'm getting the exact same problem

@adrianhelvik
Copy link

adrianhelvik commented Dec 14, 2024

When I conditionally load neotest-jest the problem goes away. Not ideal, but I have this setup to work around it:

	config = function()
		local neotest = require("neotest")
		local neotestJest = require("neotest-jest")
		local jestUtil = require("neotest-jest.jest-util")

		local setup_complete = false

		vim.diagnostic.config({ virtual_text = true })

		local function possibly_init()
			if setup_complete then
				return
			end
			local adapters = {}

			-- Load neotest-jest conditionally
			if vim.bo.filetype == "typescript" or vim.bo.filetype == "typescriptreact" then
				table.insert(
					adapters,
					neotestJest({
						jestCommand = jestUtil.getJestCommand(vim.fn.expand("%:p:h")),
						cwd = function()
							return vim.fs.dirname(vim.fn.expand("%"))
						end,
					})
				)
			end

			table.insert(adapters, require("neotest-gradle"))
			table.insert(adapters, require("neotest-rust"))

			neotest.setup({
				log_level = vim.log.levels.DEBUG,
				floating = {
					border = "rounded",
					max_height = 100,
					max_width = 100,
					options = {},
				},
				adapters = adapters,
			})
			setup_complete = true
		end

		local function test_nearest()
			possibly_init()
			neotest.run.run({ suite = false })
			neotest.summary.open()
		end

		local function watch_tests()
			possibly_init()
			neotest.watch.watch({
				suite = false,
			})
			neotest.summary.open()
		end

		vim.keymap.set("n", "<leader>tn", test_nearest, { desc = "[T]est [N]earest" })
		vim.keymap.set("n", "<leader>tw", watch_tests, { desc = "[T]est [W]atch" })
	end

Maybe something similar is happening with the dotnet adapter?

@mikaoelitiana
Copy link

I am encountering the same issue with neotest-mocha

@lanej
Copy link

lanej commented Dec 29, 2024

I have the same issue but it happens every time I load neovim. Using neovim --clean to remove neotest config solves the issue entirely.

@ruslanSorokin
Copy link

ruslanSorokin commented Jan 1, 2025

I have the same issue after saving a test file.

As according to the logs' last line:

DEBUG | 2025-01-01T06:30:09Z+0300 | ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:140 | Waiting for result 3

This indicates that the problem lies in the neotest.lib.subprocess module.

And after examining 2 call sites of the former function I found that the call from neotest.lib.treesitter.parse_positions consistently causes the endless waiting.

As a workaround, I tried hard-coding subprocess:enabled as false and it resolved the issue. I am no longer experiencing freezing.

@ruslanSorokin
Copy link

I have the same issue after saving a test file.

As according to the logs' last line:

DEBUG | 2025-01-01T06:30:09Z+0300 | ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:140 | Waiting for result 3

This indicates that the problem lies in the neotest.lib.subprocess module.

And after examining 2 call sites of the former function I found that the call from neotest.lib.treesitter.parse_positions consistently causes the endless waiting.

As a workaround, I tried hard-coding subprocess:enabled as false and it resolved the issue. I am no longer experiencing freezing.

Small update: it wasn't necessary to completely disable subprocesses on a per-module basis. I managed to resolve the issue by reading files during test discovery in a blocking manner: https://github.com/ruslanSorokin/neotest/tree/hotfix/issue-468/configure-use-of-subprocesses-in-parse_positions

This is certainly not a solution, as UI blocks. However, it at least allows us to identify whether we are encountering the same problem.

Snippet to check it out:

  {
    "ruslanSorokin/neotest",
    branch = "hotfix/issue-468/configure-use-of-subprocesses-in-parse_positions",
    opts = { discovery = { use_subprocess = false } },
  }

@Skyppex
Copy link
Author

Skyppex commented Jan 3, 2025

I've tested using your branch @ruslanSorokin with a C# and Rust project and it doesn't seem to made a difference wether use_subprocess is true or false. I did add all the dependencies i needed which is a difference from your snippet:

{
	"ruslanSorokin/neotest",
	branch = "hotfix/issue-468/configure-use-of-subprocesses-in-parse_positions",
	opts = { discovery = { use_subprocess = false } },
	dependencies = {
		"nvim-neotest/nvim-nio",
		"nvim-lua/plenary.nvim",
		"antoinemadec/FixCursorHold.nvim",
		"nvim-treesitter/nvim-treesitter",
		"Issafalcon/neotest-dotnet",
		"rouge8/neotest-rust",
	},
}

A while i ago i tried to clone the repo and debug it. Unfortunately i didn't make a new comment back then about what i learned, but from what i remember i think it had something to do with the state being nil where it shouldn't be. Can't remember what function though.

I might just go through that again, i'm just not very versed in lua debugging yet so this repo is a little tricky for me xD

@tehdb
Copy link

tehdb commented Jan 4, 2025

same here with marilari88/neotest-vitest, my lazyvim configs:

return {
  {
    "nvim-neotest/neotest",
    dependencies = {
      "marilari88/neotest-vitest",
    },
    opts = {
      log_level = 0,
      adapters = {
        ["neotest-vitest"] = {
          filter_dir = function(name, rel_path, root)
            return name ~= "node_modules"
          end
        }
      }
    }
  }
}

the last lines written to ~/.local/state/nvim/neotest.log:

DEBUG | 2025-01-05T00:18:52Z+0100 | ...are/nvim/lazy/neotest/lua/neotest/client/events/init.lua:50 | Calling listener summary for event test_focused
INFO | 2025-01-05T00:18:53Z+0100 | ...are/nvim/lazy/neotest/lua/neotest/client/events/init.lua:48 | Emitting test_focused event
DEBUG | 2025-01-05T00:18:53Z+0100 | ...are/nvim/lazy/neotest/lua/neotest/client/events/init.lua:50 | Calling listener summary for event test_focused
INFO | 2025-01-05T00:18:54Z+0100 | ...are/nvim/lazy/neotest/lua/neotest/client/events/init.lua:48 | Emitting test_focused event
DEBUG | 2025-01-05T00:18:54Z+0100 | ...are/nvim/lazy/neotest/lua/neotest/client/events/init.lua:50 | Calling listener summary for event test_focused
INFO | 2025-01-05T00:19:00Z+0100 | ...ocal/share/nvim/lazy/neotest/lua/neotest/client/init.lua:299 | Parsing ~/Workspace/faker-cli/test/cli.test.ts
DEBUG | 2025-01-05T00:19:00Z+0100 | ...l/share/nvim/lazy/neotest/lua/neotest/lib/subprocess.lua:140 | Waiting for result 2
$ nvim --version                                                                                                                                                                                                                                                                27ms 0
NVIM v0.10.3
Build type: Release
LuaJIT 2.1.1734355927

$ sw_vers                                                                                                                                                                                                                                                                       42ms 0
ProductName:            macOS
ProductVersion:         15.2
BuildVersion:           24C101

○ neotest
    version 5.8.0
    commit  d66cf4e

@skolj
Copy link

skolj commented Jan 5, 2025

I'm facing a similar issue when running python tests. Neovim freezes when running any neotest commands. I see the following inside neotest.log:

ERROR | 2025-01-04T11:24:59Z-0500 | .../share/nvimLazy/lazy/neotest/lua/neotest/client/init.lua:309 | Couldn't find positions in path <test_file_path> ...are/nvimLazy/lazy/neotest/lua/neotest/lib/subprocess.lua:147: Invalid subprocess call: Vim:Error invoking 'nvim_exec_lua' on channel 3:

@AbaoFromCUG
Copy link

I'm facing a similar issue when running python tests. Neovim freezes when running any neotest commands. I see the following inside neotest.log:我在运行 python 测试时遇到类似的问题。运行任何 neotest 命令时 Neovim 都会冻结。我在 neotest.log 中看到以下内容:

ERROR | 2025-01-04T11:24:59Z-0500 | .../share/nvimLazy/lazy/neotest/lua/neotest/client/init.lua:309 | Couldn't find positions in path <test_file_path> ...are/nvimLazy/lazy/neotest/lua/neotest/lib/subprocess.lua:147: Invalid subprocess call: Vim:Error invoking 'nvim_exec_lua' on channel 3:

TLDR: flatten.nvim

I spent a few days looking for this problem, even looking through the neo-nio source code, and I found that it was a problem with flatten.nvim, because neotest subprocess was started in the same directory, so flatten.nvim caused the child process to exit immediately

@AbaoFromCUG
Copy link

@skolj
Related willothy/flatten.nvim#106

@skolj
Copy link

skolj commented Jan 7, 2025

@AbaoFromCUG Thanks for the analysis! I'm not using flatten.nvim though (not directly at least). Not sure if neotest or its dependencies use it.

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

No branches or pull requests

9 participants