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

Error E303: unable to open swap file if 'directory' has wrong permissions #20749

Open
Nales0 opened this issue Oct 20, 2022 · 10 comments
Open

Error E303: unable to open swap file if 'directory' has wrong permissions #20749

Nales0 opened this issue Oct 20, 2022 · 10 comments
Labels
options configuration, settings startup Nvim startup sequence (`:h startup`)
Milestone

Comments

@Nales0
Copy link

Nales0 commented Oct 20, 2022

old info

Neovim version (nvim -v)

NVIM v0.8.0-v0.8.0 Build type: RelWithDebInfo

Vim (not Nvim) behaves the same?

No

Operating system/version

Linux Mint 21 Vanessa 64-bit

Terminal name/version

mate-terminal

$TERM environment variable

xterm-256color

Installation

From the debian package in the Releases page and from build

How to reproduce the issue

When I open a file (for example a.txt), nvim displays this error at start (see :help E303):

E303: Unable to open swap file for "a.txt", recovery impossible

Update: see #20749 (comment)

old info

To check the reason of this issue, I used this command:

nvim --clean -u NONE -Vmessages.txt a.txt

I will not paste the whole content of messages.txt but here are some suspicious lines (sometimes translated by myself here because I did not manage to launch nvim in English):

chdir(/usr/local/share/nvim/runtime)

not found in runtime path: "ftdetect/*.vim"
not found in runtime path: "ftdetect/*.lua"


chdir(/usr/local/share/nvim/runtime/plugin)

end of sourcing of /usr/local/share/nvim/runtime/plugin/man.lua
not found in 'packpath' : "pack/*/start/*"
not found in 'packpath' : "start/*"
not found in runtime path: "plugin/**/*.vim"
not found in runtime path: "plugin/**/*.lua"
                                            E303: Unable to open swap file for "a.txt", recovery impossible

not found in runtime path: "indent/text.vim"
not found in runtime path: "indent/text.lua"

sourcing "/usr/local/share/nvim/runtime/plugin/matchit.vim"
not found in 'packpath' : "pack/*/start/matchit"

Here are where nvim configuration is installed:

$ sudo find /usr /etc /var -name nvim
/usr/local/share/nvim
/usr/local/lib/nvim
/usr/local/bin/nvim

Let's see for example matchit.vim. Here is where it can be found:

$ sudo find /usr/local/share/nvim/runtime -name matchit.vim 
/usr/local/share/nvim/runtime/pack/dist/opt/matchit/autoload/matchit.vim
/usr/local/share/nvim/runtime/pack/dist/opt/matchit/plugin/matchit.vim
/usr/local/share/nvim/runtime/macros/matchit.vim
/usr/local/share/nvim/runtime/plugin/matchit.vim

I have no clues why this E303 error happens nor why some runtime files cannot be found. Some runtime files are found, for a proof I wrote this issue in a Mardown file and the filetype is correctly detected.

This issue happens if I try to open a new file or an already existing file.

Expected behavior

Not displaying a E303 error message.

Actual behavior

See above.

@Nales0 Nales0 added the bug issues reporting wrong behavior label Oct 20, 2022
@justinmk

This comment was marked as outdated.

@justinmk justinmk added the closed:question issues that are closed as usage questions label Oct 24, 2022
@Nales0

This comment was marked as outdated.

@zeertzjq zeertzjq reopened this Oct 26, 2022
@zeertzjq zeertzjq added startup Nvim startup sequence (`:h startup`) and removed closed:question issues that are closed as usage questions labels Oct 26, 2022
@zeertzjq
Copy link
Member

zeertzjq commented Oct 26, 2022

I just observed this in oldtests in TSan CI job:

