brainlid

brainlid

Blog Post: Adding Dialyzer without the Pain

Dialyzer is a tool that you’ve probably heard about in the Elixir community. You may have even used it. However, adding Dialyzer to an existing project can feel overwhelming when you see the wall of red and have 100s of errors. Hitting that resistance can turn people right around, running for the door. Noah Betzen shows us how adding Dialyzer doesn’t have to be scary. He walks us through the process on several existing public Elixir projects. He shares some tips and tricks that let us keep our productivity without getting overwhelmed.

Most Liked

jhogberg

jhogberg

Erlang Core Team

but it’s worth mentioning that ignoring new errors is completely valid as well.

It’s also worth mentioning that dialyzer will not analyze beyond errors because that state is absurd and cannot be reasoned about: ignoring the error merely suppresses the message. This can suppress other errors or magically bring new and incredibly difficult ones into existence.

Warnings like those for constructing “improper” lists are safe to ignore, but I don’t recommend ignoring errors. By all means, feel free to tackle the problems one at a time, but shoving them under the rug by placing them in an ignore-file can make other errors needlessly difficult to diagnose. :confused:

Nezteb

Nezteb

Let’s say we have a file that it’s ignored. Now, some code is added to that file and that new code has an error. Is there a way to have dialyzer warn about this?

One thing you can do is:

# .dialyzer_ignore.exs
[
  {"lib/something.ex", :no_return},
  {"lib/something.ex", :call},
]

Now, if you introduce new errors to lib/something.ex, as long as it isn’t a :no_return or :call error, it will still get caught. However, if you do add a new :no_return error to the file, it will get ignored unfortunately.

Another option is to use regex patterns (which I’ve not experimented with much), like so:

# .dialyzer_ignore.exs
[
  ~r/my_file\.ex.*my_function.*no local return/
]

With this, you could refine the ignored error to a specific function/error combination.

However, I created a PR to add a more strict ignore file format, and it’s been merged. It should come out in the next release of dialyxir.

Also, one helpful method to partially address the weaknesses of the ignore method that @jhogberg pointed out is:

  1. Delete all ignored lines from the .dialyzer_ignore.exs file.
  2. Run mix dialyzer again.
  3. Fix whatever errors you can (or are willing to). Maybe isolate your quest to a single file/module at a time.
  4. Regenerate an ignore file and add those lines to the ignore file again. This should hopefully now be a smaller file than it was previously.
kazlu

kazlu

I’ve been in discussions with my team about adding dialyzer to our codebase. I like the approach you outline in the article, however I do have a question. Let’s say we have a file that it’s ignored. Now, some code is added to that file and that new code has an error. Is there a way to have dialyzer warn about this? AFAICT we only have 2 options:

  1. Ignore specific lines. This seems like a maintenance nightmare as line numbers will have to be kept in sync.
  2. Ignore specific error types. This kind of works, but would break if the error is the same type as a previously ignored error.

Once we get to 0 errors, this will solve itself out, so I’m not super concerned, but I just wanted to get some input from the experts.

Thank you!

Where Next?

Popular in Blog Posts Top

New
tmartin8080
Sharing some articles I’ve written recently while working in Elixir. Some are how-to guides and others for reference. Will update this ...
New
axelson
I talk about how I really like to use runtime configuration and discuss some common pitfalls of configuration in Elixir.
New
marcin
Hi! :wave: I wanted to refresh my knowledge on how to mix phx.gen.auth with local password users db, as well as OAuth providers such as ...
New
New
aymanosman
The desire to produce structured logs is common. In this article, I will survey the major approaches one could take to achieve this goal ...
New
brainlid
Jason Stiebs shows a couple ways for a LiveView to make it easy for users to click and copy an important value to their clipboard. He sho...
New
New
Paradox
Testing is an important part of any modern piece of software. But writing tests can quickly become an exercise in frustration, with tons ...
New
PragTob
I ran into an interesting problem recently where simple concurrency on the BEAM via Task.async made my application a lot slower and a lot...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

We're in Beta

About us Mission Statement