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

Table of contents

    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 đŸ€“

    Download our ebook
    Sign to our Newsletter
    Szymon Soppa Web Developer
    Szymon Soppa Curiosum Founder & CEO

    Read more
    on #curiosum blog

    What's the difference between alias, import, require and use in Elixir?

    Alias, import, require and use in Elixir - complete guide with use cases.

    In most programming languages we often deal with instructions responsible for handling dependencies. Elixir is no different.

    In Elixir, dependency is nothing more than compiled module which for some reason you want to use in another module. There are a couple of instructions that we use in Elixir to either make it easier or possible to interact with modules.

    In this blog post I'll explain and present use case examples of four of them:

    • alias,
    • require,
    • import,
    • use.
    Continuous integration (CI) Elixir

    Mastering Elixir CI pipeline

    Developer time is precious for business. Don't waste it on things that can be easily automated. One of these things is a part of the code review process - quality checks. Continuous integration (CI) is where this automation is put in place, and in this article, I'd love to walk you through our setup.

    Debugging Elixir Code: The Definitive Guide

    Debugging Elixir Code: The Definitive Guide

    Every application contains bugs, and even if it doesn't, it will. And even if you're not a notorious bug producer and your code quality is generally good, the conditions programmers work in are often suboptimal - you will often find yourself pulling your hair out over code written years ago by someone no longer involved in your project.