Skip to content

CWD/root does not update after changing buffer for the first time #225

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

Closed
gegoune opened this issue Mar 29, 2022 · 18 comments
Closed

CWD/root does not update after changing buffer for the first time #225

gegoune opened this issue Mar 29, 2022 · 18 comments
Labels
bug Something isn't working

Comments

@gegoune
Copy link

gegoune commented Mar 29, 2022

I am on main now but it was the same on v2.x branch.

With require('neo-tree').setup() (no custom configuration) neo-tree's CWD (root) is not updating after switching to another repository for the first time.

Follow these steps:

  1. open nvim and some buffer in repository A
  2. open neo-tree, tree is correctly rooted at A's git root
  3. open another buffer from repository B (either using :e B/file or Telescope - effect is the same), tree remains rooted at A's root`, showing content of repository A instead of B
  4. come back to buffer from repository A
  5. come back to buffer from repository B, root is now correctly updated and showing content of repository B

Can you reproduce it?

I am using filesystem for above test but it behaves the same for buffers but does not update at all for git_status.

@cseickel
Copy link
Contributor

Neo-tree does not actually change it's root based on what file you are viewing. Step 5 in your example is the result of another plugin or setting changing your cwd.

It should react to the DirChanged event however, but only if that change applies to the tab or the neo-tree window, not if another windows local directory is changed.

@gegoune
Copy link
Author

gegoune commented Mar 29, 2022

Ah, that's true. Should have mentioned it. I use https://github.com/ahmedkhalf/project.nvim and it works with nvim-tree and does work with neo-tree as well, just not first time I change buffers for some reason. Apologies for incomplete information, completely slipped my mind.

@cseickel
Copy link
Contributor

I tried to install that project and test it out but it just doesn't work at all for me. The one time I got it to change the working directory, it went to the wrong one.

My suggestion is to confirm what the working directory actually is after entering a buffer:

:echo getcwd()

will output the global working directory. This applies if there is no tab cwd or window cwd.

:echo getcwd(-1, 0)

will output the tab's working directory. This is what is used when displayed in a sidebar. (position = "left" or position = "right")

:echo getcwd(0, 0)

will output the window's working directory. This is used when the neo-tree position = "current".

@gegoune
Copy link
Author

gegoune commented Mar 30, 2022

Just tried to run getcwd() (both without arguments and with -1, 0) and they show updated (correct) path to repository's root after loading new buffer, but neo-tree still shows old buffer's repository tree.

Did you not manage to get it to change cwd for you at all? Odd, that plugin just works out of the box for me.

Would you want me to try to come up with minimal config to replicate that issue?

@cseickel
Copy link
Contributor

Would you want me to try to come up with minimal config to replicate that issue?

Yes, that would be helpful.

Another thing you could try, if you don't mind cloning the repo and pointing your plugin manager to your local copy, is to change this line:

events.define_autocmd_event(events.VIM_DIR_CHANGED, { "DirChanged" }, 200)

to:

  events.define_autocmd_event(events.VIM_DIR_CHANGED, { "++nested DirChanged" }, 200)

@gegoune
Copy link
Author

gegoune commented Mar 30, 2022

No problem at all, will try to put something together soon.

After making that change I got that error:

[neo-tree.nvim] [ERROR 14:26:35] ...packer/start/neo-tree.nvim/lua/neo-tree/events/queue.lua:179: Error in setup for vim_dir_changed: .../packer/start/neo-tree.nvim/lua/neo-tree/events/init.lua:46: Vim(autocm
d):E216: No such group or event: ++nested DirChanged * :lua require('neo-tree.events').fire_event('vim_dir_changed', { afile = vim.fn.expand('<afile>') })

@cseickel
Copy link
Contributor

After making that change I got that error:

[neo-tree.nvim] [ERROR 14:26:35] ...packer/start/neo-tree.nvim/lua/neo-tree/events/queue.lua:179: Error in setup for vim_dir_changed: .../packer/start/neo-tree.nvim/lua/neo-tree/events/init.lua:46: Vim(autocm
d):E216: No such group or event: ++nested DirChanged * :lua require('neo-tree.events').fire_event('vim_dir_changed', { afile = vim.fn.expand('<afile>') })

