Skip to content

Phoenix Dialyxir Quickstart

Jeremy Huffman edited this page Jul 4, 2016 · 12 revisions

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

Edit your mix.exs:

In deps add:

{:dialyxir, "~> 0.3.5", only: [:dev]}

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

Spurious warnings are a drag so I hope we can figure this one out, but for now you'll just need to ignore it. If you really don't want to see it can you can pass the no _match arg to dialyzer e.g. mix dialyzer -Wno_match

That will give you a completely clean run with no errors. You can also add that in your mix.exs file, but I really do not recommend this because this warning could be an important one you'd want to see in other places.

Clone this wiki locally