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

fix: avoid arithmetic error (subtracting 1 from nil) #396

Merged
merged 1 commit into from
Apr 28, 2024

Conversation

fredrikaverpil
Copy link
Contributor

@fredrikaverpil fredrikaverpil commented Apr 24, 2024

Why this change?

I'm writing my own neotest adapter and I'm sending in error messages along with line numbers:

---@type string
local matched_line_number = string.match(line.Output, test_filename .. ":(%d+)")

---@type number | nil
local line_number = tonumber(matched_line_number)

if line_number ~= nil then
  ---@type neotest.Error
  error = { message = line.Output, line = line_number }
else
  ---@type neotest.Error
  error = { message = line.Output }
end

Full source here.

Sometimes, and only when a line number (an integer) is sent in, I see this crash in Neovim, and neotest breaks:

   Error  15:32:53 msg_show.lua_error Error executing vim.schedule lua callback: ...rik/.local/share/fredrik/lazy/nvim-nio/lua/nio/tasks.lua:95: Async task failed without callback: The coroutine failed with this message: 
...redrik/lazy/neotest/lua/neotest/consumers/diagnostic.lua:103: attempt to perform arithmetic on a nil value
stack traceback:
	...redrik/lazy/neotest/lua/neotest/consumers/diagnostic.lua: in function 'create_diagnostics'
	...redrik/lazy/neotest/lua/neotest/consumers/diagnostic.lua:54: in function 'draw_buffer'
	...redrik/lazy/neotest/lua/neotest/consumers/diagnostic.lua:167: in function 'draw_buffer'
	...redrik/lazy/neotest/lua/neotest/consumers/diagnostic.lua:229: in function 'listener'
	.../fredrik/lazy/neotest/lua/neotest/client/events/init.lua:51: in function <.../fredrik/lazy/neotest/lua/neotest/client/events/init.lua:47>
stack traceback:
	[C]: in function 'error'
	...rik/.local/share/fredrik/lazy/nvim-nio/lua/nio/tasks.lua:95: in function 'close_task'
	...rik/.local/share/fredrik/lazy/nvim-nio/lua/nio/tasks.lua:117: in function 'cb'
	...rik/.local/share/fredrik/lazy/nvim-nio/lua/nio/tasks.lua:183: in function <...rik/.local/share/fredrik/lazy/nvim-nio/lua/nio/tasks.lua:182>

Turns out that the function call (found on line diagnostic.lua:103) can return either integer or nil, and it's not a good idea to subtract 1 from its return value without first checking it for being nil:

col = mark_code:find("%S") - 1,

Function signature:

function string.find(s: string|number, pattern: string|number, init?: integer, plain?: boolean)
  -> start: integer|nil
  2. end: integer|nil
  3. ...any

What was done in this PR?

Instead of attempting to subtract 1 from potentially nil, I first check if the returned value is nil or not.

This completely remedies the crash and the error is now shown as expected in the buffer, which is very nice.

Notes

  • I'm not sure if 0 is a good value here to use for col, in case the returned value from the pattern matching is nil.
  • I realized I am supposed to do this: {error = { message = line.Output, line = line_number - 1 } when sending in the line number into neovim, which might help alleviate this problem.

@fredrikaverpil fredrikaverpil changed the title fix: avoid arithmetic error when adding 1 to nil fix: avoid arithmetic error when subtracting 1 from nil Apr 24, 2024
@fredrikaverpil fredrikaverpil changed the title fix: avoid arithmetic error when subtracting 1 from nil fix: avoid arithmetic error (subtracting 1 from nil) Apr 24, 2024
@rcarriga
Copy link
Collaborator

Great thanks for the PR!

@rcarriga rcarriga merged commit 44536f5 into nvim-neotest:master Apr 28, 2024
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.

2 participants