-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Run mix format on generated files by tasks phx.gen.* #6015
Open
ShPakvel
wants to merge
5
commits into
phoenixframework:main
Choose a base branch
from
ShPakvel:add_mix_format_for_generated_files
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Run mix format on generated files by tasks phx.gen.* #6015
ShPakvel
wants to merge
5
commits into
phoenixframework:main
from
ShPakvel:add_mix_format_for_generated_files
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There is `assert_passes_formatter_check(app_root_path)` in `integrated_test/` tests. Which was pseudo-truthy, because it passes only with specific short input for `phx.gen.*` tasks. Simply adding several more attributes for a `phx.gen.*` task run, we witnessing mentioned assertion to fail. User-developer can pass any number of attributes or long names for modules. It is hard to achieve correctly formatted code in generated files. And even harder to supported it during changes. This why I propose to apply `mix format` for `phx.gen.*` tasks. I implemented it with this considerations: Formatting is [recommended](https://hexdocs.pm/mix/Mix.Tasks.Format.html#module-when-to-format-code) and [implemented in the very basis of elixir](https://hexdocs.pm/elixir/Code.html#format_string!/2). But there are still projects which do not use it. And we need to respect that decision and provide mechanism to configure or even turn off formatting during files generation. Based on this, following conditions are reasonable for me: - By default format is turned on for files with extensions `[".ex", ".exs", ".heex"]`. - Run format once for all files related to generation and print notice in console about it, with instruction how to configure or turn it off. - Format all files for just generated content: new and modified. If developers use formatting, then it is reasonable to do. If they do not use formatting, then they will turned it off in general. Applied to live, html, json, context, schema, embedded. Can be applied to others later, if this will be approved. --- For `integration_test/` I added extra attributes in `mix_run!` for `phx.gen.*` tasks, to truthfully test formatting. There is issue for mysql case (some files from `phx.new`is not formatted). So for mysql I left TODO for future separate investigation, as it is not related to `phx.gen.*` files formatting. For `test/mix/tasks/` there is no deps `[:phoenix, :ecto, :ecto_sql]` we can format code according to. So for now I came up with simple disabling formatting via our new config `with_generator_env([format_extensions: []], function)` applied in `in_tmp_project` and `in_tmp_umbrella_project`. --- Small improvements that were done in the same gen files: - Simplify conditions for context and schema files by relocating logic from live, html, json into context and schema itself, where it belongs. - Invoke context files copy before parent generator files, to skip extra binding creation. It is not only skipping unused data passing in arguments, but also improve work with code (no need to double check if it is used in context or not).
Hi @ShPakvel! Thank you for the PR. As I mentioned in the other issue, please don’t format the files. It makes our work in reviewing the PRs much harder. Generally speaking, a PR should only do one thing. :) |
@josevalim Auto-formatting is reverted. I disabled formatting locally for phoenix project. Auto-formatting won't happen again from my side. |
josevalim
reviewed
Dec 12, 2024
josevalim
reviewed
Dec 12, 2024
- Revert specific gitignore change. - Delete console notification about formatting files.
Addressed review comments. ✅ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue
There is
assert_passes_formatter_check(app_root_path)
inintegrated_test/
tests. Which was pseudo-truthy, because it passes only with specific short input forphx.gen.*
tasks. Simply adding several more attributes for aphx.gen.*
task run, we witnessing mentioned assertion to fail.User-developer can pass any number of attributes or long names for modules. It is hard to achieve correctly formatted code in generated files. And even harder to supported it during changes.
This why I propose to apply
mix format
forphx.gen.*
tasks.Solution
I implemented it with this considerations:
Formatting is recommended and implemented in the very basis of elixir. But there are still projects which do not use it. And we need to respect that decision and provide mechanism to configure or even turn off formatting during files generation.
Based on this, following conditions are reasonable for me:
[".ex", ".exs", ".heex"]
.Applied to live, html, json, context, schema, embedded. Can be applied to others later, if this will be approved.
@josevalim, @chrismccord, I believe this is pretty useful feature, that bring flexibility on future generators updates and decrease manual work for adjusting formatting complexity, simplifying code as well.
Notes about implementation.
UPD: Auto-formatted code is reverted.
Because code formatting on Phoenix project is not fully applied and protected by CI, some files were auto-formatted on save. I expect it is ok based on response to this issue.For quick introduction, here is the feature in essence.
Gathering files with generated content done this way (example from
phx.gen.html
):It is invoked after copy new files and modify existing.
|> copy_new_files(paths, binding) + |> format_files() |> print_shell_instructions()
And format executed with this code:
For
integration_test/
I added extra attributes inmix_run!
forphx.gen.*
tasks, to truthfully test formatting. There is issue for mysql case (some files fromphx.new
is not formatted). So for mysql I left TODO for future separate investigation, as it is not related tophx.gen.*
files formatting.For
test/mix/tasks/
there is no deps[:phoenix, :ecto, :ecto_sql]
we can format code according to. So for now I disabled formatting via new configwith_generator_env([format_extensions: []], function)
applied inin_tmp_project
andin_tmp_umbrella_project
. Thewith_generator_env
function itself has minor update as well, to merge passing generators config instead of fully replacing it.It is pretty simple and generally applied change I came up with as first idea. Let me know if you want to resolve it in some better way.
Small improvements that were done in the same gen files: