fhunleth

fhunleth

Co-author of Nerves

Handling Elixir 1.17 compiler warnings on unused optional dependencies

I’m trying to reduce the number of Elixir 1.17 warnings in my projects, and I have questions with optional dependencies.

One example that triggers a new warning is Jason’s optional dependency on Decimal. If you create an empty project, add :jason to the dependencies and compile, you’ll see this:

==> jason
    warning: Decimal.new/1 is undefined (module Decimal is not available or is yet to be defined)
    │
 94 │         decimal.new(string)
    │                 ~
    │
    └─ (jason 1.4.3) lib/decoder.ex:94:17: Jason.Decoder.float_decode_function/1

warning: struct Decimal.Error is undefined (module Decimal.Error is not available or is yet to be defined)
└─ (jason 1.4.3) lib/decoder.ex: Jason.Decoder.float_decode_function/1

     warning: Decimal.to_string/2 is undefined (module Decimal is not available or is yet to be defined)
     │
 242 │     [?", decimal.to_string(value, :normal), ?"]
     │                  ~
     │
     └─ (jason 1.4.3) lib/encode.ex:242:18: Jason.Encode.struct/4

     warning: Decimal.to_string/1 is undefined (module Decimal is not available or is yet to be defined)
     │
 231 │     [?", decimal.to_string(value), ?"]
     │                  ~
     │
     └─ (jason 1.4.3) lib/encoder.ex:231:18: Jason.Encoder.Decimal.encode/2

There’s code that silenced a similar warning previously, but Elixir 1.17 is smarter. This is really cool that it can be detected, but I was wondering if there was preferred way of resolving warnings like this.

For example, placing the following compile-time checks around the Decimal-using functions would silence the Jason warnings:

if Code.ensure_loaded(Decimal) == {:module, Decimal} do
  def fun_that_uses_decimal() do
    ...
  end
end

Since this comes up with other libraries too, I’d like to be consistent and I’m not sure if there are subtle gotchas that this introduces.

Most Liked

LostKobrakai

LostKobrakai

Code.ensure_loaded?/2 has been what I’ve seen quite consistently being used around optional dependencies over the years, e.g.:

https://github.com/phoenixframework/phoenix_ecto/blob/main/lib/phoenix_ecto/html.ex#L1

https://github.com/kipcole9/money_sql/blob/v1.11.0/lib/money/ecto/money_ecto_composite_type.ex#L1

fhunleth

fhunleth

Co-author of Nerves

Perfect. Thank you.

Where Next?

Popular in Questions Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement