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

allowed_dirs and ignored_dirs not working #146

Closed
olimorris opened this issue Aug 9, 2024 · 6 comments
Closed

allowed_dirs and ignored_dirs not working #146

olimorris opened this issue Aug 9, 2024 · 6 comments

Comments

@olimorris
Copy link
Owner

olimorris commented Aug 9, 2024

@kohane27 - I've moved this to an issue

Thank you for informing us of the changes and decisions; and most importantly, maintaining this great plugin of yours. I really love the autoload feature.

After updating this plugin today, I have the following problem:

return {
  "olimorris/persisted.nvim",
  lazy = false,
  opts = {
    autoload = true,
    on_autoload_no_session = function()
      vim.notify("No existing session to load.")
    end,
    follow_cwd = false,

    -- BUG:
    -- ignored_dirs = {
    --   { vim.fn.hostname(), exact = true },
    --   { "/", exact = true },
    -- },
  },

  -- BUG:
  -- config = function()
  --   -- close nvim-tree before saving
  --   vim.api.nvim_create_autocmd({ "User" }, {
  --     pattern = "PersistedSavePre",
  --     group = vim.api.nvim_create_augroup("PersistedHooks", {}),
  --     callback = function()
  --       vim.cmd("NvimTreeClose")
  --     end,
  --   })
  -- end,
}

If I uncomment the -- BUG part for ignored_dirs: the autoload feature is not working until I run SessionLoad.

If I uncomment the -- BUG part for autocmd: I am met with the following error:

Error executing lua callback: ...al/share/nvim/lazy/persisted.nvim/lua/persisted/init.lua:41: attempt to index upvalue 'config' (a nil value)
stack traceback:
	...al/share/nvim/lazy/persisted.nvim/lua/persisted/init.lua:41: in function <...al/share/nvim/lazy/persisted.nvim/lua/persisted/init.lua:34>

I have read this note and have no idea why it broke. Hopefully this help. Thank you!

Originally posted by @kohane27 in #145 (comment)

@olimorris
Copy link
Owner Author

@kohane27 looks like there is a bug with the way directories are handled.

I believe the autocmd is due to using both opts and config in Lazy, as per the README. I'd advise setting up autocmds separately after the plugin loads.

@olimorris olimorris changed the title Bug in latest update allowed_dirs and ignored_dirs not working Aug 9, 2024
@olimorris
Copy link
Owner Author

@kohane27 - This should now be fixed. Thanks for flagging.

@kohane27
Copy link

Hello @olimorris , thank you for getting back to me.

return {
  "olimorris/persisted.nvim",
  lazy = false,
  opts = {
    autoload = true,
    on_autoload_no_session = function()
      vim.notify("No existing session to load.")
    end,

    follow_cwd = false,

    ignored_dirs = {
      { vim.fn.hostname(), exact = true },
      { "/", exact = true },
    },
  },
}

I updated persisted.nvim but the issue still persists with the above config: the autoload feature is not working until I run SessionLoad. Thanks again!

@olimorris
Copy link
Owner Author

Can you share a minimal.lua file from the new issue template and share with me. I can try and recreate it

@miversen33
Copy link
Contributor

Just popping in to say I am seeing the same issue.

Here is a minimal config that I have verified recreates the issue on my machine

local root = "/tmp/persisted"

-- Set stdpaths to use root dir
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- Bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

vim.opt.sessionoptions = "buffers,curdir,folds,globals,tabpages,winpos,winsize" -- Session options to store in the session

-- Install plugins
local plugins = {
  {
    "olimorris/persisted.nvim",
    opts = {
      -- Your custom config here
        use_git_branch = true,
        should_autosave = true,
        autostart = true,
        autoload = true,
        ignored_dirs = {
            "/tmp",
        }
    }
  },
  -- Put any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

Further, if you load this config and then print the contents of vim.g.persisting, you will see "nil".

So its almost like not only is persisted not loading the existing session, but its not tracking the session either.

@miversen33
Copy link
Contributor

I have a theory on what's going on here.
init.lua:200 makes the assumption that since either allowed_dirs or ignored_dirs exists, that they both must exist. This is not true.

The function being called here utils.lua#L28-L52 returns false on the above provided minimal configuration no matter where you open it from. This is because when you pass in config.allowed_dirs (an empty table), there is no way for our current directory to be in it. Thus the logic in utils promptly skips over the looping logic and "correctly" (incorrectly) returns false.

I think some better safeguards need to be put in place for situations where a user is blacklisting directories instead of whitelisting them. In its current state, I imagine that ignored_dirs will always fail.

As a test, consider adding the following to tests/dirs_spec.lua

  it("can handle only ignore directories", function()
    local cwd = "~/Code/Neovim/persisted.nvim"
    local ignored_dirs = { { "/tmp" } }
    local allowed_dirs = {}
    local allowed_match = utils.dirs_match(cwd, allowed_dirs)
    local ignored_match = utils.dirs_match(cwd, ignored_dirs)
    assert.equals(true, allowed_match and not ignored_match)
  end)

This test (currently) should fail and this test is exactly what I am seeing in my config. I assume also what @kohane27 is seeing as well.

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

No branches or pull requests

3 participants