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

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 or update_default values that come from a direct call to a time- or UUID-producing function, like default: DateTime.utc_now(). DSL options are ordinary expressions that Elixir evaluates while the module compiles, so the call runs exactly once and the result is baked into the resource as a literal: every record gets the timestamp of the last compile, and a default of Ash.UUID.generate() gives every record the same "unique" value.

Ash re-evaluates a default per record only when it is a zero-arity function or an MFA tuple; it uses any other value 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 arguments and calculation arguments resolve default the same way, so the check covers their defaults too. It doesn't flag values wrapped in a zero-arity fn or a capture; those forms are correct.

No other tool catches this bug: the code compiles without warnings, the frozen value type-checks, and no error is ever raised. In development each recompile refreshes the value, so the problem 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.