AshCredo.Check.Warning.CompileTimeDefault (ash_credo v0.15.0)

Copy Markdown View Source

Basics

This check is disabled by default.

Learn how to enable it via .credo.exs.

This check has a base priority of high and works with any version of Elixir.

Explanation

Flags default/update_default values built by calling a time-or-uuid-producing function directly, like default: DateTime.utc_now(). DSL options are ordinary expressions evaluated while the module compiles, so the call runs exactly once and the resulting literal is baked into the resource: every record gets the timestamp of the last compile, and a Ash.UUID.generate() default hands every record the same "unique" value.

Ash only re-evaluates defaults per record when given a zero-arity function (or MFA tuple); any other value is used verbatim.

# Bad - frozen at compile time, identical for every record
attribute :scheduled_at, :utc_datetime, default: DateTime.utc_now()

# Good - re-evaluated for each record
attribute :scheduled_at, :utc_datetime, default: &DateTime.utc_now/0

Action and calculation arguments resolve default the same way, so their defaults are checked too. Values wrapped in a zero-arity fn or a capture are fine and are not flagged.

Nothing else catches this: it compiles without warnings, the frozen value type-checks, and no error is ever raised - dev recompiles keep the value looking fresh, and the freeze only shows up in production.

Warning.PinnedTimeInExpression is the sibling check for the same bug class inside Ash expressions (^DateTime.utc_now()).

Check-Specific Parameters

There are no specific parameters for this check.

General Parameters

Like with all checks, general params can be applied.

Parameters can be configured via the .credo.exs config file.