How to fix PlatformIO ESP8266/ESP32 fatal error: SPI.h: No such file or directory

Problem:

You are trying to compile your PlatformIO application for the ESP8266 or ESP32 but you’re seeing an error message like

In file included from .pio/libdeps/d1_mini/TFT_eSPI/TFT_eSPI.cpp:17:0:
.pio/libdeps/d1_mini/TFT_eSPI/TFT_eSPI.h:32:17: fatal error: SPI.h: No such file or directory

*************************************************************
* Looking for SPI.h dependency? Check our library registry!
*
* CLI  > platformio lib search "header:SPI.h"
* Web  > https://platformio.org/lib/search?query=header:SPI.h
*
*************************************************************

 #include <SPI.h>

This problem is common using the TFT_eSPI library.

Solution:

First, ensure that your platformio.ini has

framework = arduino

If you’re using a different frameworkSPI.h won’t be available since it’s a part of the Arduino framework !

Secondly, add this line to your platformio.ini:

lib_ldf_mode = deep+

and recompile your source code. This will reconfigure the library dependency finder (ldf) to find dependencies of dependency libraries:

Dependency Graph
|-- <TFT_eSPI> 2.3.52
|   |-- <SPI> 1.0