elixir-lang / elixir

Elixir is a dynamic, functional language for building scalable and maintainable applications
https://elixir-lang.org/
Apache License 2.0
24.51k stars 3.38k forks source link

`mix format` changes file modification time if content was not changed #7090

Closed remi closed 6 years ago

remi commented 6 years ago

If mix format is ran on a file and its content is not changed by the formatter, its modification time is still updated.

Environment

Current behavior

$ stat config/config.exs
mtime   1512563789

$ md5sum config/config.exs
6ba14e314d35959e0702095fcba2ef18  config/config.exs

$ mix format

$ stat config/config.exs
mtime   1512563851

$ md5sum config/config.exs
6ba14e314d35959e0702095fcba2ef18  config/config.exs

We can see the file’s mtime attribute has been updated but its content stayed the same (hence the same md5 hash).

The problem with this is that it doesn’t play well with Phoenix code reloading feature (and probably other tools that look at file modification times to detect file updates) which requires the developer to restart its server when a config file has been modified (based on mtime):

You must restart your server after changing the following config or lib files:

  * config/config.exs

It this by design in mix format or is it a bug? Should the file be touched even if its content was not changed by the formatter?

Expected behavior

If mix format doesn’t change the content of a file, it should not change its modification time.

Thank you! 😄

josevalim commented 6 years ago

Yup, we should not write the files. A quick benchmark I ran locally even showed that the formatter is faster if it is not writing on every change.

doomspork commented 6 years ago

@josevalim I'd be happy to take a whack at this if it's not something you're already tackling 👍

josevalim commented 6 years ago

@doomspork glad you asked because I am almost done with it! :D

remi commented 6 years ago

@josevalim Wow, thank you for fixing this so quickly! This removed the frustration of having to restart my Phoenix server each time I run mix format on a config file! 🎉 😀