Skip to content

fix(build-engine): hoist sketch #include directives above auto-generated prototypes (#1275) - #1289

Merged
zackees merged 1 commit into
mainfrom
fix/1275-ino-prototypes-above-includes
Aug 9, 2026
Merged

zackees merged 1 commit into
mainfrom
fix/1275-ino-prototypes-above-includes

Conversation

@zackees

@zackees zackees commented Aug 9, 2026 •

Copy link
Copy Markdown
Member

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:

error: 'CRGB' does not name a type; did you mean 'OCR0B'?

The generated output was:

#include <Arduino.h>
// Auto-generated function prototypes
CRGB kelvinToRGB(uint16_t k);      // <-- CRGB not declared yet

#line 1 src/main.ino
#include <FastLED.h>               // <-- declared here, too late

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 #include directive 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:

#include <Arduino.h>
#include <FastLED.h>               // <-- hoisted, now available
// Auto-generated function prototypes
CRGB kelvinToRGB(uint16_t k);      // <-- CRGB is now declared

#line 1 src/main.ino
                                   // <-- blank (was #include, line numbers preserved)
CRGB leds[8];

Changes

  • source_scanner.rs: New hoist_include_directives(), strip_include_directives(), and is_include_directive() helpers. Modified build_ino_prelude() to accept sketch includes between Arduino.h and prototypes. Modified preprocess_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

    • Improved .ino sketch processing so #include directives from all tabs are collected and placed consistently before generated prototypes.
    • Removed duplicate includes while preserving source line numbering.
    • Ensured required headers and generated prototypes work correctly with external header types.
  • Tests

    • Added coverage for include ordering, deduplication, line preservation, and prototype generation.

…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]>
@coderabbitai

coderabbitai Bot commented Aug 9, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The INO source scanner now hoists deduplicated #include directives into the generated prelude before prototypes. It replaces original include lines with blanks to preserve source numbering. Tests cover ordering, external types, deduplication, and line preservation.

Changes

INO include hoisting

Layer / File(s) Summary
Preprocessing and prelude generation
crates/fbuild-build-engine/src/source_scanner.rs
The scanner collects and deduplicates tab includes, preserves blank source lines, and emits includes before generated prototypes.
Include hoisting validation
crates/fbuild-build-engine/src/source_scanner/tests.rs
Tests verify include ordering, external-header prototypes, cross-tab deduplication, blank original lines, and source line numbering.

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
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes hoisting sketch include directives above generated prototypes.
Linked Issues check ✅ Passed The changes satisfy issue #1275 by hoisting, deduplicating, and line-preserving sketch includes before generated prototypes.
Out of Scope Changes check ✅ Passed The changes are limited to include preprocessing, prelude generation, and related tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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/1275-ino-prototypes-above-includes

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.

@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.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 380559e and 0a84277.

📒 Files selected for processing (2)
  • crates/fbuild-build-engine/src/source_scanner.rs
  • crates/fbuild-build-engine/src/source_scanner/tests.rs

Comment on lines +358 to +362
let sketch_includes = hoist_include_directives(&contents);
let stripped_contents: Vec<String> = contents
.iter()
.map(|c| strip_include_directives(c))
.collect();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 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.

@zackees
zackees merged commit 5effaea into main Aug 9, 2026
92 checks passed
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Aug 10, 2026
@zackees
zackees deleted the fix/1275-ino-prototypes-above-includes branch October 16, 2026 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

ino prototypes are still emitted above the sketch's #includes on 2.5.15 (#1196 still reproduces)

1 participant