Skip to content

Improve error message when attributes have been defined after an embedded template #3812

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

Open
prem-prakash opened this issue May 18, 2025 · 5 comments · May be fixed by #3833
Open

Improve error message when attributes have been defined after an embedded template #3812

prem-prakash opened this issue May 18, 2025 · 5 comments · May be fixed by #3833

Comments

@prem-prakash
Copy link

prem-prakash commented May 18, 2025

LiveView generated application

defmodule MyappWeb.Layouts do
  @moduledoc """
  This module holds different layouts used by your application.

  See the `layouts` directory for all templates available.
  The "root" layout is a skeleton rendered as part of the
  application router. The "app" layout is rendered as component
  in regular views and live views.
  """
  use MyappWeb, :html

  embed_templates "layouts/*"

  @doc """
  Renders the app layout

  ## Examples

      <Layouts.app flash={@flash}>
        <h1>Content</h1>
      </Layout.app>

  """
  attr :flash, :map, required: true, doc: "the map of flash messages"

  attr :current_scope, :map,
    default: nil,
    doc: "the current [scope](https://hexdocs.pm/phoenix/scopes.html)"

  slot :inner_block, required: true

  def app(assigns) do
    ~H"""
    <header class="navbar px-4 sm:px-6 lg:px-8">
== Compilation error in file lib/myapp_web/components/layouts.ex ==
** (CompileError) lib/myapp_web/components/layouts.ex:24: attributes must be defined before the first function clause at line 12

    (phoenix_live_view 1.0.9) lib/phoenix_component/declarative.ex:557: Phoenix.Component.Declarative.compile_error!/3
    (phoenix_live_view 1.0.9) lib/phoenix_component/declarative.ex:733: Phoenix.Component.Declarative.register_component!/4
    (phoenix_live_view 1.0.9) expanding macro: Phoenix.Component.Declarative.__pattern__!/2
    lib/myapp_web/components/layouts.ex:32: MyappWeb.Layouts.app/1
@SteffenDE
Copy link
Collaborator

Do you also have an app.html.heex file in your layouts folder?

@prem-prakash
Copy link
Author

@SteffenDE yes, I do, it is exactly the generated one.

@prem-prakash
Copy link
Author

prem-prakash commented May 21, 2025

@SteffenDE important to mention that this is attempt to upgrade a Phoenix 1.7 to the 1.8rc3. Maybe the problem is related to it.

@SteffenDE
Copy link
Collaborator

You should either use app.html.heex or def app in layouts, not both. Does it work if you remove one of them?

@prem-prakash
Copy link
Author

@SteffenDE yes, you are right, I removed the app.html.heex and it worked. Thanks!
I think we can close this.
Perhaps do you think it is worth to have a more comprehensive error message for this case?

@josevalim josevalim transferred this issue from phoenixframework/phoenix May 25, 2025
@josevalim josevalim changed the title [1.8.0-rc3] Layout breaks Improve error message when attributes have been defined after an embedded template May 25, 2025
chrismccord pushed a commit that referenced this issue Jun 5, 2025
chrismccord pushed a commit that referenced this issue Jun 5, 2025
- Enhanced error message in __before_compile__ hook to explain embed_templates vs function component conflicts
- Enhanced error message in raise_if_function_already_defined! to provide clearer guidance
- Added specific examples and actionable solutions for developers
- Addresses issue #3812 by making error messages more helpful and informative

The improved messages now explain:
- What causes the conflict (embed_templates loaded before function components)
- Concrete examples (app.html.heex vs def app function)
- Clear solutions (choose one approach, not both)
- Why the conflict occurs (embedded template loads first)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants