Compilation error when running ecto.migrate after import of phx.gen.auth

I am very new to Phoenix and Elixir and currently trying to build a new Web App using Phoenix 1.6.
I am following the guide to use the gen.auth.

mix deps.get

works fine and returns:

All dependencies are up to date

The next step is to run:

mix ecto.setup

At this point, the following error is triggered:

Compilation error in file lib/testproject_web/views/user_confirmation_view.ex ==
** (CompileError) lib/testproject_web/templates/user_confirmation/edit.html.heex:1: undefined function form/1
    (elixir 1.12.3) src/elixir_locals.erl:114: anonymous fn/3 in :elixir_locals.ensure_no_undefined_local/3
    (stdlib 3.12) erl_eval.erl:680: :erl_eval.do_apply/6
    (elixir 1.12.3) lib/kernel/parallel_compiler.ex:319: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

Version of Elixir and Phoenix are up-to-date.

Does anyone have an idea how to fix this error? I have not found anything useful on the internet yet.

Thank you for any help!

Can you show the contents of file templates/user_confirmation/edit.html.heex:1 for us to see?

Thanks for the reply. There it is:

<h1>Confirm account</h1>

<.form let={_f} for={:user} action={Routes.user_confirmation_path(@conn, :update, @token)}>
    <%= submit "Confirm my account" %>

  <%= link "Register", to: Routes.user_registration_path(@conn, :new) %> |
  <%= link "Log in", to: Routes.user_session_path(@conn, :new) %>

It didn’t change here anything. It’s a generated file from gen.auth.

OK great - that form/1 comes from Phoenix.LiveView.Helpers. So let’s make sure that is being properly imported. The first step is to the View:

# lib/testproject_web/views/user_confirmation_view.ex
defmodule TestprojectWeb.UserConfirmationView do
  use TestprojectWeb, :view

This tells us where the imports are being defined - in TestprojectWeb.view. So let’s check that out:

# lib/testproject_web.ex
defmodule TestprojectWeb do
  def view do
    quote do
      use Phoenix.View,
        root: "lib/testproject_web/templates",
        namespace: TestprojectWeb

      # Import convenience functions from controllers
      import Phoenix.Controller,
        only: [get_flash: 1, get_flash: 2, view_module: 1, view_template: 1]

      # Include shared imports and aliases for views

The very last part is what we’re looking for - the shared imports. But that comes from yet another function called view_helpers, which is here in the same file. Scroll down further and you should find

# lib/testproject_web.ex
defp view_helpers do
  quote do
    # Use all HTML functionality (forms, tags, etc)
    use Phoenix.HTML

    # Import LiveView and .heex helpers (live_render, live_patch, <.form>, etc)
    import Phoenix.LiveView.Helpers

    # Import basic rendering functionality (render, render_layout, etc)
    import Phoenix.View

    import TestprojectWeb.ErrorHelpers
    import TestprojectWeb.Gettext
    alias TestprojectWeb.Router.Helpers, as: Routes

And there is Phoenix.LiveView.Helpers being imported! Note the comment even mentions the <.form> helper included.

Hopefully at some point in this process, you found something was missing, and could use the examples above to remedy the issue. If not - if everything is already there, and you’re still getting the missing form/1 error - then we can try some other possibilities next.


Thank you very much for your great help! The import import Phoenix.LiveView.Helpers was missing. Now the errors are resolved. Thanks again!


great explanation

1 Like

Also very useful for upgrading to Liveview 0.18 from .17

1 Like