Treating warnings as errors in Elixir's mix compile

Article autor
September 9, 2025
Treating warnings as errors in Elixir's mix compile
Elixir Newsletter
Join Elixir newsletter

Subscribe to receive Elixir news to your inbox every two weeks.

Oops! Something went wrong while submitting the form.
Elixir Newsletter
Expand your skills

Download free e-books, watch expert tech talks, and explore open-source projects. Everything you need to grow as a developer - completely free.

Table of contents

Warnings in Elixir are usually an important sign of a problem in the codebase. There is an easy way to make them gone.

Imagine that you define an alias to a module and then try to compile it:

defmodule App.User do
end

defmodule App.Users do
  alias App.User
end

...
iex> mix compile
iex> warning: unused alias User

or you're trying to call a function on a non-existing module:

defmodule App.Users do
  def fetch_user(id), do: User.get(id)
end

...

iex> mix compile
iex> warning: User.get/1 is undefined (module User is not available or is yet to be defined)

Both of these cases generate warnings. The latter is also indication of en error:

defmodule App.Users do
  def fetch_user(id), do: User.get(id)
end

...

iex> mix compile
iex> App.Users.fetch_user(1)
** (UndefinedFunctionError) function User.get/1 is undefined (module User is not available)
    User.get(1)

Some of the warnings will not cause an error, and some will. From my perspective, you should not tolerate any of these.

Treating warnings as errors in mix compile

Here is an easy way to treat every warning as an error during project compilation:

iex> mix compile --warnings-as-errrors
iex> warning: User.get/1 is undefined (module User is not available or is yet to be defined)

Compilation failed due to warnings while using the --warnings-as-errors option

Thanks to --warnings-as-errors, we expect compilation to fail whenever there is a warning in the app, and that's exactly why we see this message:

Compilation failed due to warnings while using the --warnings-as-errors option

Setting warning as errors flag in the mix.exs

Using the flag is cool, but you might forget to include it in every command that requires a project to be compiled. If you don't want to forget, here is an option that you can set in mix.exs:

def project do
  [
    ...
    elixirc_options: [
      warnings_as_errors: true
    ]
  ]
end

From now on, whenever you run a command that compiles the project, the warnings_as_errors flag will be used.

That's it! Happy coding 🤓

Related posts

Dive deeper into this topic with these related posts

No items found.

You might also like

Discover more content from this category

How to deal with timeout issue when debugging Phoenix app

There is a common scenario: You'd like to debug your Phoenix app with break!/4 or IEx.pry/0. Everything works fine, until... Phoenix server throws a timeout error statement.

How to override Kernel macros

The macro mechanism in Elixir is not only an interesting metaprogramming feature - in fact, it is at the language's very core. And the more awesome fact is that, using macros, you can override the algorithm of defining functions itself!

How to safely handle related database operations with Ecto Multi

Sometimes you need to do some database operations at once. A simple example: User-A transfers money to User-B. Updating just one balance at the time creates a risk of data desynchronization. What if the first DB operation goes well but updating the second user’s data fails? Sounds like a hard to catch vulnerability.