Skip to content

Fix env var config roundtrip through build.rs TOML serialization#362

Merged
aram356 merged 4 commits intomainfrom
fix/env-var-config-roundtrip
Feb 24, 2026
Merged

Fix env var config roundtrip through build.rs TOML serialization#362
aram356 merged 4 commits intomainfrom
fix/env-var-config-roundtrip

Conversation

@aram356
Copy link
Copy Markdown
Collaborator

@aram356 aram356 commented Feb 23, 2026

Summary

Fix env var → build.rs → embedded TOML → runtime config pipeline so that TRUSTED_SERVER__INTEGRATIONS__* env vars preserve correct types through the roundtrip.

Changes

File Change
build.rs Replace collect_env_vars + rerun-if-env-changed with always-rerun sentinel; conditional output write; call from_toml_and_env
settings.rs Split from_toml (runtime, toml::from_str) / from_toml_and_env (build-time, config crate + normalize); add IntegrationSettings::normalize(); vec_from_seq_or_map bracket fallback; validate_no_trailing_slash replaces Publisher::normalize(); 3 new tests
Cargo.toml toml moved from [dev-dependencies] to [dependencies]

Closes

Closes #363
Closes #346

Test plan

  • cargo test --workspace — 367 tests pass
  • Existing temp_env tests updated to use from_toml_and_env
  • New test_env_var_roundtrip_normalizes_integration_types — full build.rs pipeline: env vars → from_toml_and_env → TOML serialize → from_toml → typed config
  • New test_from_toml_ignores_env_vars — runtime path does not read env vars
  • New validate_rejects_trailing_slash_in_origin_url — validation rejects trailing /

Checklist

  • Changes follow CLAUDE.md conventions
  • No unwrap() in production code — use expect("should ...")
  • New code has tests
  • No secrets or credentials committed

@aram356 aram356 force-pushed the fix/env-var-config-roundtrip branch 6 times, most recently from 3784e50 to ac2b267 Compare February 23, 2026 21:12
Three bugs caused env-var-sourced integration settings to lose their
types when build.rs serialized Settings to TOML:

1. collect_env_vars in build.rs skipped empty objects, arrays, and null
   values, so new env vars for those fields never triggered a rebuild.

2. vec_from_seq_or_map hard-failed on bracket-wrapped strings that
   aren't valid JSON (e.g. glob patterns like [/.static/prebid/{*rest}]).
   Now falls back to bracket-stripping + comma-splitting.

3. Env vars arrive as strings ("true", "1000") in the config crate's
   HashMap<String, JsonValue>. Without eager normalization, toml's
   serializer wrote them as TOML strings, so the runtime re-parse
   produced String("true") instead of Bool(true). Added
   IntegrationSettings::normalize() called from a new from_toml_and_env
   path used only by build.rs, keeping the runtime from_toml path clean.

Also replaced the publisher.origin_url trailing-slash normalization
with a validator that rejects trailing slashes at validation time.
@aram356 aram356 force-pushed the fix/env-var-config-roundtrip branch from ac2b267 to 543a89b Compare February 23, 2026 22:13
@aram356 aram356 mentioned this pull request Feb 23, 2026
5 tasks
- Remove Settings::new() which read raw source TOML instead of the
  build-output TOML, making it broken after the from_toml/from_toml_and_env
  split. The canonical runtime entry point is get_settings() in
  settings_data.rs.

- Move InsecureSecretKey and certificate_check guards from the removed
  Settings::new() into get_settings() where they belong.

- Add .validate() call in from_toml_and_env() so trailing-slash and
  other validation errors are caught at build time, not just at runtime.

- Add ends_with(']') guard to vec_from_seq_or_map bracket fallback to
  prevent incorrect slicing on malformed input like "[foo".

- Update docs and comments to reflect that env var overrides are
  build-time only (baked by build.rs), not runtime.

- Remove unused `use core::str` import and test_settings_new test.
Copy link
Copy Markdown
Collaborator

@ChristianPavilonis ChristianPavilonis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@aram356 aram356 merged commit 00a7f36 into main Feb 24, 2026
10 checks passed
@aram356 aram356 deleted the fix/env-var-config-roundtrip branch February 24, 2026 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix env var config roundtrip through build.rs TOML serialization Investigate and optimize get_settings() runtime overhead

2 participants