【问题标题】:Is it possible to get the compiler to exit early, failing the build, if a compile time warning is raised?如果引发编译时警告,是否可以让编译器提前退出,构建失败?
【发布时间】:2015-11-06 02:05:51
【问题描述】:

我发现编译时警告非常有用,但我偶尔会错过它们,尤其是在测试在 CI 服务器上运行的拉取请求中。

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

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

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

mix clean && mix compile

这个也应该

mix clean && mix test

【问题讨论】:

    标签: elixir elixir-mix


    【解决方案1】:

    在你的mix.exs

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

    然后mix compile 会将--warnings-as-errors 传递给compile.elixir 子任务。

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


    如果你不添加别名,你仍然可以运行mix compile --warnings-as-errors,它会做你期望的事情,但是mix test --warnings-as-errors 不会做你期望的事情,因为标志没有到达compile.elixir 任务。

    【讨论】:

    • 您也可以使用elixirc_options: [warnings_as_errors: true]
    • 如果你使用 mix 编译 Erlang 代码,你需要erlc_options: [:warnings_as_errors]
    【解决方案2】:

    在某种程度上是可能的。在elixirc 命令中有一个标志--warnings-as-errors

    ☁  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
    

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

    defmodule Useless do
      defp another_userless, do: nil
    end
    

    当你不带标志编译时:

    ☁  01_language [master] ⚡ elixirc useless.ex
    useless.ex:2: warning: function another_userless/0 is unused
    ☁  01_language [master] ⚡ echo $?
    0
    

    返回代码为 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
    

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

    【讨论】:

      【解决方案3】:

      我喜欢 Michael Stalker 的解决方案 here

      将警告视为错误总是在 TDD 期间可能会很烦人,您可能会在运行测试时快速重构代码。

      相反,您可以像这样在您的 Mix 环境中设置 --warnings-as-errors 标志:

      defmodule SomeProject.MixProject do
        use Mix.Project
      
        def project do
          [
            elixirc_options: [
              warnings_as_errors: treat_warnings_as_errors?(Mix.env())
            ]
            # ...
          ]
        end
      
        defp treat_warnings_as_errors?(:test), do: false
        defp treat_warnings_as_errors?(_), do: true
      end
      

      警告将在测试时被忽略,但对于 devprod 编译则不会。

      【讨论】:

        猜你喜欢
        • 2012-02-29
        • 2016-10-10
        • 2018-08-29
        • 2017-02-13
        • 2019-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多