Repository navigation
fix(build-engine): hoist sketch #include directives above auto-generated prototypes (#1275) - #1289
Conversation
…ted prototypes (#1275) When a .ino sketch includes library headers (e.g. #include <FastLED.h>) and defines a helper whose return/parameter type comes from that header, the auto-generated function prototype referenced types not yet declared. This matches arduino-cli's behavior: prototypes are inserted after the include block, not before it. The fix extracts all #include directives from .ino tabs, deduplicates them, hoists them into the prelude between Arduino.h and the prototype block, and replaces each hoisted line with a blank line in the body to preserve line numbering for diagnostics. Fixes #1275. Co-Authored-By: Claude <[email protected]>
📝 WalkthroughWalkthroughThe INO source scanner now hoists deduplicated ChangesINO include hoisting
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant INOFiles
participant IncludeCollector
participant PreludeBuilder
participant GeneratedSource
INOFiles->>IncludeCollector: collect and deduplicate tab includes
IncludeCollector-->>INOFiles: includes and blanked tab contents
INOFiles->>PreludeBuilder: build prelude with sketch includes
PreludeBuilder->>GeneratedSource: emit includes before prototypes
INOFiles->>GeneratedSource: emit blanked tab contents
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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.
Inline comments:
In `@crates/fbuild-build-engine/src/source_scanner.rs`:
- Around line 358-362: Update hoist_include_directives and
strip_include_directives to use preprocessing-aware parsing rather than
text-prefix matching, recognizing spaced directives such as “# include
<Header.h>”. Hoist only active, unconditional includes, or retain the
surrounding `#if/`#ifdef context so conditional headers remain conditional; ignore
comment and raw-string text. Add regression coverage for disabled branches,
comments, raw strings, and spaced directives.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: cbac63b7-9312-4fc8-84cf-1af4b5e62860
📒 Files selected for processing (2)
crates/fbuild-build-engine/src/source_scanner.rscrates/fbuild-build-engine/src/source_scanner/tests.rs
| let sketch_includes = hoist_include_directives(&contents); | ||
| let stripped_contents: Vec<String> = contents | ||
| .iter() | ||
| .map(|c| strip_include_directives(c)) | ||
| .collect(); |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Preserve preprocessor semantics before hoisting includes.
Line 798 classifies a directive only by a text prefix. This can hoist #include text from a block comment or raw string into an active directive. It also moves an include out of #if or #ifdef control, so an optional header becomes unconditional.
Use preprocessing-aware parsing. Hoist only active, unconditional include directives, or preserve each directive with its required conditional context. Support valid spaced directives such as # include <Header.h>. Add regression tests for disabled branches, comments, raw strings, and spaced directives.
Also applies to: 790-828
🤖 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 `@crates/fbuild-build-engine/src/source_scanner.rs` around lines 358 - 362,
Update hoist_include_directives and strip_include_directives to use
preprocessing-aware parsing rather than text-prefix matching, recognizing spaced
directives such as “# include <Header.h>”. Hoist only active, unconditional
includes, or retain the surrounding `#if/`#ifdef context so conditional headers
remain conditional; ignore comment and raw-string text. Add regression coverage
for disabled branches, comments, raw strings, and spaced directives.
Problem
When a .ino sketch
#includes library headers (e.g.#include <FastLED.h>) and defines a helper function whose return/parameter type comes from that header, the auto-generated function prototype is placed before the#include, causing a compile error:The generated output was:
This is the same class of bug as #1196 (sketch-defined types), but for types from included library headers. The #1196 fix correctly suppresses prototypes referencing sketch-defined types, but doesn't help when the type is externally declared.
Fix
Hoist every
#includedirective from the sketch tabs into the prelude, between#include <Arduino.h>and the prototype block. Replace each hoisted line with a blank line in the body to preserve line numbering for diagnostics.Generated output now:
Changes
source_scanner.rs: Newhoist_include_directives(),strip_include_directives(), andis_include_directive()helpers. Modifiedbuild_ino_prelude()to accept sketch includes between Arduino.h and prototypes. Modifiedpreprocess_ino_files()to extract/strip includes and pass stripped contents to prelude writers.tests.rs: Three new tests covering include hoisting order, deduplication, and line-number preservation.Testing
Fixes #1275.
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
.inosketch processing so#includedirectives from all tabs are collected and placed consistently before generated prototypes.Tests