Search code examples
erlangelixirhex-pm

Elixir Decimal and :erlang_decimal


I'm using an erlang library called erlang_decimal to do some decimal math. My application also uses Decimal as a sub-dependency for Ecto, which is an Elixir library for basically the same kind of thing.

Here's my mix.exs:

  defp deps do
    [
      {:bcrypt_elixir, "~> 1.0"},
      {:comeonin, "~> 4.0"},
      {:configparser_ex, "~> 2.0"},
      {:csv, "~> 2.1.1"},
      {:decimal, "~> 1.0},
      {:ecto_enum, "~> 1.0"},
      {:ecto_sql, "~> 3.0"},
      {:erlang_decimal, "~> 0.4", app: false},
      {:ex_aws, "~> 2.0"},
      {:ex_aws_s3, "~> 2.0"},
      {:ex_machina, "~> 2.2", only: [:dev, :test]},
      {:ex_twilio, "~> 0.6.0"},
      {:gettext, "~> 0.11"},
      {:httpoison, "~> 1.0"},
      {:jason, "~> 1.0"},
      {:phoenix, "~> 1.4.0"},
      {:phoenix_ecto, "~> 4.0"},
      {:phoenix_html, "~> 2.10"},
      {:phoenix_live_reload, "~> 1.0", only: :dev},
      {:phoenix_pubsub, "~> 1.0"},
      {:plug, "~> 1.7"},
      {:plug_cowboy, "~> 2.0"},
      {:postgrex, ">= 0.0.0"},
      {:ranch, "~> 1.3"},
      {:sentry, "~> 7.0"},
      {:signed_overpunch, "~> 0.2"},
      {:sweet_xml, "~> 0.6"},
      {:telcom_parser, in_umbrella: true},
      {:timex, github: "bitwalker/timex", ref: "74afe810ee9c8e58e057830e62a865230ce00133"},
      {:trixie, in_umbrella: true}
    ]
  end

After deploying the change, I ran into the following exception:

  Elixir.UndefinedFunctionError: function Decimal.new/3 is undefined (module Decimal is not available)
    Module "Elixir.Decimal", in Decimal.new/3
    File "lib/postgrex/type_module.ex", line 713, in Postgrex.DefaultTypes."Elixir.Postgrex.Extensions.Numeric"/6
    File "lib/postgrex/protocol.ex", line 2733, in Postgrex.Protocol.rows_recv/4
    File "lib/postgrex/protocol.ex", line 1777, in Postgrex.Protocol.recv_execute/5
    File "lib/postgrex/protocol.ex", line 1652, in Postgrex.Protocol.bind_execute/4
    File "lib/db_connection/holder.ex", line 268, in DBConnection.Holder.holder_apply/4
    File "lib/db_connection.ex", line 1189, in DBConnection.run_execute/5
    File "lib/db_connection.ex", line 1276, in DBConnection.run/6
    Module "erlang", in :erlang.apply/2

Is there some sort of namespace conflict going on?

Edit:

Dialyzer is able to pick up on this issue:

Total errors: 6, Skipped: 0
done in 0m5.83s
:0:unknown_function
Function Decimal.compare/2 does not exist.
________________________________________________________________________________
:0:unknown_function
Function Decimal.div/2 does not exist.
________________________________________________________________________________
:0:unknown_function
Function Decimal.mult/2 does not exist.
________________________________________________________________________________
:0:unknown_function
Function Decimal.new/1 does not exist.
________________________________________________________________________________
:0:unknown_function
Function Decimal.round/3 does not exist.
________________________________________________________________________________
:0:unknown_function
Function Decimal.to_integer/1 does not exist.
________________________________________________________________________________
done (warnings were emitted)

I updated the mix.exs to include {:decimal, "~> 1.0"}


Solution

  • According to the Decimal docs that you linked:

    Add Decimal as a dependency in your mix.exs file.

    def deps do
      [{:decimal, "~> 1.0"}]
    end
    

    Yet, the mix.exs file you posted does not specify that dependency. In addition, the error message says:

    (module Decimal is not available)

    which also indicates that you need to add the Decimal module to your app.

    ==========

    After struggling to get erlang_decimal to work, I tried adding another dependency to mix.exs:

     defp deps do
        [
          {:decimal, "~> 0.4.3", hex: :erlang_decimal},
          {:decimal, "~> 1.6"}
    
        ]
    

    And this was the result:

    mix deps.get warning: the dependency :decimal is duplicated at the top level, please remove one of them Dependencies have diverged: * decimal (Hex package) different specs were given for the decimal app:

    In mix.exs: {:decimal, "~> 0.4.3", [env: :prod, repo: "hexpm", hex: "erlang_decimal"]}

    In mix.exs: {:decimal, "~> 1.6", [env: :prod, repo: "hexpm", hex: "decimal"]}

    Ensure they match or specify one of the above in your deps and set "override: true" ** (Mix) Can't continue due to errors on dependencies

    Then see:

    https://elixirforum.com/t/how-can-i-specify-multiple-dependencies-that-have-the-same-name/926

    and:

    https://elixirforum.com/t/working-with-modules-with-same-name/11364/6