How to handle "protocol has already been consolidated" compilation errors

I have an simple application that needs to do some JSON encoding from a MongoDB datastore. However, when I add the code below, I get this warning and the code never :

 defimpl Poison.Encoder, for: BSON.ObjectId do
   def encode(id, options) do
     BSON.ObjectId.encode!(id) |> Poison.Encoder.encode(options)
   end
 end

warning: the Poison.Encoder protocol has already been consolidated, an implementation for BSON.ObjectId has no effect

I don’t reference Poison.Encoder anywhere else in my code, but the warning is legit and indeed the code has no effect. I have two questions: what does the warning mean and how to prevent it.

You are consolidating the protocol at the wrong time. Where are you placing the code snippet above? Is it inside a function? In your test_helper.exs? Your protocol implementations must be the root of your files, usually not nested.

Hmm, interesting, this only happens when I type recompile at the iex prompt. A full quit/start cycle of iex doesn’t seem to have the problem. I guess the two are not equivalent?

Which Elixir version?

I happens on both 1.3.1 and 1.3.2

Since I ran into this using mongodb driver from @ericmj, I also logged an issue there with some example code:

Yes! Sounds like an Elixir bug indeed. Please open up a bug report in our
issues tracker as Eric said. :slight_smile:

José Valimwww.plataformatec.com.br
http://www.plataformatec.com.br/Founder and Director of R&D

See here: https://github.com/elixir-lang/elixir/issues/5228

2 Likes