Skip to content

config: env-var ${VAR} substitution scans commented TOML lines, requires unused vars to be set #324

@obchain

Description

@obchain

Problem

Bringing up `charon` against `config/default.toml` aborts at startup with:

```
Error: failed to load config from config/default.toml
Caused by:
env var `CHARON_METRICS_AUTH_TOKEN` not set
```

The only reference to `CHARON_METRICS_AUTH_TOKEN` in the file is on line 88, which is a TOML comment:

```toml

Uncomment and export CHARON_METRICS_AUTH_TOKEN only if bind is non-loopback:

auth_token = "${CHARON_METRICS_AUTH_TOKEN}"

```

The config loader runs `${VAR}` substitution on the raw TOML string before parsing comments away, so a documentation-only example reference fires the missing-var error.

Impact

P2 — documentation references in commented-out config blocks become startup blockers. Any operator following the README on a fresh checkout has to either set every documented env var (defeating the purpose of the comment showing it as optional) or delete the example lines. Found during local-stack bring-up for the Grafana demo (PR #313).

Fix

Strip TOML comments before running env-var substitution. Concretely in `crates/charon-core/src/config.rs` (the substitution helper invoked by `Config::load_from_file`): split each line on the first un-quoted `#`, drop the comment portion, then run `${VAR}` substitution on the remaining content only. Commented lines containing example env-var references then no longer participate in substitution at all.

Add a regression test:

```rust
#[test]
fn substitution_ignores_commented_env_var_examples() {
let toml = r#"
# auth_token = "${UNSET_EXAMPLE_VAR}"
[bot]
min_profit_usd_1e6 = 1
"#;
// Should parse cleanly even though UNSET_EXAMPLE_VAR is not in env.
}
```

Severity

P2 — onboarding friction. Workaround is to export every documented env var even when the value is irrelevant.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinglayer:rustRust crates (core / scanner / protocols / executor / cli)priority:p2-polishNice-to-have / polishstatus:readyScoped and ready to pick up

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions