Skip to content

Phoenix Dialyxir Quickstart

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

Sure, it is really straightforward.

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

Edit your mix.exs:

In deps add:

{:dialyxir, "~> 0.3.5"}

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:

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

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},
            ]

There is one more warning which I have not figured out how to suppress. 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()

As far as I know, this code is generating a function that is compiled into the Myapp.PageView module - it is in that beam file. But adding the expected attribute does not silence the warning:

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