Repository: jeremyjh/dialyxir
Pull request #549 (2024-11-12 17:33:26)

fix: mix dialyzer crashes when a custom ignore file provided that doesn't match the default
Closes https://github.com/jeremyjh/dialyxir/issues/544


Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 7abb663..fa29246 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,9 @@ Versions follow [Semantic Versioning 2.0](https://semver.org/spec/v2.0.0.html)
 
 ## Unreleased changes post [1.4.4]
 
+   ### Fixed
+   - Crash when default ignore file missing and custom file specified
+
 ## [1.4.3] - 2023-12-28
   ### Fixed
   - Warnings with line & column.
diff --git a/lib/mix/tasks/dialyzer.ex b/lib/mix/tasks/dialyzer.ex
index ceca844..24c4a3c 100644
--- a/lib/mix/tasks/dialyzer.ex
+++ b/lib/mix/tasks/dialyzer.ex
@@ -188,7 +188,7 @@ defmodule Mix.Tasks.Dialyzer do
           """)
 
         ignore_warnings && File.exists?(ignore_warnings) &&
-            match?(%{size: size} when size == 0, File.stat!(default)) ->
+            match?(%{size: size} when size == 0, File.stat!(ignore_warnings)) ->
           info("""
           :ignore_warnings opt specified in mix.exs: #{ignore_warnings}, but file is empty.
           """)


Test patch:
diff --git a/test/fixtures/ignore_custom_empty/ignore_test.exs b/test/fixtures/ignore_custom_empty/ignore_test.exs
new file mode 100644
index 0000000..e69de29
diff --git a/test/fixtures/ignore_custom_empty/mix.exs b/test/fixtures/ignore_custom_empty/mix.exs
new file mode 100644
index 0000000..758a068
--- /dev/null
+++ b/test/fixtures/ignore_custom_empty/mix.exs
@@ -0,0 +1,16 @@
+defmodule IgnoreCustomEmpty.Mixfile do
+  use Mix.Project
+
+  def project do
+    [
+      app: :ignore_custom_empty,
+      version: "0.1.0",
+      prune_code_paths: false,
+      dialyzer: [
+        # this file is expected to not exist
+        ignore_warnings: "ignore_test.exs",
+        list_unused_filters: true
+      ]
+    ]
+  end
+end
diff --git a/test/fixtures/ignore_custom_missing/mix.exs b/test/fixtures/ignore_custom_missing/mix.exs
new file mode 100644
index 0000000..e4970ef
--- /dev/null
+++ b/test/fixtures/ignore_custom_missing/mix.exs
@@ -0,0 +1,16 @@
+defmodule IgnoreCustomMissing.Mixfile do
+  use Mix.Project
+
+  def project do
+    [
+      app: :ignore_custom_missing,
+      version: "0.1.0",
+      prune_code_paths: false,
+      dialyzer: [
+        # this file is expected to not exist
+        ignore_warnings: "ignore_test.exs",
+        list_unused_filters: true
+      ]
+    ]
+  end
+end
diff --git a/test/mix/tasks/dialyzer_test.exs b/test/mix/tasks/dialyzer_test.exs
index d606e1f..c1fff0f 100644
--- a/test/mix/tasks/dialyzer_test.exs
+++ b/test/mix/tasks/dialyzer_test.exs
@@ -77,4 +77,30 @@ defmodule Mix.Tasks.DialyzerTest do
     assert result =~
              "Unrecognized formatter foo received. Known formatters are dialyzer, dialyxir, github, ignore_file, ignore_file_strict, raw, and short. Falling back to dialyxir."
   end
+
+  test "task runs when custom ignore file provided and exists" do
+    in_project(:ignore, fn ->
+      fun = fn -> Mix.Tasks.Dialyzer.run(["--ignore-exit-status"]) end
+
+      assert capture_io(fun) =~ "ignore_warnings: ignore_test.exs"
+    end)
+  end
+
+  test "task runs when custom ignore file provided and does not exist" do
+    in_project(:ignore_custom_missing, fn ->
+      fun = fn -> Mix.Tasks.Dialyzer.run(["--ignore-exit-status"]) end
+
+      assert capture_io(fun) =~
+               ":ignore_warnings opt specified in mix.exs: ignore_test.exs, but file does not exist"
+    end)
+  end
+
+  test "task runs when custom ignore file provided and it is empty" do
+    in_project(:ignore_custom_empty, fn ->
+      fun = fn -> Mix.Tasks.Dialyzer.run(["--ignore-exit-status"]) end
+
+      assert capture_io(fun) =~
+               ":ignore_warnings opt specified in mix.exs: ignore_test.exs, but file is empty"
+    end)
+  end
 end