如果引发编译时警告,是否有可能让编译器提前退出,使构建失败?

lpi*_*pil 9 elixir-mix elixir

我发现编译时警告非常有用,但我偶尔可能会错过它们,特别是如果它是在一个测试在CI服务器上运行的pull请求中.

理想情况下,我会在项目组合文件中指定一些会使编译器更严格的内容.

我希望这对所有混合任务都有效,我不想将标志传递给命令,因为这很容易忘记.

例如,对于带有编译器警告的项目,此命令应该失败

mix clean && mix compile
Run Code Online (Sandbox Code Playgroud)

应该是这个

mix clean && mix test
Run Code Online (Sandbox Code Playgroud)

Tal*_*tle 12

在你的mix.exs:

def project do
  [...,
   aliases: aliases]
end

defp aliases do
  ["compile": ["compile --warnings-as-errors"]]
end
Run Code Online (Sandbox Code Playgroud)

然后mix compile将传递--warnings-as-errorscompile.elixir子任务.

这也适用,mix test因为它compile隐式运行任务.


如果你没有添加别名,你仍然可以运行mix compile --warnings-as-errors,它会按照你的预期运行,但mix test --warnings-as-errors不会按预期执行,因为标志没有达到compile.elixir任务.

  • 你也可以使用`elixirc_options:[warnings_as_errors:true]`. (5认同)

Len*_*ran 7

可能在某种程度上.有一个标志--warnings-as-errorselixirc命令.

?  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
Run Code Online (Sandbox Code Playgroud)

对于这样的模块,带有警告:

defmodule Useless do
  defp another_userless, do: nil
end
Run Code Online (Sandbox Code Playgroud)

在没有标志的情况下编译时:

?  01_language [master] ? elixirc useless.ex
useless.ex:2: warning: function another_userless/0 is unused
?  01_language [master] ? echo $?
0
Run Code Online (Sandbox Code Playgroud)

您获得的返回码为0.

但是当您使用该标志进行编译时--warnings-as-errors,它将返回退出代码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
Run Code Online (Sandbox Code Playgroud)

您可以在编译脚本中使用此返回代码来中断构建过程.