You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I utilize SomeProtocol.__protocol__(:impls) to retrieve the implementations of a protocol in my code base. This is to provide an automatic mapping of protocols for certain features. It works great, except when the CodeReloader runs.
When the CodeReloader runs, the protocols in my app become unconsolidated and I have to restart the dev server.
To trigger the CodeReloader, I make an update to an Elixir file and then refresh my app frontend.
I know there's a mix.exs setting regarding consolidation, but it is not present in my mix.exs file.
Expected behavior
Reloading the code should not result in protocols not being consolidated.
Workaround
I have been struggling with a workaround for this but finally decided to figure something out for it. I ended up with the following (very hacky) solution that does seem to work.
Effectively, I'm running the same code as the CodeReloader to consolidate protocols, but I have it in force mode so it doesn't noop.
defmoduleSuper.Util.ProtocoldorequireLoggerdefimpls_for(protocol_mod,opts\\[])docaseprotocol_mod.__protocol__(:impls)do{:consolidated,impls}->impls:not_consolidated->ifKeyword.get(opts,:raise)doraise"Protocols not consolidated. This can happen in dev (restart server). In production, this is not expected."elserecompile_protocols(protocol_mod)impls_for(protocol_mod,Keyword.put(opts,:raise,true))endendendifMix.env()==:devdodefprecompile_protocols(mod)doLogger.info("Recompiling protocols due to missing consolidation: #{mod}")Mix.Task.reenable("compile.protocols")Mix.Task.run("compile.protocols",["--force"])endelsedefprecompile_protocols(_mod)do:okendendend
The text was updated successfully, but these errors were encountered:
Hi @sb8244! I cannot reproduce this issue. Here is what I did:
Cloned latest Phoenix
cd installer
mix new demo --dev
Added the following to lib/demo.ex:
defprotocol Foo do
def omg(foo)
end
Now I started the server and whenever I change a file, I reload the page, triggering the code reloader. Both Foo and Enumerable are still consolidated.
Besides using latest Phoenix (main), I am also using Elixir v1.17.0.
@josevalim I will see if I can reproduce it on a clean repo. I don't think we're doing anything crazy (other than inspecting the protocol), but it's also a decently complex repo so there's a chance something is throwing it off.
Environment
1.7.10
Mac OSX
Actual behavior
I utilize
SomeProtocol.__protocol__(:impls)
to retrieve the implementations of a protocol in my code base. This is to provide an automatic mapping of protocols for certain features. It works great, except when the CodeReloader runs.When the CodeReloader runs, the protocols in my app become unconsolidated and I have to restart the dev server.
To trigger the CodeReloader, I make an update to an Elixir file and then refresh my app frontend.
I know there's a mix.exs setting regarding consolidation, but it is not present in my mix.exs file.
Expected behavior
Reloading the code should not result in protocols not being consolidated.
Workaround
I have been struggling with a workaround for this but finally decided to figure something out for it. I ended up with the following (very hacky) solution that does seem to work.
Effectively, I'm running the same code as the CodeReloader to consolidate protocols, but I have it in
force
mode so it doesn't noop.The text was updated successfully, but these errors were encountered: