Skip to content

fix: add nil check to get_cwd #2557

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

Merged
merged 2 commits into from
Nov 25, 2023
Merged

fix: add nil check to get_cwd #2557

merged 2 commits into from
Nov 25, 2023

Conversation

kezhenxu94
Copy link
Contributor

In a recent update, I found the restore of rmagatti/auto-session doesn't work, and from my debugging, it looks like the function get_cwd needs a nil check.

When get_cwd is used in the autocmd (BufEnter), the core is not initialized and thus TreeExplorer is nil and thus TreeExplorer.absolute_path throws exception, this PR adds a nil check to the TreeExplorer

if vim.fn.getcwd() ~= core.get_cwd() or (opts.reload_on_bufenter and not opts.filesystem_watchers.enable) then

Reproducible Configs

I'm using LazyVIM

~/.config/nvim/lua/custom/plugins/nvim-tree.lua

return {
  "nvim-tree/nvim-tree.lua",
  lazy = false,
  dependencies = {
    "nvim-tree/nvim-web-devicons",
  },
  config = function()
    require("nvim-tree").setup {}

    -- restore nvim-tree with auto-session
    local api = require "nvim-tree.api"
    local view = require "nvim-tree.view"
    vim.api.nvim_create_autocmd("BufEnter", {
      pattern = "NvimTree*",
      callback = function()
        if not view.is_visible() then
          api.tree.open()
        end
      end,
    })
  end,
}

~/.config/nvim/lua/custom/plugins/auto-session.lua

return {
  'rmagatti/auto-session',
  config = function()
    require("auto-session").setup {
      log_level = "error",
      auto_session_suppress_dirs = { "~/", "~/Projects", "~/Downloads", "/", "/tmp" },
    }
  end
}

Then open neovim, open a file and open the NvimTree, then :SessionSave, and exit neovim. Next time you open neovim a exception will be thrown.

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.

Great catch! Many thanks for your contribution.

It appears that most callers are nil safe. If we find any that aren't we can resolve them.

@alex-courtis alex-courtis merged commit 5e4475d into nvim-tree:master Nov 25, 2023
@kezhenxu94 kezhenxu94 deleted the nil branch November 26, 2023 00:45
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