NeogitOrg / neogit

An interactive and powerful Git interface for Neovim, inspired by Magit
MIT License
3.83k stars 228 forks source link

Neogit nightly commits are broken #1236

Closed avegancafe closed 5 months ago

avegancafe commented 5 months ago

Description

If I open neogit, and then close it, and open it again, all subsequent neogit tabs will throw this error:

stack traceback:                                                                                                                                                                             
^I[C]: in function 'nvim_exec2'                                                                                                                                                              
^Ivim/_editor.lua: in function 'cmd'                                                                                                                                                         
^I.../.local/share/nvim/lazy/neogit/lua/neogit/lib/buffer.lua:370: in function 'create_fold'                                                                                                 
^I.../.local/share/nvim/lazy/neogit/lua/neogit/lib/buffer.lua:198: in function 'set_folds'                                                                                                   
^I....local/share/nvim/lazy/neogit/lua/neogit/lib/ui/init.lua:626: in function 'update'                                                                                                      
^I....local/share/nvim/lazy/neogit/lua/neogit/lib/ui/init.lua:603: in function 'render'                                                                                                      
^I...hare/nvim/lazy/neogit/lua/neogit/buffers/status/init.lua:1224: in function 'callback'                                                                                                   
^I...share/nvim/lazy/neogit/lua/neogit/lib/git/repository.lua:246: in function 'callback'                                                                                                    
^I...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:25: in function 'callback_or_next'                                                                                             
^I...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:45: in function 'saved_callback'                                                                                               
^I...are/nvim/lazy/plenary.nvim/lua/plenary/async/control.lua:126: in function 'tx'                                                                                                          
^I.../share/nvim/lazy/plenary.nvim/lua/plenary/async/util.lua:67: in function 'callback'                                                                                                     
^I...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:25: in function 'callback_or_next'                                                                                             
^I...share/nvim/lazy/plenary.nvim/lua/plenary/async/async.lua:45: in function 'cb'                                                                                                           
^I...yle/.local/share/nvim/lazy/neogit/lua/neogit/process.lua:359: in function <...yle/.local/share/nvim/lazy/neogit/lua/neogit/process.lua:325> function: builtin#18 vim/_editor.lua:0: nvim
_exec2(): Vim(fold):E350: Cannot create fold with current 'foldmethod'

Monosnap screencast Apr 5

Neovim version

NVIM v0.10.0-dev-2104+gfd2ed024c-Homebrew Build type: Release LuaJIT 2.1.1710088188 Run "nvim -V1 -v" for more info

Operating system and version

macOS 14.4

Steps to reproduce

Update branch to nightly with lazy.nvim

Expected behavior

No response

Actual behavior

There are a ton of errors and I'm not able to use any of the neogit functions

Minimal config

-- NOTE: See the end of this file if you are reporting an issue, etc. Ignore all the "scary" functions up top, those are
-- used for setup and other operations.
local M = {}

local base_root_path = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h") .. "/.min"
function M.root(path)
  return base_root_path .. "/" .. (path or "")
end

function M.load_plugin(plugin_name, plugin_url)
  local package_root = M.root("plugins/")
  local install_destination = package_root .. plugin_name
  vim.opt.runtimepath:append(install_destination)

  if not vim.loop.fs_stat(package_root) then
    vim.fn.mkdir(package_root, "p")
  end

  if not vim.loop.fs_stat(install_destination) then
    print(string.format("> Downloading plugin '%s' to '%s'", plugin_name, install_destination))
    vim.fn.system({
      "git",
      "clone",
      "--depth=1",
      plugin_url,
      install_destination,
    })
    if vim.v.shell_error > 0 then
      error(string.format("> Failed to clone plugin: '%s' in '%s'!", plugin_name, install_destination),
        vim.log.levels.ERROR)
    end
  end
end

---@alias PluginName string The plugin name, will be used as part of the git clone destination
---@alias PluginUrl string The git url at which a plugin is located, can be a path. See https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols for details
---@alias MinPlugins table<PluginName, PluginUrl>

