Skip to content

fix(ci): merge root platformio.ini build_flags into synthesised PIO env (#2664) - #2794

Merged
zackees merged 1 commit into
masterfrom
fix/platformio-merge-build-flags
Jun 5, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/platformio-merge-build-flags

Conversation

@zackees

@zackees zackees commented Jun 5, 2026 •

Copy link
Copy Markdown
Member

Closes #2664.

Summary

bash compile --backend platformio synthesises a fresh PIO project at .build/pio/<env>/platformio.ini from ci/boards.py and was silently discarding build_flags declared in the user's root platformio.ini. Anyone editing [env:<board>].build_flags at the project root reasonably expects those flags to take effect; without this merge they silently vanish, producing subtle "flags work under fbuild but not under --backend=platformio" bugs (e.g. a missing ESP32-C6 USB-CDC routing flag → silent device, ~1h debug for the issue reporter).

Option chosen: 1 (merge)

Per the issue's "Proposed fix" list, option 1 ("Merge root [env:<board>].build_flags into the synthesised PIO config") was straightforward to implement here because the codebase already has a PlatformIOIni parser (ci/compiler/platformio_ini.py) that resolves extends = and ${env:...} substitutions. No need to rewrite the synthesizer or change Board semantics.

Option 2 (extends = ../../../platformio.ini::env:<board>) was rejected: the synthesised file is written under .build/pio/<env>/ after package URLs have been rewritten to local file:// paths, and a hard extends pointing at the original root file would re-introduce raw URLs and reorder cache optimisation. Option 3 (docs only) wasn't necessary because option 1 was small.

Files changed

  • ci/compiler/build_config.py — new module-level helper get_root_platformio_build_flags(board_name, project_root). Reads the root platformio.ini, resolves inheritance via the existing PlatformIOIni parser, returns the matching env's build_flags (or [] for missing file / no match / parse error — never raises).
  • ci/compiler/pio.py — _init_platformio_build() calls the helper and appends the returned flags onto the cloned board's build_flags right before apply_board_specific_config() writes the synthesised ini. This sits next to the existing -Wl,-Map,… and -fopt-info-all=… augmentation block, which is the canonical place build_flags are extended for the synthesised env.
  • ci/tests/test_root_platformio_flag_merge.py — 5 unit tests covering: missing root file, no matching [env:<board>], direct flags, extends + ${env:...} substitution, malformed input.

Ordering / precedence

Board flags first, root flags appended. PlatformIO's "last wins" semantics for repeated -D / linker flags mean the root file effectively overrides the Board — which is what the issue reporter expects, since they're editing the obvious file.

Test plan

  • uv run pytest ci/tests/test_root_platformio_flag_merge.py ci/tests/test_board_to_platformio_ini.py -v → 8 passed
  • bash lint → all phases green (python_pipeline, cpp_linting, javascript_linting, meson_linting)
  • Manual repro by issue reporter: add -DSOME_FLAG=1 to [env:esp32c6] in root platformio.ini, run bash compile esp32c6 --examples AutoResearch --backend platformio, confirm SOME_FLAG appears in .build/pio/esp32c6/platformio.ini and in the compiled firmware.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features

    • Users can now specify custom build flags in the project's root platformio.ini configuration file, which will be properly resolved and merged during the build process.
  • Tests

    • Added comprehensive test coverage for build flag merging scenarios, including inheritance resolution and error handling.

bash compile --backend platformio synthesises a fresh PIO project at
.build/pio/<env>/platformio.ini from ci/boards.py and historically
ignored build_flags declared in the user's root platformio.ini. Anyone
editing [env:<board>].build_flags at the project root reasonably
expects those flags to take effect; without this merge they silently
vanish, producing "flags work under fbuild but not under platformio"
bugs (e.g. a missing ESP32-C6 USB-CDC routing flag → silent device).

This patch adds get_root_platformio_build_flags() in
ci/compiler/build_config.py, which reuses the existing PlatformIOIni
parser (handles extends + ${env:...} substitution) to extract the
matching env's flags. _init_platformio_build() in ci/compiler/pio.py
appends those flags to the cloned board's build_flags right before
apply_board_specific_config writes the synthesised ini.

Ordering: Board flags first, root flags appended — repeated -D's let
the root file override per PlatformIO's "last wins" semantics, which
matches the principle of least surprise for the issue's reporter.

A malformed or missing root ini returns [] and never blocks a build.

Tests: ci/tests/test_root_platformio_flag_merge.py covers the missing
file, no-matching-env, direct flags, extends inheritance with
${env:...} substitution, and malformed-input cases.

Closes #2664

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 5, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR resolves the silent discarding of root platformio.ini build flags during bash compile --backend platformio by adding extraction and merge logic. A new helper function reads the root config, resolves inheritance, and returns matching board flags, which are then appended to synthesized board configuration during build initialization.

Changes

Root platformio.ini build flags extraction and merge

Layer / File(s) Summary
Root build flags extraction function
ci/compiler/build_config.py
Introduces get_root_platformio_build_flags(board_name, project_root) that safely reads the root platformio.ini, locates and parses the matching [env:<board_name>] section via existing PlatformIOIni parser, resolves ${env:...} inheritance substitutions, and returns the resolved build_flags list. Returns empty list when the root file is missing or no matching environment exists; swallows parse/other exceptions by printing a warning and returning [] while re-raising KeyboardInterrupt.
PlatformIO build initialization integration
ci/compiler/pio.py
Imports the extraction function and calls it during synthesized board initialization to fetch root platformio.ini build flags. When root flags are found, logs the count and source environment, then appends them to board_with_sketch_include.build_flags so PlatformIO's "last wins" override semantics apply root flags over earlier board defaults.
Test suite validation
ci/tests/test_root_platformio_flag_merge.py
Comprehensive test coverage for get_root_platformio_build_flags() behavior: missing root file returns empty, no matching [env:<board>] returns empty, direct build_flags are extracted, extends-based inheritance with ${env:<base>.build_flags} substitution is resolved, and malformed ini content gracefully returns empty list. Includes helper method to write temporary platformio.ini test fixtures.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

A rabbit in the weeds of build-flags found,
Root configs read and resolved, safe and sound,
Inheritance chains now unwound so clear,
Flags flow through the pipeline without fear!
🐰 platformio.ini secrets, no longer lost—
Just appended at the end, whatever the cost! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: merging root platformio.ini build_flags into the synthesized PlatformIO environment for the CI/compiler subsystem.
Linked Issues check ✅ Passed The PR implementation matches issue #2664 objective 1: merge root platformio.ini [env:].build_flags into synthesized config via new helper function, import in pio.py, and comprehensive unit tests.
Out of Scope Changes check ✅ Passed All changes are directly scoped to implementing the selected fix: helper function, integration point, and unit tests. No extraneous modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/platformio-merge-build-flags

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
ci/compiler/pio.py (1)

1-4: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Fix file structure: shebang must be line 1.

The shebang #!/usr/bin/env python3 must be the first line of the file. Currently, an import appears before it, which violates Python file structure conventions and the coding guidelines for import ordering.

🔧 Proposed fix
-from ci.util.global_interrupt_handler import handle_keyboard_interrupt
-
-
 #!/usr/bin/env python3
 """
 PlatformIO Builder for FastLED
 
 Provides a clean interface for building FastLED projects with PlatformIO.
 """
 
+from ci.util.global_interrupt_handler import handle_keyboard_interrupt
+
 import gc
 import hashlib
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ci/compiler/pio.py` around lines 1 - 4, Move the shebang to the very first
line of the file and then place imports after it; specifically ensure
"#!/usr/bin/env python3" appears before the import of handle_keyboard_interrupt
(from ci.util.global_interrupt_handler import handle_keyboard_interrupt) and
that import ordering follows PEP8 after the shebang.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@ci/compiler/pio.py`:
- Around line 1-4: Move the shebang to the very first line of the file and then
place imports after it; specifically ensure "#!/usr/bin/env python3" appears
before the import of handle_keyboard_interrupt (from
ci.util.global_interrupt_handler import handle_keyboard_interrupt) and that
import ordering follows PEP8 after the shebang.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d4137d56-10db-4473-b014-7458ad626db9

📥 Commits

Reviewing files that changed from the base of the PR and between 787de45 and f5e7927.

📒 Files selected for processing (3)
  • ci/compiler/build_config.py
  • ci/compiler/pio.py
  • ci/tests/test_root_platformio_flag_merge.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

bash compile --backend platformio silently discards root platformio.ini build_flags

1 participant