Review Compiler Warnings

I recently had a server side error because a Phoenix view module was not aliased.

file: company_admin_view.ex

  def render("foo.json", %{user: user}) do
    %{
      ...some other stuff here...
      user: render_one(user, UserView, "user_admin.json"),
    }
  end

UserView is undefined, as I have forgotten to alias MyApp.Web.UserView. I’m guessing the compiler warned me about this at some point, but I missed it. How do I get to review all existing compiler warnings? I’ve tried mix compile --force, but this does not mention anything about UserView being undefined.

1 Like

You won’t get a warning for this. The compiler can’t know at compiletime, if you will have that module available at runtime. Even worse, it can’t even know that you want to try to specify a module there which shall be used to call a function in it later on.

The compiler just sees an alias, which is only syntactic sugar around atoms, and as such UserView (exactly :"Elixir.UserView") does exist from the moment you load the referencing module into memory for the first time until you shut down your BEAM.

You will get xref warnings for function call literals:

warning: function Blah.bar/0 is undefined (module Blah is not available)
  lib/jlr_demo/web/schema.ex:5

Dynamic function calls this can’t happen for the reasons @NobbZ states.

Try #credo. Good stuff

Would that help here? I would be very surprised if Credo could detect dynamic function calls.

You are right. It won’t. But it catches and suggests as part of coding styles guide. Kinda post #elixirfmt eventually I envision.

Even if we were able to detect, that there is a dynamic function call happening into a module that isn’t available right now during compilation, we can’t know if there might be exactly this module at runtime because its generated somewhere else and simply dropped into the module path.