Is it possible to make the compiler fail earlier if the build is not completed if a warning about compilation time occurs?

I find compile-time warnings to be very useful, but sometimes I can skip them, especially if it asks for a download request, where tests run on the CI server.

Ideally, I would indicate something in the mix project file that would make the compiler more rigorous.

I want this to work for all mixing tasks, and I donโ€™t want to pass the flag to the team, as this is easy to forget.

For example, with a compiler warning project, this command should fail

mix clean && mix compile 

Like this one

 mix clean && mix test 
+6
source share
2 answers

Perhaps to some extent. elixirc has the --warnings-as-errors flag.

 โ˜ hello_elixir [master] โšก elixirc Usage: elixirc [elixir switches] [compiler switches] [.ex files] -o The directory to output compiled files --no-docs Do not attach documentation to compiled modules --no-debug-info Do not attach debug info to compiled modules --ignore-module-conflict --warnings-as-errors Treat warnings as errors and return non-zero exit code --verbose Print informational messages. ** Options given after -- are passed down to the executed code ** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTIONS ** Options can be passed to the erlang compiler using ERL_COMPILER_OPTIONS 

For a module like this with a warning:

 defmodule Useless do defp another_userless, do: nil end 

When compiling without a flag:

 โ˜ 01_language [master] โšก elixirc useless.ex useless.ex:2: warning: function another_userless/0 is unused โ˜ 01_language [master] โšก echo $? 0 

You get a return code as 0 .

But when you compile the --warnings-as-errors flag, it returns exit code 1 .

 โ˜ 01_language [master] โšก elixirc --warnings-as-errors useless.ex useless.ex:1: warning: redefining module Useless useless.ex:2: warning: function another_userless/0 is unused โ˜ 01_language [master] โšก echo $? 1 

You can use this return code in your script compiler to break the build process.

+7
source

In mix.exs :

 def project do [..., aliases: aliases] end defp aliases do ["compile": ["compile --warnings-as-errors"]] end 

Then mix compile will pass the --warnings-as-errors subtask to compile.elixir .

This also works for mix test , as it does the compile task implicitly.


If you don't add an alias, you can still run mix compile --warnings-as-errors and it will do what you expect, but mix test --warnings-as-errors will not do what you expect, since the flag does not reach the compile.elixir task.

+6
source

All Articles