fix: gitsigns not automatically attached due to async not returning #914
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Attempt to fix #903.
What's Going On?
This bug is triggered when opening neovim inside a directory, with both neo-tree and this plugin installed.
Opening the directory causes neovim to create a netrw buffer.
When the
setup()
function of this plugin is called, the plugin will eventually reach this particular part of the code:gitsigns.nvim/lua/gitsigns.lua
Lines 122 to 128 in 37d26d7
At this point in time, the netrw buffer exists, so
M.attach()
will be called with that buffer.M.attach()
will eventually give up control here withasync.scheduler_if_buf_valid()
:gitsigns.nvim/lua/gitsigns/attach.lua
Lines 296 to 299 in 37d26d7
After giving up control, the neotree plugin will run the "hijack netrw" logic (NB: hijacking is the default behaviour of neotree and does not need to be configured manually.) The hijack logic can be found here:
https://github.com/nvim-neo-tree/neo-tree.nvim/blob/f86e871584bd3c5a00b4ff8344305889eb52ebff/lua/neo-tree/setup/netrw.lua#L53-L101
As part of the hijack, it deletes the netrw buffer:
After the hijack is done, the
gitsigns
plugin regains control here:gitsigns.nvim/lua/gitsigns/async.lua
Lines 183 to 189 in 37d26d7
Unfortunately,
buf
is notnil
(it is the ID of the now-deleted netrw buffer), andvim.api.nvim_buf_is_valid(buf)
returns false, socb()
is never executed, and so we never continue executing.This is problematic, because the code after the for loop that contains
M.attach()
is the one responsible for adding the attachautocmd
, but this adding is never executed:Solution
Moved the autocmd to before the for loop, instead of after the for loop.
This solution feels like a temporary band-aid, but it does fix the problem.