credo_hot_upgrades usage rules

Copy Markdown View Source

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 json

In 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:42

Each 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

CheckThe problemThe fix
CompileTimeEnv@attr Application.get_env(...) freezes the value of the build machineRead the value in a function, where it is used
ClosureInStatefn or &local/1 kept in GenServer state, ETS, :persistent_term, the process dictionary or a messageKeep data or {module, function, args}, or &__MODULE__.name/arity
TelemetryLocalHandler:telemetry.attach given fn or &local/4Attach &__MODULE__.name/4
LoggerLocalFilter:logger.add_primary_filter or add_handler_filter given fn or &local/2Pass {&__MODULE__.name/2, arg}
ClosureInOptionsfn or &local/1 in a child spec or in an option a library keepsPass &__MODULE__.name/arity or {module, function, args}
OnLoad@on_load runs again each time an upgrade loads the moduleMake the function safe to run more than once, or move the work to a supervised process
LocalReceiveLoopa receive loop that calls itself by its bare name, from a clause or from afterRecurse with __MODULE__.name(...)
LocalAcceptLoopan accept loop that calls itself by its bare nameRecurse with __MODULE__.name(...)
ProcessDictionaryStateProcess.put/2 in a GenServer callbackKeep the value in the state the callback returns
ApplicationStartSideEffectssetup done in Application.start/2, which an upgrade does not run againDo it in a supervised process, or expose a function an upgrade can call again
NoNifthe module loads a NIF, with :erlang.load_nif/2, use Rustler or use ZigNo change to the code. A release that changes this module or its shared object is deployed in full
SupervisorInitSideEffectssetup done in a supervisor's init/1, which an upgrade runs a second timeDo it in a child of the supervisor
LiveViewClosurefn 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
CacheWithoutCodeChangea GenServer fills :persistent_term or ETS and has no code_change/3Define code_change/3 and rebuild the cache in it

Rules for the fixes

  • A function captured as &__MODULE__.name/arity must be public: change defp to def, and give it @doc false when it is not part of the module's interface.
  • &__MODULE__.name/arity, &Other.Module.name/arity and a capture of a Kernel function such as &length/1 are safe. &name/arity, fn ... end and &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 with apply/3, or keep the extra data next to the capture.
  • code_change/3 is 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.ClosureInState

That 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.