Harpoon 2 persistency doesn't work when using the auto-session plugin.
Description: When adding files to harpoon and then closing and reopening nvim, all harpoon data is deleted. When disabling the autosession plugin, harpoon works normally.
Have same issue
I'm using auto-session, but I find my harpoon marks are being lost every time I exit Neovim whether auto-session is enabled/loaded or not. As much as I like this plugin, I just can't seem to get it working the way I expect.
What is auto session? Does this change your current working directory?
What is auto session? Does this change your current working directory?
Sorry for the unclarity, when I said 'auto-session' I was referring to the rmagatti/auto-session plugin. It doesn't change the working directory, but the natecraddock/workspaces.nvim plugin I'm using in conjunction with auto-session does. They make a surprisingly effective one-two punch for picking a "workspace" from a Telescope-powered popup (that's what the workspace plugin does) and then loading any session associated with the working directory of said workspace (that's what the auto-session plugin does). The relevant portion of my Lazy setup for the two is:
{
"natecraddock/workspaces.nvim",
opts = {
hooks = {
open = { "SessionRestore" }, -- Calls the auto-session plugin.
}
},
},
{
"rmagatti/auto-session",
lazy = false,
enabled = true,
opts = {
auto_restore = false,
auto_save = false,
suppressed_dirs = { '~/', '~/Dropbox', '~/Downloads', '/' },
}
},
If there's any way I can tweak harpoon to get it working, I'd surely appreciate it. Thanks for weighing in on the topic!
Is anyone still having this issue, it appears to be working for me.
Issue #551 is around saving with git worktrees, I wonder if that is where this comes from. I don't appear to have an issue in my current repo but can check a worktree repo at work tomorrow.
Is anyone still having this issue, it appears to be working for me.
Its working correctly at the last commit on harpoon2 for you?
I have still the same problem.
Last working commit is e76cb03c420bb74a5900a5b3e1dde776156af45f for me.
I am working on a repo that has work trees and it seems to be fine, this is my lazy lock;
"harpoon": { "branch": "harpoon2", "commit": "ed1f853847ffd04b2b61c314865665e1dadf22c7" },
"auto-session": { "branch": "main", "commit": "317412742990371f8e4709074da5c378456a27ff" },
As for conf I don't think I have anything especial
auso-session
-- selene: allow(mixed_table)
--- @type LazyPluginSpec
return {
"rmagatti/auto-session",
dependencies = "nvim-telescope/telescope.nvim",
lazy = false,
-- TODO: the bellow is taken from an example on https://github.com/rmagatti/auto-session more investigation is required
---enables autocomplete for opts
---@module "auto-session"
---@type AutoSession.Config
opts = {
session_lens = {
suppressed_dirs = { "~/", "~/Downloads", "/" },
auto_restore_last_session = true,
auto_save = true,
bypass_save_filetypes = { "alpha" },
-- telematry config
-- opens up the telematry dialog on setup
load_on_setup = true,
previewer = false,
mappings = {
-- Mode can be a string or a table, e.g. {"i", "n"} for both insert and normal mode
delete_session = { "i", "<C-D>" },
alternate_session = { "i", "<C-S>" },
copy_session = { "i", "<C-Y>" },
},
theme_conf = {
border = true,
-- layout_config = {
-- width = 0.8, -- Can set width and height as percent of window
-- height = 0.5,
-- },
},
},
},
}
Harpoon
-- selene: allow(mixed_table)
--- @type LazyPluginSpec
return {
"ThePrimeagen/harpoon",
branch = "harpoon2",
dependencies = { "nvim-lua/plenary.nvim" },
config = function()
local harpoon = require("harpoon")
harpoon:setup({
settings = {
save_on_toggle = true,
sync_on_ui_close = true,
},
})
harpoon:extend({
UI_CREATE = function(cx)
local wk = require("which-key")
wk.add({
<just keymapys>
}, { mode = "n", buffer = cx.bufnr })
end,
})
local wk = require("which-key")
wk.add({
{ "<leader>h", group = "[H]arpoon" },
<just keymapys>
}, { mode = "n" })
end,
}
I'll see how it goes for the rest of the day and update if I find anything.
i am having this issue as well i am using latest harpoon2 and auto-session
"auto-session": { "branch": "main", "commit": "00334ee24b9a05001ad50221c8daffbeedaa0842" },
"harpoon": { "branch": "harpoon2", "commit": "ed1f853847ffd04b2b61c314865665e1dadf22c7" },
any help on this?
i am having this issue as well i am using latest harpoon2 and auto-session
"auto-session": { "branch": "main", "commit": "00334ee24b9a05001ad50221c8daffbeedaa0842" }, "harpoon": { "branch": "harpoon2", "commit": "ed1f853847ffd04b2b61c314865665e1dadf22c7" }, any help on this?
Sorry to say I never was able to get it working as expected. If I can find the time, I'll give it another try. In the meantime, I've been using the LintaoAmons/bookmarks.nvim plugin instead. I'm not fond of its dependency on SQLite, but it does at least do the job I thought Harpoon would do.
I dug into this and it's a bit of a rat's nest. Here's my understanding of what's happening:
- Harpoon doesn't track directory changes (e.g.
DirChangedPre,DirChangedautocmds) - By default, Harpoon stores it's data in a json file that's named for the hash of the cwd (at the time that the data writing function happens to be called).
- Harpoon ultimately, stores it's data in memory in the require('harpoon').data._data. You can see what's in memory by calling
:=require('harpoon'):dump()(despite the code saying you'll be fired if you call it :)) - The data stored in memory can actually be for multiple directories even though the hashed filename is for a single directory. For example, in my testing, this is the data for ~/tmp/a:
filename: 6253df416fe20173a1e31afefb5bb88490e9ed31c9f6ea1d4fe46fa798dd2499.json (echo -n /Users/cam/tmp/a | sha256)
{
"/Users/cam/tmp/a":{
"__harpoon_files":[
"{\"context\":{\"row\":1,\"col\":3},\"value\":\"a.txt\"}",
"{\"context\":{\"row\":1,\"col\":1},\"value\":\"a2.txt\"}"
]
},
"/Users/cam/tmp":{
"__harpoon_files":[
]
}
}
- The Harpoon Data:sync function doesn't replace data._data with data read from disk, instead, while it does read data from disk, it replaces any of those keys with what's in data_data and then writes that back to the disk
- Harpoon has an autocmd for
BufLeavethat can cause data to be synced, but only if it finds an existing harpooned item and, even then, any data read from disk won't replace what's in memory
As for how to fix, Harpoon could be updated to respond to directory change autocmds and clear data._data when cwd changes. This is more personal opinion, but I also think it would be cleaner if data written to the hash filename for cwd only contained data for cwd (instead of potentially including other directories).
If you want auto-session to force Harpoon to re-read it's data when a session is restored, you can add the following to your auto-session config:
pre_restore_cmds = {
-- might not be necessary, but save current harpoon data when we're about to restore a session
function() require('harpoon'):sync() end,
},
post_restore_cmds = {
function()
-- vim.notify('calling harpoon sync after restore')
local harpoon = require('harpoon')
-- this is the only way i found to force harpoon to reread data from the disk rather
-- than using what's in memory
harpoon.data = require('harpoon.data').Data:new(harpoon.config)
end,
},
I dug into this and it's a bit of a rat's nest. Here's my understanding of what's happening:
- Harpoon doesn't track directory changes (e.g. ’DirChangedPre
,DirChanged` autocmds)
Snip! First, thanks for that. I'm a little bit surprised that you say that because it seemed to me like Harpoon tracked some of my directory changes but not those made via auto-session. That's part of the reason I was surprised I couldn't get it working.
As for how to fix, Harpoon could be updated to respond to directory change autocmds and clear data._data when cwd changes. This is more personal opinion, but I also think it would be cleaner if data written to the hash filename for cwd only contained data for cwd (instead of potentially including other directories).
If you want auto-session to force Harpoon to re-read it's data when a session is restored, you can add the following to your auto-session config:
Snip! And thanks for that too! I'm still hoping to find time to come back to it and try again. When I do, I'll see if that fixes my problems.
@dc-jbw I noticed that too, that in some of my projects, it seemed like Harpoon did seem to follow session loading. I didn't fully investigate it but my two thoughts are either lazy loading delaying when harpoon reads it's data or the data being saved in several different cwd files. In that second case, it didn't really follow the cwd change but if things were loaded the same way, it'd be able to find the data consistently even if it might loading it from the wrong cwd file
I got a solution that worked for me here https://github.com/rmagatti/auto-session/issues/433
Hey, thanks @cameronr ! I tried what you posted and found it fixed my issue too!