Failures: 
	From test_cd.vim:
	Found errors in Test_cd_unknown_dir():
	Caught exception in Test_cd_unknown_dir(): Vim(edit):E303: Unable to open swap file for "Xa/Xb.txt", recovery impossible @ command line..script /home/runner/work/neovim/neovim/src/nvim/testdir/runtest.vim[433]..function RunTheTest[40]..Test_cd_unknown_dir, line 4
	From test_cmdline.vim:
	Found errors in Test_cmdline_expand_cur_alt_file():
	Caught exception in Test_cmdline_expand_cur_alt_file(): Vim(file):E303: Unable to open swap file for "http://some.com/file.txt", recovery impossible @ command line..script /home/runner/work/neovim/neovim/src/nvim/testdir/runtest.vim[433]..function RunTheTest[40]..Test_cmdline_expand_cur_alt_file, line 2
	From test_gf.vim:
	Found errors in Test_gf_url():
	Caught exception in Test_gf_url(): Vim(wincmd):E303: Unable to open swap file for "demo://remote_file", recovery impossible @ command line..script /home/runner/work/neovim/neovim/src/nvim/testdir/runtest.vim[433]..function RunTheTest[40]..Test_gf_url, line 35
	From test_mksession.vim:
	Found errors in Test_mksession_slash():
	Caught exception in Test_mksession_slash(): Vim(edit):E303: Unable to open swap file for "a/b/c", recovery impossible @ command line..script /home/runner/work/neovim/neovim/src/nvim/testdir/runtest.vim[433]..function RunTheTest[40]..Test_mksession_slash[12]..script /home/runner/work/neovim/neovim/src/nvim/testdir/Xtest_mks1.out, line 56
	From test_alot.vim:
	Found errors in Test_with_directories():
	Caught exception in Test_with_directories(): Vim(next):E303: Unable to open swap file for "Xdir?/*/nofile", recovery impossible @ command line..script /home/runner/work/neovim/neovim/src/nvim/testdir/runtest.vim[433]..function RunTheTest[40]..Test_with_directories, line 17

When I made these changes to the test suite:

diff --git a/src/nvim/testdir/Makefile b/src/nvim/testdir/Makefile
index a6d1cf100..1157fa581 100644
--- a/src/nvim/testdir/Makefile
+++ b/src/nvim/testdir/Makefile
@@ -179,4 +179,4 @@ newtestssilent: $(NEW_TESTS_RES)
 	@echo "[OLDTEST] Running" $*
 	@rm -rf $*.failed test.ok $(RM_ON_RUN)
 	@mkdir -p $(TMPDIR)
-	@/bin/sh runnvim.sh $(ROOT) $(NVIM_PRG) $* $(RUN_VIMTEST) $(NO_INITS) -u NONE --cmd "set shortmess-=F" -S runtest.vim $*.vim
+	@/bin/sh runnvim.sh $(ROOT) $(NVIM_PRG) $* $(RUN_VIMTEST) $(NO_INITS) -u NONE --cmd "source setup.vim" -S runtest.vim $*.vim
diff --git a/src/nvim/testdir/setup.vim b/src/nvim/testdir/setup.vim
index f89528746..fe949ab5d 100644
--- a/src/nvim/testdir/setup.vim
+++ b/src/nvim/testdir/setup.vim
@@ -1,4 +1,6 @@
-if exists('s:did_load')
+let pre_cmd = expand('<stack>') =~# '^pre-vimrc command line'
+
+if pre_cmd || exists('s:did_load')
   " Align Nvim defaults to Vim.
   set backspace=
   set complete=.,w,b,u,t,i
@@ -31,6 +33,10 @@ if exists('s:did_load')
   nnoremap Q gQ
 endif
 
+if pre_cmd
+  finish
+endif
+
 " Common preparations for running tests.
 
 " Only load this once.

https://github.com/neovim/neovim/actions/runs/3330361510/jobs/5508748000

The error doesn't appear in other CI jobs. Maybe there is a data race?

@zeertzjq zeertzjq changed the title Error E303 at start and missing runtime files with v0.8.0 Error E303 at start with v0.8.0 Oct 27, 2022
@zeertzjq zeertzjq changed the title Error E303 at start with v0.8.0 Error E303: unable to open swap file at start with v0.8.0 Oct 27, 2022
@zeertzjq
Copy link
Member

@Nales Do you have XDG_STATE_HOME environment variable set? If not, does ~/.local/state/nvim/swap/ directory exist?

@Nales0
Copy link
Author

Nales0 commented Oct 28, 2022

Thank you @zeetzjq for investigating and reopening the issue.

XDG_STATE_HOME is not set and ~/.local/state/nvim/swap exists. I checked this directory and its owner was root!

I changed the owner of ~/.local/state/nvim/swap to my local user and now everything works fine. When I open a file, a swap file is created in this directory.

So I checked multiple scenarios and here is the full issue. When there is no ~/.local/state/nvim/swap directory, nvim creates one. The owner of this new directory is the user who executed nvim. If it were the local user, the owner would be the local user; if it were rooŧ (by doing a sudo nvim), the owner is root.

I suppose I immediately used sudo nvim instead of nvim right after installing neovim.

Do other directories are created this way? This could be a very insidious issue for other users who do the same thing as I did.

Should we find a way to prevent this issue? I do not know if this is a good idea to force the creation of this directory as the local user instead of root, and I do not know if this is feasible.

@zeertzjq
Copy link
Member

zeertzjq commented Oct 28, 2022

It's strange that the directory is not created under /root in this case.

@Nales0
Copy link
Author

Nales0 commented Oct 28, 2022

Actually I use sudo as an alias for sudo -E. Here is the relevant manual part.

     -E, --preserve-env
                 Indicates to the security policy that the user wishes to pre‐
                 serve their existing environment variables.  The security
                 policy may return an error if the user does not have permis‐
                 sion to preserve the environment.

I checked using sudo without the -E option: doing so does not create ~/.local/state/nvim/swap, and it creates /root/.local/state/nvim/swap/. I suppose the issue here is only my fault.

I let you close the issue if you think it is a normal behaviour. I think we should find a way to warn the user that he or she has no access to the swap directory. At least it could lead the user to fix the issue himself or herself.

Anyway, thank you all for the time and the help here!

@zeertzjq
Copy link
Member

Maybe :checkhealth can check if there are writeable swap directories.

@zeertzjq zeertzjq removed bug issues reporting wrong behavior startup Nvim startup sequence (`:h startup`) labels Oct 30, 2022
@zeertzjq zeertzjq added the startup Nvim startup sequence (`:h startup`) label Mar 20, 2023
@justinmk
Copy link
Member

justinmk commented Feb 20, 2024

Status

Improve checkhealth

Maybe :checkhealth can check if there are writeable swap directories.

👍 Looks like Nvim doesn't provide an easy way to check if directory contents are readable. The closest I can find is from :help getfperm():

If {fname} does not exist or its directory cannot be read, an empty string is returned.

So this trick can be used (passing /. as the "file"):

let is_dir_readable = getfperm(dir .. '/.')

'directory' bug?

:help 'directory' says:

The swap file will be created in the first directory where this is possible. If it is not possible in any directory, but last directory listed in the option does not exist, it is created.

This seems to fail if any of the directories the 'directory' list are not accessible. Repro steps:

mkdir x
chmod a-rwx x
nvim --clean +'set directory=x,xx'
:edit foo
E303: Unable to open swap file for "foo", recovery impossible

@justinmk justinmk added the complexity:low Low-risk, self-contained. Do NOT ask "can I work on this", just read CONTRIBUTING.md label Feb 20, 2024
@justinmk justinmk added this to the backlog milestone Feb 20, 2024
@justinmk justinmk added options configuration, settings and removed complexity:low Low-risk, self-contained. Do NOT ask "can I work on this", just read CONTRIBUTING.md labels Feb 20, 2024
@neovim neovim deleted a comment from majorzpley Feb 20, 2024
@justinmk justinmk changed the title Error E303: unable to open swap file at start with v0.8.0 Error E303: unable to open swap file if 'directory' has wrong permissions Feb 20, 2024
@vaisakh678

This comment was marked as resolved.

@neovim neovim locked as off-topic and limited conversation to collaborators May 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
options configuration, settings startup Nvim startup sequence (`:h startup`)
Projects
None yet
Development

No branches or pull requests

4 participants