Sorry, I had the syntax wrong. ++nested has to go at the end, not the beginning, which would require more code changes. I'll figure that once I can recreate the issue.

@gegoune
Copy link
Author

gegoune commented Mar 30, 2022

Unfortunately changing line 57 to

events.define_autocmd_event(events.VIM_DIR_CHANGED, { "DirChanged ++nested" }, 200)

made it never update root of the tree, as oppose to not only updating it on first change.

@cseickel cseickel added the bug Something isn't working label Mar 30, 2022
@gegoune
Copy link
Author

gegoune commented Mar 30, 2022

I tried to come up with minimal reproducible configuration but failed. I have two points:

  1. When I am changing to another buffer (step 3) cursor is in regular buffer, not in neo-tree's buffer. I have noticed that if I open another buffer (using telescope) while cursor is still in neo-tree's window it does properly update its root after opening new buffer. What could be the difference?

  2. I can make updating root work with cursor in the buffer in step 3 by disabling various formatting and diagnostic sources on null-ls. But for some reason when I add those source that seemingly breaks it for my setup to minimal config it does work there as expected. It's pretty weird.

Will keep trying to come up with solid steps to reproduce it. Just putting it up there now in case it points you to something.

@cseickel
Copy link
Contributor

cseickel commented Mar 30, 2022

The issue here is the firing, or possibly the swallowing of the DirChanged autocmd event.

My idea about added ++nested to the autocmd might be the fix, it just needs to be in the right place. What that does is allow the event to be triggered by another event. Normally that's turned off to prevent infinite loops, but in this case it may be needed if the directory is being changed as part of a handler for another event. I'll add this in and see if that helps.

I'm not sure how null-ls can factor into this, but it's an interesting clue. Probably still related to how autocmd events are affected by what it does.

@cseickel
Copy link
Contributor

I just pushed a change to the main branch that will enabled nested events for the DirChanged autocmd. This may fix your issue, can you give it a try?

@gegoune
Copy link
Author

gegoune commented Mar 31, 2022

Thanks for looking into it, but unfortunately it did not help, root still doesn't change on initial change of buffers.

@cseickel
Copy link
Contributor

Ignore that close event, it happened automatically when I released because I mentioned the issue number in the commit. I'm re-opening now.

@cseickel cseickel reopened this Mar 31, 2022
@cseickel
Copy link
Contributor

  1. When I am changing to another buffer (step 3) cursor is in regular buffer, not in neo-tree's buffer. I have noticed that if I open another buffer (using telescope) while cursor is still in neo-tree's window it does properly update its root after opening new buffer. What could be the difference?

I know what this is. In that situation, I think Telescope will open the buffer in the Neo-tree window. When that happens, neo-tree will recognize that its window was stolen, show that buffer in a different window, and set neo-tree as the active buffer again. There are some vim.schedule calls in that process which has two effects:

  • It repeats the BufEnter/BufWinEnter events again in that new window
  • That second firing happens in a new event, not a continuation of the first triggering event
  • It defers that second event by some small amount of time

One or more of those is probably critical to fixing the problem, I just don't know how just yet!

@lopi-py
Copy link
Contributor

lopi-py commented Apr 9, 2022

  • It repeats the BufEnter/BufWinEnter events again in that new window

:h 'eventignore' may help by setting it before and after triggering the events

@cseickel
Copy link
Contributor

cseickel commented May 1, 2022

@gegoune A lot has changed since this was initially reported. Is this still happening?

@gegoune
Copy link
Author

gegoune commented May 1, 2022

Did not check that very use case in a while and indeed it seems to be fixed now. Gonna close. Thanks!

@gegoune gegoune closed this as completed May 1, 2022
@ihaohe
Copy link

ihaohe commented Jul 18, 2023

Did not check that very use case in a while and indeed it seems to be fixed now. Gonna close. Thanks!

I still have the problem. Could you give me some suggestions about how to config?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants