auto-session icon indicating copy to clipboard operation
auto-session copied to clipboard

[BUG] filetype doesn't get restored after change of directory

Open nnako opened this issue 3 years ago • 8 comments

After starting nvim and creating some tabs containing some buffers with different language files (all the same files will work as well), I use the command cd to walk into a specific project folder. After that, I issue the command SaveSession. This leads to creating a vim file within the default folder ... / nvim-data / sessions / ... . This file contains all the settings to restore the session.

Now, after closing vim, restarting it and cd-ing into the folder from the last session, all the buffers get restored into the appropriate tabs. BUT: the filetype is lost. issuing the command :set filetype results in an empty string.

The expected behavior would be to see the filetypes restored for all the buffers / tabs. Not to be obliged to manually re-set all the filetypes.

Baseline

  • OS. Windows 10:
  • Neovim version v0.9.0

what could this be? Is this not the expected behavior? is it maybe a configuration issue?

nnako avatar Feb 15 '23 21:02 nnako

Hi, custom filetypes are not something that gets saved in sessions in Neovim. Which is why I mentioned on the last issue that you'd have to use a hook to set a particular filetype, or even configure Neovim to always associate a particular file extension to a particular filetype of your choice.

You are setting the filetype manually, right?

rmagatti avatar Feb 17 '23 05:02 rmagatti

It is not about "custom" filetypes, here, but just the "normal", "standard" ones like python or vim which get set automatically when loading respective files into vim or nvim. The problem here seems to be that after retrieving sessions with auto-session, the filetype option stays empty. Even for "standard" filetypes.

yes, I have to set any filetype manually (or using :tabdo set filetype=...) after retrieving / restoring any session by triggering the plugin using cd <project-folder>.

I can't imagine this being the intended behavior. As I am just starting with nvim there are not many plugins which might be able to influence filetype detection.

nnako avatar Feb 17 '23 17:02 nnako

Right. You mention there are not many plugins that would influence filetype detection, auto-session is also not one of them. It just uses the underlying session creation and loading capabilities from Neovim.

Something you can try is:

  • Manually opening a file nvim file.py
  • Manually Saving a session :mksess
  • Manually loading the session file :source Session.vim

Do you still see the issue happening?

rmagatti avatar Feb 18 '23 00:02 rmagatti

I did this:

  • start nvim (via icon on desktop)
  • open Python file from anywhere in the filesystem e.g. using built-in netrw
  • create session file in current folder via :mksess
  • close nvim
  • re-open nvim by desktop icon (same current folder)
  • restore session via :source Session.vim

and the filetype is restored, so I see the Python coloring. So, there does not seem to be an issue within the built-in session management.

nnako avatar Feb 18 '23 11:02 nnako

one thing is odd, though: when I create a session file for the default nvim binary folder (in my case it is at c:\PROGS\Neovim__v0_9_0_dev\bin) with the auto-session sessions folder:

C++%PROGS%Neovim__v0_9_0_dev%bin.vim

then, when I start nvim, the session is loaded and the filetype from this session is loaded, correctly. But as soon as I use the cd command from the command line, the appropriate filetype does not get applied for files / buffers loaded into nvim.

nnako avatar Feb 18 '23 14:02 nnako

Have you set cwd_change_handling correctly in your config? What does your setup call look like?

rmagatti avatar Feb 19 '23 06:02 rmagatti

yes, I have and had (as mentioned in #205). But this should not influence the missing colors, anyway, I would think.

It is odd that restoration of colors works in the first place (when starting nvim and auto-loading the session respective to the current folder) but fails on all subsequent folder changes.

nnako avatar Feb 22 '23 06:02 nnako

    post_restore_cmds = {
      function()
        vim.api.nvim_exec2("edit", {})
      end,
    },

3rd avatar May 06 '24 13:05 3rd

i was running into this issue, and it appears to have been fixed by adding localsettings to my sessionoptions

apaleslimghost avatar Jul 10 '24 12:07 apaleslimghost

@nnako it's probably worth trying out @apaleslimghost's findings here if you haven't already

rmagatti avatar Jul 16 '24 08:07 rmagatti

I just came across this and can confirm that this solves the issue:

vim.opt.sessionoptions:append({ "localoptions" })

Thanks @apaleslimghost

rbmarliere avatar Jul 21 '24 16:07 rbmarliere

Seems like we should add a check for that and pop up a warning if localoptions isn't present. I'll look into that

cameronr avatar Jul 21 '24 17:07 cameronr