lewis6991 / gitsigns.nvim

Git integration for buffers
MIT License
5.14k stars 190 forks source link

Gitsigns not showing up when autoloading a session with neovim-session-manager #855

Closed tranzystorekk closed 1 year ago

tranzystorekk commented 1 year ago

Description

Gitsigns no longer appear in my gutter when I open a project directory nvim . and let the https://github.com/Shatur/neovim-session-manager plugin autoload a saved session.

The issue seems to have appeared after this commit: https://github.com/lewis6991/gitsigns.nvim/commit/5a2b1205841bc06ffb15719b14d54987e17b22ae

Neovim version

0.9.1

Operating system and version

Void Linux

Expected behavior

Git signs appear in the gutter when opening a directory with a git project, e.g. nvim . with a neovim-session-manager autoload config (included below).

Actual behavior

No git signs appear in the gutter of the autoloaded file

Minimal config

for name, url in pairs{
  gitsigns = 'https://github.com/lewis6991/gitsigns.nvim',
  plenary = 'https://github.com/nvim-lua/plenary.nvim',
  nvim_tree = 'https://github.com/nvim-tree/nvim-tree.lua',
  nvim_session_manager = 'https://github.com/Shatur/neovim-session-manager',
  -- ADD OTHER PLUGINS _NECESSARY_ TO REPRODUCE THE ISSUE
} do
  local install_path = vim.fn.fnamemodify('gitsigns_issue/'..name, ':p')
  if vim.fn.isdirectory(install_path) == 0 then
    vim.fn.system { 'git', 'clone', '--depth=1', url, install_path }
  end
  vim.opt.runtimepath:append(install_path)
end

require('gitsigns').setup{
  debug_mode = true, -- You must add this to enable debug messages
  -- ADD GITSIGNS CONFIG THAT IS _NECESSARY_ FOR REPRODUCING THE ISSUE
}

require('nvim-tree').setup{}

local config = require('session_manager.config')
require('session_manager').setup {
  autoload_mode = config.AutoloadMode.Disabled,
  autosave_last_session = true,
}

-- ADD INIT.LUA SETTINGS THAT IS _NECESSARY_ FOR REPRODUCING THE ISSUE
vim.wo.signcolumn = 'yes'

vim.api.nvim_create_autocmd('VimEnter', {
  callback = function ()
    local api = require 'nvim-tree.api'
    if api.tree.is_visible() then
      vim.cmd('SessionManager load_current_dir_session')
    end
  end,
})

Steps to reproduce

With the minimal config posted above:

  1. Open a git directory with nvim . and open an already committed file to let the session manager register a session
  2. Exit neovim to save the session
  3. Open the same directory again with nvim . to autoload the session
  4. nvim-session-manager autoloads the previously opened file, but git signs no longer show in gutter when changing the file.

Gitsigns debug messages

After opening a git directory with session autoload:

attach(1): Attaching (trigger=setup)
run_job: git --version
run_job: git --no-pager --literal-pathspecs -c gc.auto=0 config user.name
run_job: git --no-pager --literal-pathspecs -c gc.auto=0 rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD
function: 0x4030c8b8: Not in git repo

After opening a file (i.e. nvim <filename>) in a git repo (no session autoload):

attach(1): Attaching (trigger=setup)
run_job: git --version
run_job: git --no-pager --literal-pathspecs -c gc.auto=0 config user.name
run_job: git --no-pager --literal-pathspecs -c gc.auto=0 rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD
run_job: git --no-pager --literal-pathspecs -c gc.auto=0 --git-dir /home/devuser/repos/dotfiles/.git -c core.quotepath=off ls-files --stage --others --exclude-standard --e
ol /home/devuser/repos/dotfiles/config/nvim-pde/init.lua
function: 0x401a9cf0(1): Watching git dir
run_job: git --no-pager --literal-pathspecs -c gc.auto=0 --git-dir /home/devuser/repos/dotfiles/.git show :0:config/nvim-pde/init.lua
update(1): updates: 1, jobs: 5
run_job: git --no-pager --literal-pathspecs -c gc.auto=0 rev-parse --show-toplevel --absolute-git-dir --abbrev-ref HEAD
update(1): updates: 2, jobs: 6
attach(1): Already attached
cli.run: Running action 'debug_messages' with arguments {}
lewis6991 commented 1 year ago

Please provide repro steps without lazy.nvim. The issue template illustrated how to do this.

tranzystorekk commented 1 year ago

Sorry, missed that part, updated.

tranzystorekk commented 1 year ago

The issue seems to have appeared after this commit: 5a2b120

This guess of mine is probably incorrect, I tried to load earlier gitsigns versions without success.

lewis6991 commented 1 year ago

The repro looks fairly minimal so hopefully it won't take too long to debug (when I get around to looking at it)

lewis6991 commented 1 year ago

