Path

Recognize direct and indirect circular dependencies in an Alloy configuration.

Estimated time: 1 min

Recognize circular dependencies

In the previous milestone, a reference created a dependency from prometheus.remote_write.local_prom to local.file.example. Alloy uses dependencies like that to decide which components to evaluate first, so those dependencies can’t form a circle.

A component can’t reference itself directly:

Alloy
// Invalid: direct self-reference.
local.file "self_reference" {
  filename = local.file.self_reference.content
}

Components also can’t reference each other in a loop:

Alloy
// Invalid: circular dependency.
local.file "a" {
  filename = local.file.b.content
}

local.file "b" {
  filename = local.file.a.content
}

Here, a depends on b, while b depends on a. Neither can be evaluated first.

When you read component references, follow each dependency in its direction. If following the references brings you back to a component you already visited, the configuration has a circular dependency and is invalid.

You can now explain why circular dependencies aren’t allowed: a component can’t reference itself directly or indirectly because Alloy needs a clear order for evaluating dependencies.


More to explore (optional)