# Variable resolution

> Floe supports a three-source variable system that lets you inject environment-specific values into config files without editing the config itself.

Source: https://openlakeforge.dev/floe/guides/variables/

Floe supports a three-source variable system that lets you inject
environment-specific values into config files without editing the config
itself.

---

## Sources and precedence

Variables are collected from three sources and merged before any
placeholder substitution occurs.  When the same key appears in more than
one source the **highest-precedence** value wins:

```
config variables  >  env file variables  >  profile variables
    (highest)                                   (lowest)
```

| Source | Where defined | Precedence |
|--------|---------------|-----------|
| **Config variables** | `env.vars:` block in the main Floe YAML config | Highest |
| **Env file variables** | YAML map loaded from `env.file:` in the main Floe YAML config | Mid |
| **Profile variables** | `variables:` block in the environment profile | Lowest |

---

## Placeholder syntax

### In variable *values* — `${VAR}`

Variable values may themselves reference other variables using the
`${VAR}` syntax:

```yaml
# profiles/prod.yml
variables:
  BASE_PATH: /data/prod
  INCOMING:  ${BASE_PATH}/incoming   # expands to /data/prod/incoming
  OUTPUT:    ${BASE_PATH}/output     # expands to /data/prod/output
```

References are resolved recursively — a value may reference a variable
whose value also contains `${…}` placeholders — up to the built-in depth
limit (32 levels).

### In config fields — `{{VAR}}`

Once all variables are fully resolved, Floe substitutes them into the
main config using the `{{VAR}}` (double-brace) syntax:

```yaml
# config.yml
entities:
  - name: customers
    source:
      format: csv
      path: "{{INCOMING}}/customers.csv"
    sink:
      accepted:
        format: parquet
        path: "{{OUTPUT}}/customers.parquet"
```

---

## Resolution rules

1. **Merge** – build a single map from all three sources, higher-priority
   sources overwriting lower ones.
2. **Expand** – for every variable value, substitute `${KEY}` with the
   fully-resolved value of `KEY`, recursively.
3. **Fail-fast** – any placeholder that cannot be resolved or forms a cycle
   produces an immediate, actionable error before the run begins.

---

## Fail-fast errors

| Situation | Error (example) |
|-----------|-----------------|
| Reference to undefined variable | `variable "${MISSING}" is referenced but not defined` |
| Unclosed placeholder | `variable "PATH": unclosed placeholder in value '/data/${UNCLOSED'` |
| Empty placeholder | `variable "VAR": empty placeholder ${}` |
| Circular reference | `circular variable reference detected: A -> B -> A` |
| Depth limit exceeded | `variable expansion exceeded maximum depth (32)` |

---

## Examples

### Simple value override

```yaml
# profiles/dev.yml
variables:
  ENV:     dev
  CATALOG: dev_catalog

# profiles/prod.yml
variables:
  ENV:     prod
  CATALOG: prod_catalog
```

### Nested references

```yaml
variables:
  ROOT:     /mnt/data
  ENV:      prod
  BASE:     ${ROOT}/${ENV}       # → /mnt/data/prod
  INCOMING: ${BASE}/incoming     # → /mnt/data/prod/incoming
  OUTPUT:   ${BASE}/output       # → /mnt/data/prod/output
```

### Cross-source reference

A config variable can reference a value supplied by the profile:

```yaml
# profiles/prod.yml
variables:
  CATALOG: polaris_prod

# config.yml
env:
  vars:
    TABLE: "{{CATALOG}}.bronze.customers"
    # CATALOG comes from the profile at runtime
```

---

See also: [Environment profiles](https://openlakeforge.dev/floe/guides/profiles/) for the full profile schema.

---

<small>Synced from floe @ `v1.1.0` (`313b534`), [docs/guides/variables.md](https://github.com/OpenLakeForge/floe/blob/313b53441b3b088290403ed1adb2f3894833b6c5/docs/guides/variables.md).</small>