---Do the initial setup. Downloads plugins, ensures the minimal init does not pollute the filesystem by keeping
---everything self contained to the CWD of the minimal init file. Run prior to running tests, reproducing issues, etc.
---@param plugins? table<PluginName, PluginUrl>
function M.setup(plugins)
  vim.opt.packpath = {}                      -- Empty the package path so we use only the plugins specified
  vim.opt.runtimepath:append(M.root(".min")) -- Ensure the runtime detects the root min dir

  -- Install required plugins
  if plugins ~= nil then
    for plugin_name, plugin_url in pairs(plugins) do
      M.load_plugin(plugin_name, plugin_url)
    end
  end

  vim.env.XDG_CONFIG_HOME = M.root("xdg/config")
  vim.env.XDG_DATA_HOME = M.root("xdg/data")
  vim.env.XDG_STATE_HOME = M.root("xdg/state")
  vim.env.XDG_CACHE_HOME = M.root("xdg/cache")

  -- NOTE: Cleanup the xdg cache on exit so new runs of the minimal init doesn't share any previous state, e.g. shada
  vim.api.nvim_create_autocmd("VimLeave", {
    callback = function()
      vim.fn.system({
        "rm",
        "-r",
        "-f",
        M.root("xdg")
      })
    end
  })
end

-- NOTE: If you have additional plugins you need to install to reproduce your issue, include them in the plugins
-- table within the setup call below.
M.setup({
  plenary = "https://github.com/nvim-lua/plenary.nvim.git",
  telescope = "https://github.com/nvim-telescope/telescope.nvim",
  diffview = "https://github.com/sindrets/diffview.nvim",
  neogit = "https://github.com/NeogitOrg/neogit"
})
-- WARN: Do all plugin setup, test runs, reproductions, etc. AFTER calling setup with a list of plugins!
-- Basically, do all that stuff AFTER this line.
require("neogit").setup({
  disable_commit_confirmation = true
  kind = tab
  auto_show_console = false
  graph_style = unicode
}) -- For instance, setup Neogit
CKolkey commented 5 months ago

Are you setting foldmethod anywhere in your config?

famiu commented 5 months ago

Unsure if this is related, but after updating Neogit to the latest commit (have not updated it for a few days) on the nightly branch in my Windows 11 VM, I am getting this error when I open a buffer or window:

Error executing luv callback:
.../nvim-data/lazy/plenary.nvim/lua/plenary/async/async.lua:18: The coroutine failed with this message: ...pData/Local/nvim-data/lazy/neogit/lua/neogit/process.lua:364: E5560: Vimscript function must not be called in a lua loop callback
stack traceback:
    [C]: in function 'error'
    .../nvim-data/lazy/plenary.nvim/lua/plenary/async/async.lua:18: in function 'callback_or_next'
    .../nvim-data/lazy/plenary.nvim/lua/plenary/async/async.lua:45: in function 'callback'
    ...vim-data/lazy/plenary.nvim/lua/plenary/async/control.lua:101: in function 'returned_function'
    .../nvim-data/lazy/plenary.nvim/lua/plenary/async/async.lua:31: in function 'callback_or_next'
    .../nvim-data/lazy/plenary.nvim/lua/plenary/async/async.lua:45: in function 'step'
    .../nvim-data/lazy/plenary.nvim/lua/plenary/async/async.lua:48: in function 'execute'
    .../nvim-data/lazy/plenary.nvim/lua/plenary/async/async.lua:118: in function 'fn'
    ...Data/Local/nvim-data/lazy/neogit/lua/neogit/lib/util.lua:520: in function <...Data/Local/nvim-data/lazy/neogit/lua/neogit/lib/util.lua:517>

This error does not occur on Linux for me.

CKolkey commented 5 months ago

Hmm. Thats the debounce, which is only called when refreshing the status buffer... ugh, windows.

Ok, windows or wsl?

famiu commented 5 months ago

ugh, windows.

As someone who's had a lot of trouble with having to debug Windows-specific bugs in the past as a Linux user, I fully relate 😆

Ok, windows or wsl?

Windows, not WSL.

CKolkey commented 5 months ago

I wonder why this is only on windows.. If you have time and feel like bisecting (haha), I'd appreciate it, since I don't have access to a windows machine/vm. Otherwise I'll try to get my hands on one at some point.

famiu commented 5 months ago

I wonder why this is only on windows.. If you have time and feel like bisecting (haha), I'd appreciate it, since I don't have access to a windows machine/vm. Otherwise I'll try to get my hands on one at some point.

