Skip to content

feat(anvil): add [anvil] section to foundry.toml#13653

Open
koko1123 wants to merge 1 commit intofoundry-rs:masterfrom
koko1123:feat/anvil-config-toml
Open

feat(anvil): add [anvil] section to foundry.toml#13653
koko1123 wants to merge 1 commit intofoundry-rs:masterfrom
koko1123:feat/anvil-config-toml

Conversation

@koko1123
Copy link
Copy Markdown
Contributor

@koko1123 koko1123 commented Mar 6, 2026

Summary

Adds support for configuring Anvil via foundry.toml, matching the existing pattern used by [fmt], [doc], [fuzz], and other standalone sections. This closes a long-standing feature request to make Anvil project-aware.

  • New AnvilConfig struct in crates/config/src/anvil.rs with 40+ fields covering all persistent Anvil settings (network, accounts, gas, fork, tracing, etc.)
  • Registered as a standalone section in STANDALONE_SECTIONS, giving us [anvil], [profile.ci.anvil], and FOUNDRY_ANVIL_* env var support for free
  • Modified NodeArgs to load config and merge with CLI args (CLI always wins)
  • Changed 6 CLI fields from T to Option<T> (port, accounts, balance, slots_in_an_epoch, host, order) so config file defaults can apply when CLI flags aren't explicitly passed

Motivation

We're building a perpetual swap protocol at Strobe Labs that relies heavily on Anvil for testing (Prague EVM, gas_limit = u64::MAX, no_storage_caching, custom fork configs). Today, these settings live as CLI flags in our CI scripts, duplicating what's already partially expressed in foundry.toml. Having an [anvil] section lets us colocate all Foundry configuration in one file and leverage profiles for CI vs local development.

Example

[anvil]
port = 9545
hardfork = "prague"
gas_limit = "max"
no_storage_caching = true
auto_impersonate = true

[profile.ci.anvil]
accounts = 5

Design decisions

  • CLI precedence: CLI flags always override config values. Fields with clap default_value were changed to Option<T> so we can distinguish "user passed a flag" from "clap used its default"
  • Error surfacing: Invalid config values (bad host addresses, unknown order values, malformed TOML) produce clear errors instead of being silently ignored
  • Excluded fields: Session-specific flags (--load-state, --dump-state, --state, --config-out, --silent, --timestamp, --number) are intentionally excluded from the config struct since they don't make sense as persistent project configuration
  • Fork URL block selector: The @block suffix in fork URLs (e.g., http://localhost:8545@1400000) is preserved during merge, not dropped

Closes #4226
Ref #10779

Test plan

  • cargo build -p foundry-config compiles
  • cargo build -p anvil compiles
  • 148 existing config unit tests pass (no regressions)
  • 4 new tests: standalone section parsing, profile overrides, env var overrides, defaults
  • Manual: create foundry.toml with [anvil] section, run anvil, verify settings apply
  • Manual: verify CLI flags override config file values
  • Manual: verify FOUNDRY_ANVIL_PORT=1234 anvil works
  • Manual: verify [profile.ci.anvil] with FOUNDRY_PROFILE=ci works

@koko1123 koko1123 force-pushed the feat/anvil-config-toml branch 4 times, most recently from 093978c to 5c5d9f7 Compare March 10, 2026 22:44
Add support for configuring Anvil via foundry.toml, enabling teams to
version-control their Anvil settings alongside their project config.

This implements a new `[anvil]` section in foundry.toml that supports:
- All persistent Anvil settings (port, accounts, gas_limit, fork_url, etc.)
- Profile-based overrides via `[profile.ci.anvil]`
- Environment variable overrides via `FOUNDRY_ANVIL_*`
- CLI flags always take precedence over config file values

Implementation follows the established pattern used by `[fmt]`, `[doc]`,
and `[fuzz]` sections.

Closes foundry-rs#4226
Ref foundry-rs#10779
@koko1123 koko1123 force-pushed the feat/anvil-config-toml branch from 5c5d9f7 to 8f25bb3 Compare March 13, 2026 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Add [anvil] section to foundry.toml

2 participants