Skip to content
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

fix: arithmetic on nil value error on first git project open #3064

Merged
merged 3 commits into from
Feb 9, 2025

Conversation

bafto
Copy link
Contributor

@bafto bafto commented Feb 2, 2025

When opening neovim in a directory with a git repo for the first time after PC reboot, I always got this error message:

Fehler beim Ausführen von "BufEnter Autokommandos für "*"":
Error executing lua callback: ...-data/lazy/nvim-tree.lua/lua/nvim-tree/explorer/init.lua:395: attempt to perform arithmetic on a nil value
stack traceback:
...-data/lazy/nvim-tree.lua/lua/nvim-tree/explorer/init.lua:395: in function 'populate_children'
...-data/lazy/nvim-tree.lua/lua/nvim-tree/explorer/init.lua:419: in function 'explore'
...-data/lazy/nvim-tree.lua/lua/nvim-tree/explorer/init.lua:328: in function '_load'
...-data/lazy/nvim-tree.lua/lua/nvim-tree/explorer/init.lua:70: in function 'new'
...l/nvim-data/lazy/nvim-tree.lua/lua/nvim-tree/classic.lua:82: in function <...l/nvim-data/lazy/nvim-tree.lua/lua/nvim-tree/
classic.lua:80>
...ocal/nvim-data/lazy/nvim-tree.lua/lua/nvim-tree/core.lua:28: in function 'init'
.../nvim-tree.lua/lua/nvim-tree/actions/root/change-dir.lua:88: in function 'f' 
.../nvim-tree.lua/lua/nvim-tree/actions/root/change-dir.lua:77: in function 'force_dirchange'
...ata/Local/nvim-data/lazy/nvim-tree.lua/lua/nvim-tree.lua:113: in function <...ata/Local/nvim-data/lazy/nvim-tree.lua/lua/nvim-tree.lua:101>

This small change fixes it for me. I don't looked into the code all that deep, so maybe there is a better fix.
On a quick scim through the Issues I didn't find any matching Issue, maybe I overlooked it.

@@ -391,7 +391,7 @@ function Explorer:populate_children(handle, cwd, node, project, parent)
end
else
for reason, value in pairs(FILTER_REASON) do
if filter_reason == value then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like node.hidden_stats could still be nil, as it's an optional member.

How about something really defensive like:

diff --git a/lua/nvim-tree/explorer/init.lua b/lua/nvim-tree/explorer/init.lua
index 0fdd46a..c42ffe0 100644
--- a/lua/nvim-tree/explorer/init.lua
+++ b/lua/nvim-tree/explorer/init.lua
@@ -389,9 +389,9 @@ function Explorer:populate_children(handle, cwd, node, project, parent)
           nodes_by_path[child.absolute_path] = true
           child:update_git_status(node_ignored, project)
         end
-      else
+      elseif node.hidden_stats then
         for reason, value in pairs(FILTER_REASON) do
-          if filter_reason == value then
+          if filter_reason == value and type(node.hidden_stats[reason]) == "number" then
             node.hidden_stats[reason] = node.hidden_stats[reason] + 1
           end
         end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented it and it works for me as well.
I only have this issue on windows so this defensive version might be better to not break anything on linux.

@bafto bafto requested a review from alex-courtis February 3, 2025 09:30
Copy link
Member

@alex-courtis alex-courtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for your contribution!

I'm glad you were able to test windows.

@bafto
Copy link
Contributor Author

bafto commented Feb 9, 2025

No problem, was just a small fix for myself anyways :)
Now I can finally switch back from my fork once this is merged.

@alex-courtis
Copy link
Member

Apologies; I didn't press the merge button.

@alex-courtis alex-courtis merged commit 8052310 into nvim-tree:master Feb 9, 2025
5 checks passed
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.

2 participants