Bug: At emacs startup, ocamlformat creates a lot of temporary files
Describe the bug OCamlformat creates a lot of files in /tmp/ named ocamlformat[random ascii*].ml when starting emacs
How to Reproduce Steps to reproduce the behavior:
- Create an empty directory with a file.ml file
- Download the file plain-ocamlformat.zip and unzip it
- Start emacs in the directory like this:
- emacs -q -l plain-ocamlformat.el file.ml
- You should now see a lot of files being created and a max-lisp-eval-depth bug appear
It should be noticed that when saving a file, ocamlformat will format each temporary file it created
I don't observe this with the main branch of ocamlformat, what version are you using? If that's an old one can you try with 0.20.0 or 0.20.1? The .el file provided your archive requires many packages and don't work out of the box for me, can you try with a simplified version to make sure it doesn't come from the .el file?
I have ocamlformat 0.20.1 The file I provided should install all the required packages and doesn't do anything more than loading the needed files with use-package. Is there another way to test it out of the box? What I did is
mkdir dir
cd dir
opam switch create .
opam install ocamlformat tuareg
touch file.ml
emacs -q -l plain-ocamlformat.el file.ml
The steps I provided will remove all installed files when you leave emacs so it's safe to execute it ;-)
I'm using emacs 28.0.50, if this may help but I have the same behaviour with emacs 27.2
So, after a bit of investigation this is what I noticed:
- Hooking ocamlformat to tuareg-mode provokes the bug (the creation of a lot of temporary files in the /tmp directory):
(tuareg-mode . ocamlformat)makes ocamlformat bug(tuareg-mode . (lambda () (add-hook 'before-save-hook 'ocamlformat-before-save nil 'local)))is perfectly fine
- Not having ocamlformat outside detected projects enabled launches ocamlformat with nothing happening in the file (which is expected)
This is what appears in the
*messages*buffer of emacs:
If I have aSaving file /home/mattias/dir/main.ml... Wrote /tmp/ocamlformatDFlBDG.ml Applied ocamlformat on /home/mattias/dir/main.ml Wrote /home/mattias/dir/main.ml.ocamlformatfile indir, the same messages appear but the file is formatted (which is expected)
Temporary files are created anytime you save a file but if you don't hook ocamformat to tuareg-mode at least you don't end up with 20 temporary files created at startup.
Using the #2003 patch doesn't change anything about these behaviours. You still can't reproduce it with the steps I provided?
I see, using the lambda notation to hook ocamlformat onto the tuareg mode is what we used in the documentation (https://github.com/ocaml-ppx/ocamlformat/blob/main/doc/editor_setup.mld) this page shows some tips on how to neatly interface ocamlformat with tuareg. Unfortunately I'm far from being an emacs lisp expert, so if you want to dig deeper as to why this issue occurs maybe it would be better to ask some emacs community.