jeremyjh / dialyxir

Mix tasks to simplify use of Dialyzer in Elixir projects.
Apache License 2.0
1.69k stars 141 forks source link

Unknown error occurred: %Protocol.UndefinedError{description: "", protocol: String.Chars, value: {47, 1}} #444

Closed rlipscombe closed 8 months ago

rlipscombe commented 2 years ago

Precheck

Environment

Current behavior

Running mix dialyze on a combined Erlang and Elixir project, I get the following error on my Erlang modules:

Unknown error occurred: %Protocol.UndefinedError{description: "", protocol: String.Chars, value: {47, 1}}

I strongly suspect that it's due to the fact that the "line number" in Erlang BEAM files now contains the column number (cf https://github.com/erlang-lager/lager/issues/558), and this is tripping dialyxir up.

If I use --format raw, I don't get the error. If I use --format short, I see lib/dialyxir/formatter.ex:73: Dialyxir.Formatter.format_warning/2 in the stack trace.

rlipscombe commented 2 years ago

The following patch against 1.1.0 fixes it:

diff --git a/lib/dialyxir/formatter.ex b/lib/dialyxir/formatter.ex
index 6aa9c38..4034f29 100644
--- a/lib/dialyxir/formatter.ex
+++ b/lib/dialyxir/formatter.ex
@@ -70,7 +70,7 @@ defmodule Dialyxir.Formatter do
     warning = warning(warning_name)
     string = warning.format_short(arguments)

-    "#{base_name}:#{line}:#{warning_name} #{string}"
+    "#{base_name}:#{format_line(line)}:#{warning_name} #{string}"
   end

   defp format_warning(dialyzer_warning = {_tag, {file, line}, message}, :dialyxir) do
@@ -83,7 +83,7 @@ defmodule Dialyxir.Formatter do
         string = warning.format_long(arguments)

         """
-        #{base_name}:#{line}:#{warning_name}
+        #{base_name}:#{format_line(line)}:#{warning_name}
         #{string}
         """
       rescue
@@ -138,6 +138,9 @@ defmodule Dialyxir.Formatter do
     formatted <> String.duplicate("_", 80)
   end

+  defp format_line({line, col}), do: "#{line}:#{col}"
+  defp format_line(line), do: "#{line}"
+
   defp wrap_error_message(message, warning) do
     """
     Please file a bug in https://github.com/jeremyjh/dialyxir/issues with this message.