Skip to content

Skip empty files during mix format #11802

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

Merged
merged 1 commit into from
May 7, 2022
Merged

Skip empty files during mix format #11802

merged 1 commit into from
May 7, 2022

Conversation

ak3nji
Copy link
Contributor

@ak3nji ak3nji commented May 6, 2022

Previously it was adding an empty line to empty files during the mix format task.
This PR makes it so empty files are skipped and remain unchanged.

Fixes #11800

@josevalim
Copy link
Member

Thank you for the PR! I believe we should make this change up in the Code.format_string code and similar, and make it sure that formatting "" always returns "".

@ak3nji ak3nji force-pushed the main branch 2 times, most recently from 29c6cae to 71dba76 Compare May 6, 2022 22:16
@ak3nji
Copy link
Contributor Author

ak3nji commented May 6, 2022

Thank you for the PR! I believe we should make this change up in the Code.format_string code and similar, and make it sure that formatting "" always returns "".

I took a look at Code.format_string! and it was already formatting "" to "". The bug was in Format.elixir_format/2 which was always adding a trailing \n for every string, including the empty string from an empty file.

@@ -561,7 +563,7 @@ defmodule Mix.Tasks.Format do

defp format_file({file, formatter}, task_opts) do
input = read_file(file)
output = formatter.(input)
output = (bit_size(input) > 0 && formatter.(input)) || <<>>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this change, given the change to elixir_format?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should skip empty files when formatting, but the change to elixir_format skips formatting empty lines, and an empty file is just an empty line. Removed this piece of code from the PR as it was redundant. Thanks!

@josevalim
Copy link
Member

The bug was in Format.elixir_format/2 which was always adding a trailing \n for every string, including the empty string from an empty file.

I see, makes sense!

@josevalim josevalim merged commit 9e1aa0b into elixir-lang:main May 7, 2022
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

@eksperimental
Copy link
Contributor

Thank you @ak3nji for looking into it.
After the fix, I 'm playing around with it and found another edge case.
If you create a file only with a space it will format it as \n.
I'm wondering if we should trim the content of the file and then format it. In this case if we do that, it will return an empty string.
I think both results are OK, just mentioning it to see if there is a rationale that is more suitable than the other.

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

Successfully merging this pull request may close these issues.

Formatter will complain about empty .exs files
3 participants