Skip to content

fix: change_dir with global = false does not change the working directory - #3353

Open
joshw1013 wants to merge 1 commit into
nvim-tree:masterfrom
joshw1013:master
Open

fix: change_dir with global = false does not change the working directory#3353
joshw1013 wants to merge 1 commit into
nvim-tree:masterfrom
joshw1013:master

Conversation

@joshw1013

Copy link
Copy Markdown

Issue

With actions.change_dir.global = false, changing the directory manually through nvim-tree fails to change to the working directory for the user. This is caused by Explorer:cd, running :lcd, which is window-scoped and applies to the current window, which is nvim-tree itself. The editing window's cwd is unchanged, so :! and pickers still resolve against the old root.

Fix

My code does lcd on the target window as well, under the case that the current window is nvim-tree.

@alex-courtis

Copy link
Copy Markdown
Member

Issue

With actions.change_dir.global = false, changing the directory manually through nvim-tree fails to change to the working directory for the user. This is caused by Explorer:cd, running :lcd, which is window-scoped and applies to the current window, which is nvim-tree itself. The editing window's cwd is unchanged, so :! and pickers still resolve against the old root.

Fix

My code does lcd on the target window as well, under the case that the current window is nvim-tree.

Thank you for creating this pull request.

This is caused by Explorer:cd, running :lcd, which is window-scoped and applies to the current window, which is nvim-tree itself. The editing window's cwd is unchanged, so :! and pickers still resolve against the old root.

That appears to be working as documented for global = false. Confirming: are you talking about the nvim-tree window or another window with a file buffer?

I am clearly not understanding the nature of the problem. Please provide a detailed test case to demonstrate the problem and a fix e.g.

  • open nvim-tree in directory x
  • open file y
  • focus nvim-tree
  • <c-]> on dir z

@joshw1013

Copy link
Copy Markdown
Author

Sure I can explain in more detail. I think the best way is to just show you the video of what is going on from my config. The first one is the current nvim-tree code, and the second is the one with my fix.

You can see that in the first one:

  • I open nvim-tree
  • use <C-]> to change the directory
  • quit nvim-tree
  • I open nvim-tree back up again
  • The directory change did not stick

If you try to reproduce it and don't see that, it's possible that without these settings

sync_root_with_cwd = true
respect_buf_cwd = true

it won't reset nvim-tree's root, but you should still be able to see that :pwd gives the wrong directory.

The reason this is going on, is that when I type <C-]>, nvim-tree window is currently open, meaning the lcd gets applied to the nvim-tree window, not to the target window, which is the one that opened up nvim-tree, and the one that we care about.

Also, here's my config if it helps you at all:
https://github.com/joshw1013/nvim/blob/main/lua/josh/plugins/nvim-tree.lua

Let me know if it makes sense now / if you need anything else

current.mp4
fixed.mp4

@alex-courtis

Copy link
Copy Markdown
Member

Many thanks for the detailed reproduction.

I understand the problem (directory change does not persist after quitting tree) and the solution (lcd in the buffer window).

Unfortunately I don't think we can make this change:

  • Current expected behaviour will change, disrupting users that don't expect it
    • The cwd of the buffer window will change
    • This will break users (myself included) that use, say, lsp diagnostics expecting the project root
  • The tree is ephemeral: it is destroyed when you close it and has no "memory"

Is there any other solution you can think of to achieve your goal? I am guessing that change_dir.global is not a solution that meets your workflow.

@gegoune

gegoune commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

As a generic approach we could emit events, allowing users to do what and when they want, keeping nvim-tree itself lean.

@alex-courtis

Copy link
Copy Markdown
Member

As a generic approach we could emit events, allowing users to do what and when they want, keeping nvim-tree itself lean.

We could definitely add a TreeChangeDir event, maybe with a param scope: One of "global" or "local" as per vim.api.keyset.option

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.

3 participants