Credo checks for Elixir code that breaks when its module is replaced by a hot code upgrade, with the node still running. They read one version of the source. They do not build or run anything.
Run the checks
Only these checks, with output to parse:
mix credo --only CredoHotUpgrades --format jsonIn an umbrella, run it from the root: one .credo.exs there covers every application.
The reasoning behind one warning:
mix credo explain lib/my_app/worker.ex:42Each entry of the JSON has check (the module, CredoHotUpgrades.Check.<Name>), filename, line_no, trigger and message.
The message ends with the fix.
Fix each warning this way
| Check | The problem | The fix |
|---|---|---|
CompileTimeEnv | @attr Application.get_env(...) freezes the value of the build machine | Read the value in a function, where it is used |
ClosureInState | fn or &local/1 kept in GenServer state, ETS, :persistent_term, the process dictionary or a message | Keep data or {module, function, args}, or &__MODULE__.name/arity |
TelemetryLocalHandler | :telemetry.attach given fn or &local/4 | Attach &__MODULE__.name/4 |
LoggerLocalFilter | :logger.add_primary_filter or add_handler_filter given fn or &local/2 | Pass {&__MODULE__.name/2, arg} |
ClosureInOptions | fn or &local/1 in a child spec or in an option a library keeps | Pass &__MODULE__.name/arity or {module, function, args} |
OnLoad | @on_load runs again each time an upgrade loads the module | Make the function safe to run more than once, or move the work to a supervised process |
LocalReceiveLoop | a receive loop that calls itself by its bare name, from a clause or from after | Recurse with __MODULE__.name(...) |
LocalAcceptLoop | an accept loop that calls itself by its bare name | Recurse with __MODULE__.name(...) |
ProcessDictionaryState | Process.put/2 in a GenServer callback | Keep the value in the state the callback returns |
ApplicationStartSideEffects | setup done in Application.start/2, which an upgrade does not run again | Do it in a supervised process, or expose a function an upgrade can call again |
NoNif | the module loads a NIF, with :erlang.load_nif/2, use Rustler or use Zig | No change to the code. A release that changes this module or its shared object is deployed in full |
SupervisorInitSideEffects | setup done in a supervisor's init/1, which an upgrade runs a second time | Do it in a child of the supervisor |
LiveViewClosure | fn or &local/1 kept in a LiveView socket by attach_hook, assign or a stream dom_id: | Use &__MODULE__.name/arity, or keep data and call the function where it is used |
CacheWithoutCodeChange | a GenServer fills :persistent_term or ETS and has no code_change/3 | Define code_change/3 and rebuild the cache in it |
Rules for the fixes
- A function captured as
&__MODULE__.name/aritymust be public: changedefptodef, and give it@doc falsewhen it is not part of the module's interface. &__MODULE__.name/arity,&Other.Module.name/arityand a capture of aKernelfunction such as&length/1are safe.&name/arity,fn ... endand&Other.Module.name(&1, extra)are not: the last one is an anonymous function of the module that writes it.- A capture needs the arguments the caller gives it. When the function needs more, keep
{module, function, extra_args}and call it withapply/3, or keep the extra data next to the capture. code_change/3is only called in a release where its module changed. Keep what decides the shape of a cache in the module that owns it.- Do not move a closure to another place the checks do not look at, a variable passed through two functions for instance. The upgrade fails the same way.
When a warning may be silenced
Silence one line, with the reason, only when the code never lives through an upgrade:
# A release task: it runs in a node of its own, which stops when it ends
# credo:disable-for-next-line CredoHotUpgrades.Check.ClosureInStateThat covers release tasks, Mix tasks, seeds and scripts, and a loop that ends after a fixed number of messages. Do not silence a warning to get a clean run, and do not disable a check for the whole project without asking the person who owns it.
Test files are already left out.
What these checks do not see
A clean run does not mean a release can be hot upgraded.
What needs two versions to compare is not checked here: a struct or a cached value that changes shape, a child added to a supervisor, a dependency that changes its processes.
A function passed as an attribute inside a ~H template is not seen either.
Build both releases and apply the upgrade to a running node for those, with relup-guard-elixir in CI.