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

Bug: At emacs startup, ocamlformat creates a lot of temporary files #2001

Open
mattiasdrp opened this issue Feb 1, 2022 · 7 comments
Open

Comments

@mattiasdrp
Copy link
Contributor

mattiasdrp commented Feb 1, 2022

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
@mattiasdrp
Copy link
Contributor Author

It should be noticed that when saving a file, ocamlformat will format each temporary file it created

@gpetiot
Copy link
Collaborator

gpetiot commented Feb 1, 2022

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?

@mattiasdrp
Copy link
Contributor Author

mattiasdrp commented Feb 1, 2022

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

@mattiasdrp
Copy link
Contributor Author

The steps I provided will remove all installed files when you leave emacs so it's safe to execute it ;-)

@mattiasdrp
Copy link
Contributor Author

I'm using emacs 28.0.50, if this may help but I have the same behaviour with emacs 27.2

@mattiasdrp
Copy link
Contributor Author

mattiasdrp commented Feb 5, 2022

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:
    Saving file /home/mattias/dir/main.ml...
    Wrote /tmp/ocamlformatDFlBDG.ml
    Applied ocamlformat on /home/mattias/dir/main.ml
    Wrote /home/mattias/dir/main.ml
    
    If I have a .ocamlformat file in dir, 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?

@gpetiot
Copy link
Collaborator

gpetiot commented Feb 7, 2022

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.

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

No branches or pull requests

2 participants