Skip to content

feat: Do not compile unused libraries listed in a sketch profile.#3144

Merged
cmaglie merged 4 commits into
arduino:masterfrom
cmaglie:exclude-unused-lib
Apr 2, 2026
Merged

feat: Do not compile unused libraries listed in a sketch profile.#3144
cmaglie merged 4 commits into
arduino:masterfrom
cmaglie:exclude-unused-lib

Conversation

@cmaglie

@cmaglie cmaglie commented Mar 26, 2026

Copy link
Copy Markdown
Member

Please check if the PR fulfills these requirements

See how to contribute

  • The PR has no duplicates (please search among the Pull Requests
    before creating one)
  • The PR follows
    our contributing guidelines
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)
  • UPGRADING.md has been updated with a migration guide (for breaking changes)
  • configuration.schema.json updated if new parameters are added.

What kind of change does this PR introduce?

When using a sketch profile, the libraries listed in the profile are always added for compilation by the Arduino CLI, even if the libraries are not actually used/included in the sketch.
This differs from the "classic" behavior (without the profiles), where the libraries are checked by the library discovery process and only compiled if actually used in the sketch, and this difference may lead to some problems like the one described in this issue.

This PR updates the library discovery process and allows the builder to detect if a library listed in the profile is not in use.

The change can be summarized as follows:

  1. The libraries listed in the sketch profile are added to the include path, but not yet in the compile queue; those libraries are put in a wait-list.
  2. The library discovery process proceeds as usual
  3. For each source file successfully examined, the builder checks the dependency file (.d) generated by the compiler
  4. If any of the files listed in the dep file belong to a library in the wait-list, then the library is taken out of the wait-list and added to the compile queue.

This hybrid approach should enable us to add the libraries listed in the profile quickly, without undergoing the slow full-discovery process, while also preventing us from compiling unnecessary libraries.

What is the current behavior?

The libraries listed in a profile are always compiled.

What is the new behavior?

The libraries listed in a profile are compiled only if used.

Does this PR introduce a breaking change, and is titled accordingly?

No

Other information

Fix #3085

@cmaglie cmaglie self-assigned this Mar 26, 2026
@cmaglie cmaglie added type: enhancement Proposed improvement topic: build-process Related to the sketch build process labels Mar 26, 2026
@cmaglie cmaglie force-pushed the exclude-unused-lib branch from d1feb7d to 6661cb4 Compare March 26, 2026 18:13
It could be possible that the compilation is unsuccessful, due to
preprocessor errors.
@codecov

codecov Bot commented Mar 27, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 69.36%. Comparing base (0d02d03) to head (bb528c5).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...rnal/arduino/builder/internal/detector/detector.go 88.88% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3144      +/-   ##
==========================================
+ Coverage   69.31%   69.36%   +0.04%     
==========================================
  Files         252      252              
  Lines       19406    19422      +16     
==========================================
+ Hits        13452    13472      +20     
+ Misses       4708     4706       -2     
+ Partials     1246     1244       -2     
Flag Coverage Δ
unit 69.36% <88.88%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
@cmaglie cmaglie requested a review from a team March 27, 2026 15:36
Comment thread internal/arduino/builder/internal/detector/detector.go Outdated
Comment thread internal/arduino/builder/internal/detector/detector.go Outdated
Comment thread internal/arduino/builder/internal/detector/detector.go Outdated
Comment thread internal/arduino/builder/internal/detector/detector.go Outdated
Comment thread internal/arduino/builder/internal/detector/detector.go Outdated
Comment thread internal/arduino/builder/internal/detector/detector.go Outdated
Comment thread internal/arduino/builder/internal/detector/detector.go Outdated
Comment thread internal/arduino/builder/internal/detector/detector.go Outdated
Comment thread internal/integrationtest/compile_5/profile_libs_test.go Outdated
Comment thread internal/integrationtest/compile_5/profile_libs_test.go Outdated
@cmaglie cmaglie merged commit da1d3eb into arduino:master Apr 2, 2026
102 checks passed
@cmaglie cmaglie deleted the exclude-unused-lib branch April 2, 2026 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: build-process Related to the sketch build process type: enhancement Proposed improvement

2 participants