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

cwd session can get overwritten #72

Closed
josh-nz opened this issue Jul 9, 2024 · 3 comments
Closed

cwd session can get overwritten #72

josh-nz opened this issue Jul 9, 2024 · 3 comments

Comments

@josh-nz
Copy link
Contributor

josh-nz commented Jul 9, 2024

This is essentially a variant of #67.

I have autoload set to cwd, autosave.cwd = true, and autosave.on_quit = true. This works great for the typical use case for me: I load nvim, open some buffers, quit, come back, and the session is restored for that cwd.

We implemented the feature that when a file is passed to nvim, it prevents autoload. This is good, but autosave then saves this single file on quit, overwriting my cwd autosave session. While #67 was more an issue due to dev bugs, this is a real world issue and really annoying.

Probably want to have more of a think how to handle this. I'm sure it's going to affect others.

Repro:

  1. Have the three config values set as above
  2. Open nvim, no args.
  3. Open some buffers.
  4. Quit -> buffers should autosave.
  5. Open nvim, no args -> buffers should autoload. (This and next step are just for verification)
  6. Quit.
  7. Open nvim with a file argument.
  8. Quit.
  9. Open nvim.

Actual: the file argument file is restored as the autoloaded session.
Expected: the original buffers from step 3.

I would expect that if autoload is disabled, then autosave is also disabled (with caveats, see below).

Some quick notes on things to consider:
User might not have not autoload enabled
User might opt in to a session, so autosave might need to be enabled at some point even if it was disabled due to a file arg being passed to nvim.

@josh-nz josh-nz changed the title cwd autosave can get overwritten cwd session can get overwritten Jul 9, 2024
@josh-nz
Copy link
Contributor Author

josh-nz commented Jul 12, 2024

Another situation that can occur:

Repro:

  1. Have the three config values set as per original post.
  2. Open nvim, no args.
  3. Open some buffers.
  4. Quit -> buffers should autosave as cwd.
  5. Open nvim, no args -> buffers should autoload.
  6. Save session as foo.
  7. Close session foo. Buffers may or may not delete depending on config.
  8. If buffers do not close, adjust buffers so they are different than what foo has.
  9. Quit
  10. Open nvim.

Actual: Buffers are as per step 8.
Expected: Buffers should be as per step 4.

There is a variant of this, where autoload cwd is not enabled, but a user manually loads a session, then closes it.

How the cwd session currently behaves is like a scoped tmp session. It should be considered temporary as it is easy to overwrite accidentally. But it doesn't have tmp in the name so I feel it's a bit misleading. I'd really prefer it to be less "temp" and more like an explicitly named session.

I've also logged #73 which is probably the first thing to address.

@josh-nz
Copy link
Contributor Author

josh-nz commented Jul 12, 2024

While looking at the code thinking how I might solve this, the following occurred to me:

  • autosave is only checked/called during session load and Neovim exit.
  • if there is a current session, and current session autosave is on, then autosave on quit.
  • autoload, or manually loading the cwd session will set the current session. *(continued below)
  • therefore, the autosave.cwd config is only invoked when current session is nil. This will be the case when autoload is prevented due to file args passed to Neovim, or a session is manually closed. Both of these cases are the issues logged in my posts above.
  • I can prevent saving the cwd session in these cases by adjusting my autosave.cwd function to look like the following:
      autosave = {
        cwd = function()
              local s = require("possession.session")
              local p = require("possession.paths")
              return not s.exists(p.cwd_session_name())
        end,
    }

This looks to satisfy my needs as outlined in this issue.

* (continued from above). I'm unsure if setting the current session value during autoload is something you expected to happen. It gets set in session.load and was not something I considered while implementing autoload. If this behaviour is undesirable for some reason, we can reconsider what to do.

@josh-nz josh-nz closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2024
@jedrzejboczar
Copy link
Owner

Thanks, it's probably worth adding a note in the readme.

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

2 participants