dialoguer icon indicating copy to clipboard operation
dialoguer copied to clipboard

Notepad.exe prompts to save in a different location from temp file

Open indygreg opened this issue 2 years ago • 1 comments

Thank you for this terrific crate!

When attempting to use the editor prompt on Windows 11, notepad.exe is successfully opened with the contents of the temporary file rendering. However, when notepad goes to save the file, it brings up a file dialog prompting you of the location to save the file. On my machine, this defaults to to ~/Documents.

However, if I exit the Rust process, the file saves fine.

I think this is due to how Windows locks files. Specifically, you can't open a file for writing if another process has an active [write] handle on it unless you just through hoops. In this crate, the tempfile crate has a handle on the file, effectively locking out other writers.

A common solution to this problem is to create a temporary directory then create and close handles on a file within that. The contents of the temporary directory will get purged on drop. But you don't have to worry about multiple processes having a handle on the temporary file. This approach also enables you to give the temporary file a nice name, which could provide UI wins.

indygreg avatar Apr 24 '22 18:04 indygreg

This problem often occurs when using tempfile crate. For the local fix you can change src/edit.rs:96 from ".tempfile()?;" to ".tempfile_in("your_default_save_place")?;"

PFAhard avatar Jun 15 '22 07:06 PFAhard