如果出现编译时警告,是否有可能使编译器提前退出,导致构建失败? [英] Is it possible to get the compiler to exit early, failing the build, if a compile time warning is raised?
问题描述
我发现编译时警告非常有用,但是有时我会错过它们,尤其是当它在CI服务器上运行测试的请求请求中时。
理想情况下,我会在项目混合文件中指定一些内容,使编译器更加严格。
我希望所有混合任务都可以使用此功能,我确实
例如,对于带有编译器警告的项目,此命令应该失败
p> 混合清洁&&混合编译
应该这样编译
混合清洁&&混合测试
在某种程度上可能。在 elixirc
命令中有一个标记-警告为错误
。
☁hello_elixir [master]⚡elixirc
用法:elixirc [elixir开关] [编译器开关] [.ex文件]
-o输出已编译文件的目录
--no-docs不要将文档附加到已编译模块
--no-debug-info不要将调试信息附加到已编译模块
--ignore -module-conflict
--warnings-as-errors将警告视为错误并返回非零退出代码
--verbose打印参考消息。
**之后给出的选项将传递给执行的代码
**可以使用ELIXIR_ERL_OPTIONS将选项传递给erlang运行时
**可以将选项传递给erlang编译器,使用ERL_COMPILER_OPTIONS
对于这样的模块,带有警告:
defmodule没用
defp another_userless,做:无
end
在不带标志的情况下进行编译:
☁01_language [master] ⚡elixirc useless.ex
useless.ex:2:警告:函数another_userless / 0未使用
☁01_language [master]⚡echo $?
0
您获得的返回码为 0 。 / p>
但是,当您使用标志-警告错误
进行编译时,它会返回退出代码 1 。
☁01_language [master]⚡elixirc --warnings-as-errors useless.ex
useless.ex:1:警告:重新定义模块无用
useless.ex:2:警告:函数another_userless / 0未使用
☁01_language [master]⚡echo $?
1
您可以在编译脚本中使用此返回码来中断构建过程。
I find the compile time warnings very useful, but I can occasionally miss them, especially if it's on a pull request where the tests are running on a CI server.
Ideally I would specify something in the project mix file that would make the compiler more strict.
I want to this to be in effect for all mix tasks, and I do not want to have to pass the flag to the command, as this is easy to forget.
For example with a project with a compiler warning, this command should fail
mix clean && mix compile
As should this one
mix clean && mix test
Possible to some extent. There is a flag --warnings-as-errors
in elixirc
command.
☁ 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 you compile without the flag:
☁ 01_language [master] ⚡ elixirc useless.ex
useless.ex:2: warning: function another_userless/0 is unused
☁ 01_language [master] ⚡ echo $?
0
You get the return code as 0.
But when you compile with the flag --warnings-as-errors
, it returns an exit code of 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 compile script for break the build process.
这篇关于如果出现编译时警告,是否有可能使编译器提前退出,导致构建失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!