Skip to content

Phoenix Dialyxir Quickstart

Giorgio edited this page Feb 13, 2019 · 12 revisions

Assuming you've just generated a new Phoenix project using 1.2:

Edit your mix.exs:

In deps add:

{:dialyxir, "~> 0.5.0", only: [:dev], runtime: false}

In project add:

dialyzer: [plt_add_deps: :transitive]

At the command line run:

mix do deps.get, deps.compile, dialyzer --plt

Now you are all set, you can run mix dialyzer anytime you want to check your project.

except...

As of July, 2016 there are some spurious warnings in the default project. At least some of these will be fixed upstream, but for now you need to add a couple annotations to get rid of them.

Example file names for a project named myapp:

The Ecto.Repo using macro emits code that will cause warnings that look like:

repo.ex:2: Function rollback/1 has no local return

To silence these:

In lib/myapp/repo.ex add the @dialyzer attribute as below:

defmodule Myapp.Repo do
  use Ecto.Repo, otp_app: :myapp
  @dialyzer {:nowarn_function, rollback: 1}
end

The Gettext macro, due to a bug in Elixir 1.3.1 (will be fixed in 1.3.2), emits lines like this: gettext.ex:1: The inferred type for the 1st argument of 'MACRO-dgettext'/3 ({_,_}) is not a supertype of #{}, which is expected type for this argument in the callback of the 'Elixir.Gettext.Backend' behaviour

Similarly, add this attribute in web/gettext.ex:

  @dialyzer [{:nowarn_function, 'MACRO-dgettext': 3},
             {:nowarn_function, 'MACRO-dgettext': 4},
             {:nowarn_function, 'MACRO-dngettext': 5},
             {:nowarn_function, 'MACRO-dngettext': 6},
            ]

The default PageView module (which includes web/page/index.html.eex) will emit the warning: index.html.eex:1: The pattern {'safe', _@2} can never match the type binary()

Add the attribute below to suppress this.

web/views/page_view.ex

defmodule Myapp.PageView do
  use Chat.Web, :view
  @dialyzer :no_match
end

If you use Phoenix.Presence then in each of your Phoenix.Presence modules add this right after the use line:

  @dialyzer [
    {:nowarn_function, 'init': 1},
    {:nowarn_function, 'track': 3},
    {:nowarn_function, 'track': 4},
    {:nowarn_function, 'update': 3},
    {:nowarn_function, 'update': 4},
    ]

mix dialyzer should now give you a completely clean run with no errors unless you use Phoenix.Presence, which still causes this on each use Phoenix.Presence line:

my_presence.ex:75: Expression produces a value of type {'ok',pid()}, but this value is unmatched

It is also good to notice that in new phoenix projects (Elixir 1.8.1 and Phoenix 1.4.0), when following the above configuration a warning of lib/phoenix/router.ex:2: Function call/2 has no local return is emitted. More details here

Clone this wiki locally