Mix phx.gen.html generator immediately causes compile errors?

I’m making a new Phoenix app for the first time in about a year, and trying to get a handle on everything that’s changed with regards to context etc.

My first order of business is creating a database table where I can track email signups, so I put this command in:

mix phx.gen.html Signups Email emails email:string name:string data:text

And then I put the new routes in my router.ex. But even before I can migrate, I get an error:

== Compilation error in file lib/appname/views/email_view.ex ==
** (CompileError) lib/appname/views/email_view.ex:2: module AppnameWeb is not loaded and could not be found
    (elixir) expanding macro: Kernel.use/2
    lib/appname/views/email_view.ex:2: AppnameWeb.EmailView (module)
    (elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1
I have Appname.Web other places in my app, but the generator created a new module called AppnameWeb that doesn't seem to be loaded anywhere?

If I do a find and replace in my project to Appname.Web to AppnameWeb, it lets me migrate but still fails to load the new emails index page.

Request: GET /emails
** (exit) an exception was raised:
    ** (UndefinedFunctionError) function Appname.EmailController.init/1 is undefined (module Appname.EmailController is not available)
        Appname.EmailController.init(:index)
        (appname) web/router.ex:1: anonymous fn/1 in Appname.Router.__match_route__/4
        (phoenix) lib/phoenix/router.ex:278: Phoenix.Router.__call__/1
        (appname) lib/appname/endpoint.ex:1: Appname.Endpoint.plug_builder_call/2
        (appname) lib/plug/debugger.ex:99: Appname.Endpoint."call (overridable 3)"/2
        (appname) lib/appname/endpoint.ex:1: Appname.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) /Users/user/Dev/appname/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

So am I supposed to have both Appname.Web and AppnameWeb? I’m not sure what’s supposed to go where any more, and the fact that the generator generates broken code isn’t helping…

I tried making a new app again and run into the exact same error.

Have you started this project with 1.3.0rc2 or earlier? There App.Web was convention, after that it has been changed to AppWeb.

If so, you’ll need to identify and find those old stuff und update it to match current conventions to make the generator work again.

This is only a guess though.

Thanks for the reply! That sounded like a reasonable explanation, so I made sure I updated to the latest version and created another test app.

> mix phoenix.new -v
Phoenix v1.3.0-rc.3

And I still get the same error.

Can you please update phoenix_new to the latest version? Just to be sure, and try again? Perhaps the structure with *.Web was longer there than I thought…

Thanks for the reply! That sounded like a reasonable explanation, so I made
sure I updated to the latest version and created another test app.

mix phoenix.new -v
Phoenix v1.3.0-rc.3

But that’s not the latest version :slight_smile: – should be

08:35 ~/testcases/elixir$ mix phx.new -v
Phoenix v1.3.0

Hello! I’ve just gotten started with Elixir and Phoenix, and I’m running into this problem. I’m using the latest Phoenix
mix phx.new -v Phoenix v1.3.0
and getting the same error:

== Compilation error in file lib/stats_api_web/views/changeset_view.ex ==
** (CompileError) lib/stats_api_web/views/changeset_view.ex:2: module StatsApiWeb is not loaded and could not be found
    (elixir) expanding macro: Kernel.use/2
    lib/stats_api_web/views/changeset_view.ex:2: StatsApiWeb.ChangesetView (module)
    (elixir) lib/kernel/parallel_compiler.ex:121: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1

This is with a brand new app. I haven’t changed a single thing. I’m really excited to dig into Elixir and Phoenix, but need the generators to get started. :slight_smile:

Well, I’ve confirmed that a project-wide s/StatsApi.Web/StatsApiWeb will allow mix ecto.migrate to complete.

Updating the module from StatsApi to StatsApiWeb in the router scope allows pages/api endpoints to load.

However, there’s still a compile error when running mix test, and I just don’t have the knowledge of the internals to sort it out. Here’s the text of that compile error:

16:14:21.561 [error] GenServer #PID<0.3167.0> terminating
** (CompileError) test/stats_api/public/public_test.exs:2: module StatsApi.DataCase is not loaded and could not be found
    (elixir) src/elixir_expand.erl:86: :elixir_expand.expand/2
    (elixir) src/elixir_expand.erl:511: :elixir_expand.expand_block/4
    (elixir) src/elixir_expand.erl:39: :elixir_expand.expand/2
    (elixir) expanding macro: Kernel.use/1
    test/stats_api/public/public_test.exs:2: StatsApi.PublicTest (module)
    (elixir) src/elixir.erl:239: :elixir.eval_forms/4
    (elixir) src/elixir_compiler.erl:61: :elixir_compiler.eval_forms/3
    (elixir) src/elixir_lexical.erl:17: :elixir_lexical.run/3
    (elixir) src/elixir_compiler.erl:27: :elixir_compiler.quoted/3
    (elixir) lib/code.ex:376: Code.require_file/2
    (elixir) lib/kernel/parallel_require.ex:59: anonymous fn/2 in Kernel.ParallelRequire.spawn_requires/5

I’ll see if I can keep working without generators in the mean time. :slight_smile:

Well, I’m pleased to report that I discovered what went wrong. Turns out I was creating the project using mix phoenix.new rather than mix phx.new. After using mix phx.new to create the project, the mix phx.gen.json generator works perfectly fine. Hope this helps someone along the way! Sorry for being a n00b! :slight_smile:

1 Like