With the minimal config posted above:

  1. Open a git directory with nvim . and open an already committed file to let the session manager register a session
  2. Exit neovim to save the session
  3. Open the same directory again with nvim . to autoload the session
  4. nvim-session-manager autoloads the previously opened file, but git signs no longer show in gutter when changing the file.

I tried this and it appears to work fine.

image
KPatr1ck commented 1 year ago

I am facing the same problem. It did come out after this commit 5a2b1205841bc06ffb15719b14d54987e17b22ae

tranzystorekk commented 1 year ago

I noticed it doesn't reproduce in all of my projects, so confusing :/

lewis6991 commented 1 year ago

Maybe try a project you can reproduce in, and delete files until the issue goes away.

tranzystorekk commented 1 year ago

After playing around a little with the repo and non-repro projects, here's some more observations:

In a non-repro project, debug_messages contains two attach messages:

In a repro case, there's only the initial trigger=setup attach, even tho the actual file is opened as well, so it seems as if for some reason gitsigns isn't notified about the file being opened.

A noticeable difference is that the non-repro project contains thousands of subdirs and files, so it takes neovim/session manager a couple seconds to reopen the reloaded file. This could explain why gitsigns has enough time to get properly notified and attached in that case.

tranzystorekk commented 1 year ago

If anyone comes here looking for a remedy, for now the workaround is to simply manually call :Gitsigns attach after the session has been loaded

lewis6991 commented 1 year ago

In the repro case does debug messages always say attach(1): Already attached?

If so it looks like Gitsigns is trying to attach but incorrectly thinks it already has.

tranzystorekk commented 1 year ago

No, it says attach(1): Attaching (trigger setup), which is probably expected because gitsigns tries to attach to the nvim tree buffer. The problem is that neovim session manager loads a session and opens its file, but that doesn't come as an attach event.

lewis6991 commented 1 year ago

attach(1) means the attach function is running for buffer 1. If this fails then there should be a message explaining why it didn't attach later in the log.

tranzystorekk commented 1 year ago

IIUC that attach correctly fails with "new: Not a git repo" because it's the project directory itself. My point is, there should subsequently occur another attach for the file opened by the session manager, but that never happens in the repro case.

dmitrykruglov commented 1 year ago

Same problem here with neo-tree.nvim . When I launch neovim with nvim . (which opens a neo-tree buffer for me), gitsigns does not automatically attach when I open any files afterwards.

lewis6991 commented 1 year ago

Same problem here with neo-tree.nvim . When I launch neovim with nvim . (which opens a neo-tree buffer for me), gitsigns does not automatically attach when I open any files afterwards.

Repro steps please!

Saying "me too!" doesn't help progress anything.

fabivs commented 1 year ago

Having the same issue/behavior as @dmitrykruglov, here is how I can reproduce it every time:

Setup:

Steps:

tranzystorekk commented 1 year ago

@fabivs could you prepare and post a minimal reproducible config, using the template in New issue -> Bug report form? It would be invaluable for this issue.

fabivs commented 1 year ago

hi @tranzystorek-io I was about to fill the bug report like you asked, but because I did some changes in my config today I noticed that I couldn't recreate the issue anymore. Then after messing around with it for a bit I was able to figure out the piece of config that makes it work.

Originally I had only the plugin installation under lazy, so basically this:

return require('lazy').setup({

  -- other plugins...

  {
    'lewis6991/gitsigns.nvim',
    config = function()
      require('gitsigns').setup()
    end
  }

  -- other plugins...

})

And nothing else configured for Gitsigns.

Today I happened to add this code in my after/plugin/gitsigns.lua file, and with this it works fine, I can't recreate the issue anymore. Hope this information helps.

tranzystorekk commented 1 year ago

@fabivs I think you misunderstood, I meant in the bug report form there is a template for a minimal reproduction config file (doesn't use lazy, sets up a separate, clean plugin dir etc.).

fabivs commented 1 year ago

Alright, I opened the issue here, with this minimal config I am able to re-create the problem.

miguno commented 1 year ago

Have you tried disabling the statuscolumn? See https://github.com/luukvbaal/statuscol.nvim/issues/83#issuecomment-1751537535.

It did work in my case to bring back gitsigns. The issue is directly or indirectly caused, apparently, by gitsigns using extmarks now by default instead of legacy vim signs. That's as much as I could deduce, I don't have much background in this context.

lewis6991 commented 1 year ago

This issue predates extmark signs being default. Please try not to confuse multiple issues as one.

tranzystorekk commented 1 year ago

I recently switched my session management setup to be simpler and more predicatable and this issue no longer reproduces for me on any of the previously problematic repos.

Some observations on what was a probable culprit in my case:

lewis6991 commented 1 year ago

Ok, since there's no way to reproduce this and other people are commenting on unrelated issues, I'll close this. Please open a new issue with repro steps if any problems continue.