Bisect leads me to commit ff2df2de4d7c0c2cdb8f1a7c2b665eb4b5a2d096

I should clarify, during the bisect, I came across a different, equally strange error where doing :Neogit leads me to an empty buffer instead of the usual. It didn't error, but it didn't do anything either. I counted it as a bad commit in the bisect. Seems like the comment where that starts happening is the same commit as the one I linked above.

After more bisecting, seems like the commit where the blank buffer is replaced by an error is commit d62fd23ec7e7f38ce438aad76daf4284a6e6a0b5

CKolkey commented 5 months ago

Yeah, right around there I was changing the order-of-operations with respect to opening the buffer and gathering repo state. Previously, we did repo state first, then opened the buffer as a callback. This worked great, unless you specified a different CWD for neogit. I used vim.fn.lcd() before opening neogit, which changed the wrong window.

Now we do it in the right order, but.. well, I still don't understand why this would only fail on windows.

avegancafe commented 5 months ago

@CKolkey for what it's worth I'm not on windows I'm on macos. But yes I'm setting my foldmethod to expr in my config

CKolkey commented 5 months ago

@CKolkey for what it's worth I'm not on windows I'm on macos. But yes I'm setting my foldmethod to expr in my config

No worries, your issue is an easy fix: set the foldmethod to manual more aggressively, in case something else messed with it after I opened the window. I'll push that in a bit

avegancafe commented 5 months ago

Ohh okay sounds good!

AThePeanut4 commented 5 months ago

@CKolkey I'm also getting the E5560: Vimscript function must not be called in a lua loop callback error, and it happens every time I open Neogit from a tab where I've used :tcd to change the working directory. Using either :cd or :lcd doesn't trigger the error. This is on both macOS and Linux.

After a bunch of debugging and troubleshooting, I believe I've managed to figure out the core issue, and it's a bug with Neovim itself: https://github.com/neovim/neovim/issues/28213. If any plugin (including Neogit itself) calls vim.api.nvim_buf_call between the vim.cmd.lcd call and the initial refresh of the buffer, or if Neovim internally opens the temporary "autocmd window" for any reason, then the window-local cwd for the status window will be lost. This then means that the git.repo access needs to create a new Repo instance, since the cwd changed, which then needs to find the git root dir, which it does by calling vim.fn.jobstart, which errors, because the original status buffer refresh call was debounced and is thus in a luv loop callback.

If I disable all my plugins except for Neogit, then I can only get it to error once, if I first change the tab-local cwd with :tcd and then only run :Neogit. Subsequent :Neogit calls then don't error. If I enable all my plugins, then it errors every time.

CKolkey commented 5 months ago

Pushed a commit to nightly that changes the order of operations regarding calling lcd() and drawing the buffer. I can't seem to reproduce this error - do you mind checking to see if this helps? I moved the call to after the last buffer:call() method during buffer creation.

AThePeanut4 commented 5 months ago

@CKolkey That seems to fix it for me, I'm not getting the error anymore :+1:

CKolkey commented 5 months ago

Hell yea - all thanks to your reporting 😇

CKolkey commented 5 months ago

@famiu are you still seeing issues on windows?

avegancafe commented 5 months ago

I stopped getting this error now @CKolkey ! This is maybe a different ticket, but do you know what setting could be set that'd cause these folds to render with the default fold preview? This doesn't happen on main, only nightly

image
famiu commented 5 months ago

@famiu are you still seeing issues on windows?

Seemed to be fixed as far as I can tell

CKolkey commented 5 months ago

I stopped getting this error now @CKolkey ! This is maybe a different ticket, but do you know what setting could be set that'd cause these folds to render with the default fold preview? This doesn't happen on main, only nightly image

Are you setting vim.wo.foldtext anywhere? For neogit buffers, it should be an empty string. https://github.com/NeogitOrg/neogit/blob/abbb35a48f47ebd786ef44174b080c61e7fb7cf0/lua/neogit/lib/buffer.lua#L633

That'll leave the text as-is in the buffer, with highlights and everything. I'm going to close this, as this issue is pretty far from where we started, but if you still have issues with folds, feel free to open a new issue :)

avegancafe commented 5 months ago

@CKolkey I do not, the only fold settings I have are foldmethod=expr and foldlevelstart=99, I tried removing these though and still see the issue