Skip to content

Disable backup (.bck) files and modify files directly #11714

Description

@faresbakhit

Suggestion

Can an option be added to disable backup files and modify the files directly when saving changes with the write:* family of commands?

Current Behavior

The current behavior can be tested by invoking inotifywait -m . from inotify-tools, opening a file, modifying the file, and saving the change with :w.

Output from inotifywait on my machine using helix 24.7 (079f5442):

./ OPEN example.txt
./ ACCESS example.txt
./ CLOSE_NOWRITE,CLOSE example.txt
./ MOVED_FROM example.txt
./ MOVED_TO example.txtU77clA.bck
./ CREATE example.txt
./ OPEN example.txt
./ MODIFY example.txt
./ CLOSE_WRITE,CLOSE example.txt
./ ATTRIB example.txt
./ DELETE example.txtU77clA.bck

tl;dr: Helix reads example.txt contents into memory, and upon saving it moves (not copy) example.txt to example.txtXXXXXX.bck, creates example.txt, saves the new changes to example.txt, and removes example.txtXXXXXX.bck.

I'm not sure why Helix need to do this and the code doesn't help me either:

let backup = if path.exists() {
let path_ = write_path.clone();
// hacks: we use tempfile to handle the complex task of creating
// non clobbered temporary path for us we don't want
// the whole automatically delete path on drop thing
// since the path doesn't exist yet, we just want
// the path
tokio::task::spawn_blocking(move || -> Option<PathBuf> {
let mut builder = tempfile::Builder::new();
builder.prefix(path_.file_name()?).suffix(".bck");
let backup_path = if is_hardlink {
builder
.make_in(path_.parent()?, |backup| std::fs::copy(&path_, backup))
.ok()?
.into_temp_path()
} else {
builder
.make_in(path_.parent()?, |backup| std::fs::rename(&path_, backup))
.ok()?
.into_temp_path()
};
backup_path.keep().ok()
})
.await
.ok()
.flatten()
} else {
None
};

Use Case

Many tools that monitor a set of files/directories for changes and does something when files are created/changed/removed and don't produce the intended behavior since they get multiple events (CREATE -> CREATE -> REMOVE) instead of one MODIFY event.

Such tools include latexmk, hugo, and other static site generators or 'live' coding environments (although, I only use latexmk and hugo)

This is output from hugo server --disableFastRender:

Template added /partials/head.html5iipqu.bck
Template added /partials/head.html

And as a result it doesn't update the generated files properly. With the new option the output should be:

Template changed /partials/head.html

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions