LoginSignup
15
0

More than 1 year has passed since last update.

Elixirコンパイラのwarnings_as_errorsオプションを使って確実にコンパイルする

Last updated at Posted at 2022-11-28

Elixirコンパイラのwarnings_as_errorsオプションを有効にすると、 警告をエラーとして扱うことができ、コンパイル時に警告が発生した場合にコンパイルが失敗するようになります。

未使用の関数などの問題を確実に捕捉することができますし、コードの異常をより早く検知できます。

コマンドラインの例

mix compile --warnings-as-error

Mixプロジェクトの例

defmodule MyProject.MixProject do
  use Mix.Project

  #...

  def project do
    [
      elixirc_options: [warnings_as_errors: warnings_as_errors(Mix.env())]
    ]
  end

  defp warnings_as_errors(:test), do: false
  defp warnings_as_errors(_), do: true

  #...
end

test/test_helper.exsの例

例えばテスト環境のみで有効にしたい場合は、test/test_helper.exsCode.put_compiler_option/2を使用してコンパイラの設定を必要に応じて変更できます。

test_helper.exs
Code.put_compiler_option(:warnings_as_errors, true)

いくつかのNerves関連パッケージでtest/test_helper.exswarnings_as_errorsオプションを有効化しています。

DashbitではCIで活用しているようです。mix compile --warnings-as-errorsに加えて、mix deps.unlock --check-unusedmix format --check-formattedも使っているとのことです。

MIX_ENV=test mix deps.compile
MIX_ENV=test mix compile --warnings-as-errors
mix test --warnings-as-errors
mix deps.unlock --check-unused
mix format --check-formatted

ご参考までに

15
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
0