-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix AGENTS.md including Ecto references in --no-ecto projects #6581
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
Draft
bmalum
wants to merge
4
commits into
phoenixframework:main
Choose a base branch
from
bmalum:fix-agents-md-ecto-references
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.
+177
−58
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c3fff01
Remove Ecto references from AGENTS.md for --no-ecto projects
bmalum 95c4c8b
Isolate AGENTS.md content for all phx.new flags
bmalum adf95a7
Fix LiveView leakage in --no-live projects with Ecto
bmalum e04da94
Remove UI/CSS guidelines from API-only projects
bmalum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| ### Phoenix Components | ||
|
|
||
| - Out of the box, `core_components.ex` imports an `<.icon name="hero-x-mark" class="w-5 h-5"/>` component for hero icons. **Always** use the `<.icon>` component for icons, **never** use `Heroicons` modules or similar | ||
| - **Always** use the imported `<.input>` component for form inputs from `core_components.ex` when available. `<.input>` is imported and using it will save steps and prevent errors | ||
| - If you override the default input classes (`<.input class="myclass px-2 py-1 rounded-lg">)`) class with your own values, no default classes are inherited, so your | ||
| custom classes must fully style the input |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| ### Phoenix LiveView Layout Guidelines | ||
|
|
||
| - **Always** begin your LiveView templates with `<Layouts.app flash={@flash} ...>` which wraps all inner content | ||
| - Anytime you run into errors with no `current_scope` assign: | ||
| - You failed to follow the Authenticated Routes guidelines, or you failed to pass `current_scope` to `<Layouts.app>` | ||
| - **Always** fix the `current_scope` error by moving your routes to the proper `live_session` and ensure you pass `current_scope` as needed |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| ### Phoenix Layouts & Flash | ||
|
|
||
| - The `MyAppWeb.Layouts` module is aliased in the `my_app_web.ex` file, so you can use it without needing to alias it again | ||
| - Phoenix v1.8 moved the `<.flash_group>` component to the `Layouts` module. You are **forbidden** from calling `<.flash_group>` outside of the `layouts.ex` module |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,3 @@ | ||
| ### Phoenix v1.8 guidelines | ||
|
|
||
| - **Always** begin your LiveView templates with `<Layouts.app flash={@flash} ...>` which wraps all inner content | ||
| - The `MyAppWeb.Layouts` module is aliased in the `my_app_web.ex` file, so you can use it without needing to alias it again | ||
| - Anytime you run into errors with no `current_scope` assign: | ||
| - You failed to follow the Authenticated Routes guidelines, or you failed to pass `current_scope` to `<Layouts.app>` | ||
| - **Always** fix the `current_scope` error by moving your routes to the proper `live_session` and ensure you pass `current_scope` as needed | ||
| - Phoenix v1.8 moved the `<.flash_group>` component to the `Layouts` module. You are **forbidden** from calling `<.flash_group>` outside of the `layouts.ex` module | ||
| - Out of the box, `core_components.ex` imports an `<.icon name="hero-x-mark" class="w-5 h-5"/>` component for hero icons. **Always** use the `<.icon>` component for icons, **never** use `Heroicons` modules or similar | ||
| - **Always** use the imported `<.input>` component for form inputs from `core_components.ex` when available. `<.input>` is imported and using it will save steps and prevent errors | ||
| - If you override the default input classes (`<.input class="myclass px-2 py-1 rounded-lg">)`) class with your own values, no default classes are inherited, so your | ||
| custom classes must fully style the input | ||
| This section intentionally minimal - core Phoenix concepts are covered in the main usage-rules sections below. |
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| ## Ecto Forms | ||
|
|
||
| ### Creating forms from changesets | ||
|
|
||
| When using changesets, the underlying data, form params, and errors are retrieved from it. The `:as` option is automatically computed too. E.g. if you have a user schema: | ||
|
|
||
| defmodule MyApp.Users.User do | ||
| use Ecto.Schema | ||
| ... | ||
| end | ||
|
|
||
| And then you create a changeset that you pass to `to_form`: | ||
|
|
||
| %MyApp.Users.User{} | ||
| |> Ecto.Changeset.change() | ||
| |> to_form() | ||
|
|
||
| Once the form is submitted, the params will be available under `%{"user" => user_params}`. | ||
|
|
||
| In the template, the form assign can be passed to the `<.form>` function component: | ||
|
|
||
| <.form for={@form} id="todo-form"> | ||
| <.input field={@form[:field]} type="text" /> | ||
| </.form> | ||
|
|
||
| Always give the form an explicit, unique DOM ID, like `id="todo-form"`. | ||
|
|
||
| ### Avoiding form errors with changesets | ||
|
|
||
| **Always** use a form assigned via `to_form/1` or `to_form/2`, and the `<.input>` component in the template. In the template **always access forms this way**: | ||
|
|
||
| <%!-- ALWAYS do this (valid) --%> | ||
| <.form for={@form} id="my-form"> | ||
| <.input field={@form[:field]} type="text" /> | ||
| </.form> | ||
|
|
||
| And **never** do this: | ||
|
|
||
| <%!-- NEVER do this (invalid) --%> | ||
| <.form for={@changeset} id="my-form"> | ||
| <.input field={@changeset[:field]} type="text" /> | ||
| </.form> | ||
|
|
||
| - You are FORBIDDEN from accessing the changeset in the template as it will cause errors | ||
| - **Never** use `<.form let={f} ...>` in the template, instead **always use `<.form for={@form} ...>`**, then drive all form references from the form assign as in `@form[:field]` |
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| ## Ecto LiveView Forms | ||
|
|
||
| ### Creating LiveView forms from changesets | ||
|
|
||
| When using changesets with LiveView, the underlying data, form params, and errors are retrieved from it. The `:as` option is automatically computed too. E.g. if you have a user schema: | ||
|
|
||
| defmodule MyApp.Users.User do | ||
| use Ecto.Schema | ||
| ... | ||
| end | ||
|
|
||
| And then you create a changeset that you pass to `to_form`: | ||
|
|
||
| %MyApp.Users.User{} | ||
| |> Ecto.Changeset.change() | ||
| |> to_form() | ||
|
|
||
| Once the form is submitted, the params will be available under `%{"user" => user_params}`. | ||
|
|
||
| In the template, the form assign can be passed to the `<.form>` function component: | ||
|
|
||
| <.form for={@form} id="todo-form" phx-change="validate" phx-submit="save"> | ||
| <.input field={@form[:field]} type="text" /> | ||
| </.form> | ||
|
|
||
| Always give the form an explicit, unique DOM ID, like `id="todo-form"`. | ||
|
|
||
| ### Avoiding form errors with changesets | ||
|
|
||
| **Always** use a form assigned via `to_form/1` or `to_form/2` in the LiveView, and the `<.input>` component in the template. In the template **always access forms this way**: | ||
|
|
||
| <%!-- ALWAYS do this (valid) --%> | ||
| <.form for={@form} id="my-form"> | ||
| <.input field={@form[:field]} type="text" /> | ||
| </.form> | ||
|
|
||
| And **never** do this: | ||
|
|
||
| <%!-- NEVER do this (invalid) --%> | ||
| <.form for={@changeset} id="my-form"> | ||
| <.input field={@changeset[:field]} type="text" /> | ||
| </.form> | ||
|
|
||
| - You are FORBIDDEN from accessing the changeset in the template as it will cause errors | ||
| - **Never** use `<.form let={f} ...>` in the template, instead **always use `<.form for={@form} ...>`**, then drive all form references from the form assign as in `@form[:field]` |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,9 @@ | |
|
|
||
| - Phoenix templates **always** use `~H` or .html.heex files (known as HEEx), **never** use `~E` | ||
| - **Always** use the imported `Phoenix.Component.form/1` and `Phoenix.Component.inputs_for/1` function to build forms. **Never** use `Phoenix.HTML.form_for` or `Phoenix.HTML.inputs_for` as they are outdated | ||
| - When building forms **always** use the already imported `Phoenix.Component.to_form/2` (`assign(socket, form: to_form(...))` and `<.form for={@form} id="msg-form">`), then access those forms in the template via `@form[:field]` | ||
| - When building forms **always** use the already imported `Phoenix.Component.to_form/2` (`assign(:form, to_form(...))` and `<.form for={@form} id="msg-form">`), then access those forms in the template via `@form[:field]` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Phoenix.Component already assumes LiveView, so this change still assumes LiveView. I would keep it as is, I think the templates one below is fine. |
||
| - **Always** add unique DOM IDs to key elements (like forms, buttons, etc) when writing templates, these IDs can later be used in tests (`<.form for={@form} id="product-form">`) | ||
| - For "app wide" template imports, you can import/alias into the `my_app_web.ex`'s `html_helpers` block, so they will be available to all LiveViews, LiveComponent's, and all modules that do `use MyAppWeb, :html` (replace "my_app" by the actual app name) | ||
| - For "app wide" template imports, you can import/alias into the `my_app_web.ex`'s `html_helpers` block, so they will be available to all templates and all modules that do `use MyAppWeb, :html` (replace "my_app" by the actual app name) | ||
|
|
||
| - Elixir supports `if/else` but **does NOT support `if/else if` or `if/elsif`**. **Never use `else if` or `elseif` in Elixir**, **always** use `cond` or `case` for multiple conditionals. | ||
|
|
||
|
|
||
This file contains hidden or 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this works for phx.new, it doesn't work for
mix usage_rules.sync, since you then get basically the same content twice :(So I don't see a good way to make this work without lots of duplication (basically having one set of rules for the root
usage-rulesand then a separate set of files for the generator). The question then is if we're fine with that and we want to strive for perfect AGENTS.md for all possible phx.new flags, or if we instead explicitly only support the default case. Then, we'd tell people to use--no-agents-mdor we could also skip generating the AGENTS.md file for non-trivial flags to not generate confusing content. I don't know... (cc @chrismccord @josevalim)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SteffenDE I think you can control in usage_rules what you want to sync, no?
In any case, I think this is changing too much at the same time. For example, I would not be inclined to split the Phoenix one into multiple sections. But I am fine with two versions for Ecto (as long as we confirm usage_rules can choose what to sync).
So in my two cents: