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

slint-viewer --auto-reload does not work well with Neovim (and potentially other editors) #3641

Closed
necabo opened this issue Oct 10, 2023 · 4 comments · Fixed by #5094
Closed
Labels
a:tooling Tools (mT,bO) bug Something isn't working good first issue Good for newcomers priority:low Lowest priority. The issue is kept open for tracking purpose, but noone is actively working on this

Comments

@necabo
Copy link

necabo commented Oct 10, 2023

Neovim on Linux and potentially other editors may have surprising behavior when saving files.
notify, the library used to watch files in slint-viewer, mentions this behavior here and links to related issues.

I suspect this to be the reason that slint-viewer --auto-reload <file> fails to reload on save after a few saves (sometimes it works once, sometimes a few more times).
I tried some of the suggested fixes in the referenced issues such as setting backupcopy=yes in Neovim and can confirm that this changes which events notify reports, fixing the issue.
It's just unfortunate that the default Neovim behavior breaks auto-reloading and the issue isn't easy to discover.

One suggested fix applications could employ is watching the parent directory instead.
I don't know if that approach would work for slint-viewer or how relevant this issue is. Maybe documenting this somewhere is enough.
Personally, I'll just use the LSP preview which works very well in Neovim.

@hunger hunger added the a:tooling Tools (mT,bO) label Oct 10, 2023
@hunger
Copy link
Member

hunger commented Oct 10, 2023

I always use the preview build into the language server: That works great with neovim.

I'll need to take a look at the slint-viewer...

LeonMatthesKDAB added a commit to LeonMatthes/Dotfiles that referenced this issue Dec 13, 2023
@codecat
Copy link

codecat commented Dec 24, 2023

With Sublime Text, on some saves it would successfully reload, and on other saves it would tell me "No component found".

Successful reload of ui/main_window.slint
error: No component found

@ogoffart
Copy link
Member

ogoffart commented Jan 2, 2024

Accroding to LeonMatthes/Dotfiles@d7ff39f#diff-aacb5e1be9b006044db0bfd005e040928e33b550a9d44e60d80b7e1e0855b638R37-R39 , it seems that neovim will first move the old file to a backup file, then re-create a new file with the given name. And this doesn't work well with the way the slint-viewer uses the file system watcher.

@ogoffart ogoffart added bug Something isn't working priority:low Lowest priority. The issue is kept open for tracking purpose, but noone is actively working on this labels Jan 2, 2024
@ogoffart
Copy link
Member

I was told We should also watch the parent directory and reload the view if a file that matches the file we're watching is added (and watch that)

The code that do the watcher is around there

if let Some(watcher) = fswatcher {

@ogoffart ogoffart added the good first issue Good for newcomers label Apr 17, 2024
0x6e added a commit to 0x6e/slint that referenced this issue Apr 18, 2024
Some editors may backup a file by renaming it to append ~, then saving
the new file contents to a new file. notify-rs/notify#166 explains
that a non-recursive watcher on the parent directory is the
recommended way to deal with this situation.

Closes: slint-ui#3641
0x6e added a commit to 0x6e/slint that referenced this issue Apr 19, 2024
Some editors, such as vim, rename (move) a file to a backup location,
then write the new contents to a new location when the user saves
their changes. notify stops watching the renamed file, and does not
automatically start watching the new file created. Additionally,
slint-viewer attempts to reload before the editor has written the new
file, which causes an error. The file is then never reloaded because
the watcher was lost.

This patch solves the problem by attempting to watch the file again,
if the previous watch failed due to a Generic or PathNotFound error.
Generic is required because this is error type we get on macOS for "No
such file or directory.". We delay the retry by a small timeout to
give the editor a chance to write the new file. Note that this still
results in an error being printed about the missing file.

Tested manually by editing both root .slint file, and .slint files
imported from sub-directories.

Closes: slint-ui#3641
ogoffart pushed a commit that referenced this issue Apr 22, 2024
Some editors, such as vim, rename (move) a file to a backup location,
then write the new contents to a new location when the user saves
their changes. notify stops watching the renamed file, and does not
automatically start watching the new file created. Additionally,
slint-viewer attempts to reload before the editor has written the new
file, which causes an error. The file is then never reloaded because
the watcher was lost.

This patch solves the problem by attempting to watch the file again,
if the previous watch failed due to a Generic or PathNotFound error.
Generic is required because this is error type we get on macOS for "No
such file or directory.". We delay the retry by a small timeout to
give the editor a chance to write the new file. Note that this still
results in an error being printed about the missing file.

Tested manually by editing both root .slint file, and .slint files
imported from sub-directories.

Closes: #3641
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:tooling Tools (mT,bO) bug Something isn't working good first issue Good for newcomers priority:low Lowest priority. The issue is kept open for tracking purpose, but noone is actively working on this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants