VSCode; ESP8266 Arduino framework, intellisense fail to find header but compile ok

New to PlatformIO, developing a small school project recently. I added two configuration for two boards, one Arduino Uno, the other WeMos D1. Arduino one works fine, but IntelliSense went wrong with D1 source, when including <ESP8266WiFi.h>.
Later I found that IntelliSense failed to detect all ESP8266-specific builtins (WiFiClient.h, ESP8266mDNS.h…) but OK to detect Arduino builtins (Arduino.h, SPI.h…)
(FYI, the repo is available on GitHub)


(Sorry for error message in Chinese, which is “update includePath” and “cannot open source file” thing)

But the weird thing is that when hitting Build, compilation is success.

================================== [SUCCESS] Took 1.66 seconds ==================================

Environment    Status    Duration
-------------  --------  ------------
arduino        SUCCESS   00:00:00.628
d1_th          SUCCESS   00:00:01.664
================================== 2 succeeded in 00:00:02.292 ==================================

The code (d1_th_main.cpp):

#include <Arduino.h>
#include <ESP8266WiFi.h>

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  // put your main code here, to run repeatedly:
}

platformio.ini:

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[platformio]
; default_envs = arduino

[env]
lib_deps =
	SimpleDHT
	LinkedList
	git@github.com:45gfg9/RTCLib.git
src_filter = +<${PIOENV}_*.*>

[env:arduino]
platform = atmelavr
board = uno
framework = arduino

[env:d1_th]
platform = espressif8266
board = d1
framework = arduino
upload_port = /dev/cu.wchusbserial*

And I took a glance at c_cpp_properties.json:

{
    "configurations": [
        {
            "name": "!!! WARNING !!! AUTO-GENERATED FILE, PLEASE DO NOT MODIFY IT AND USE https://docs.platformio.org/page/projectconf/section_env_build.html#build-flags"
        },
        {
            "name": "Mac",
            "macFrameworkPath": [],
            "includePath": [
                "/Users/45gfg9/Documents/Project/SH/include",
                "/Users/45gfg9/Documents/Project/SH/src",
                "/Users/45gfg9/Documents/Project/SH/.pio/libdeps/arduino/RTCLib/src",
                "/Users/45gfg9/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
                "/Users/45gfg9/Documents/Project/SH/.pio/libdeps/arduino/LinkedList_ID443",
                "/Users/45gfg9/Documents/Project/SH/.pio/libdeps/arduino/SimpleDHT_ID849",
                "/Users/45gfg9/.platformio/packages/framework-arduino-avr/cores/arduino",
                "/Users/45gfg9/.platformio/packages/framework-arduino-avr/variants/standard",
                "/Users/45gfg9/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
                "/Users/45gfg9/.platformio/packages/framework-arduino-avr/libraries/HID/src",
                "/Users/45gfg9/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
                "/Users/45gfg9/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
                "/Users/45gfg9/.platformio/packages/tool-unity",
                ""
            ],
            "browse": {
                "limitSymbolsToIncludedHeaders": true,
                "path": [
                    "/Users/45gfg9/Documents/Project/SH/include",
                    "/Users/45gfg9/Documents/Project/SH/src",
                    "/Users/45gfg9/Documents/Project/SH/.pio/libdeps/arduino/RTCLib/src",
                    "/Users/45gfg9/.platformio/packages/framework-arduino-avr/libraries/Wire/src",
                    "/Users/45gfg9/Documents/Project/SH/.pio/libdeps/arduino/LinkedList_ID443",
                    "/Users/45gfg9/Documents/Project/SH/.pio/libdeps/arduino/SimpleDHT_ID849",
                    "/Users/45gfg9/.platformio/packages/framework-arduino-avr/cores/arduino",
                    "/Users/45gfg9/.platformio/packages/framework-arduino-avr/variants/standard",
                    "/Users/45gfg9/.platformio/packages/framework-arduino-avr/libraries/EEPROM/src",
                    "/Users/45gfg9/.platformio/packages/framework-arduino-avr/libraries/HID/src",
                    "/Users/45gfg9/.platformio/packages/framework-arduino-avr/libraries/SPI/src",
                    "/Users/45gfg9/.platformio/packages/framework-arduino-avr/libraries/SoftwareSerial/src",
                    "/Users/45gfg9/.platformio/packages/tool-unity",
                    ""
                ]
            },
            "defines": [
                "PLATFORMIO=40304",
                "ARDUINO_AVR_UNO",
                "F_CPU=16000000L",
                "ARDUINO_ARCH_AVR",
                "ARDUINO=10808",
                "__AVR_ATmega328P__",
                ""
            ],
            "intelliSenseMode": "clang-x64",
            "cStandard": "c11",
            "cppStandard": "c++11",
            "compilerPath": "/Users/45gfg9/.platformio/packages/toolchain-atmelavr/bin/avr-gcc",
            "compilerArgs": [
                "-mmcu=atmega328p",
                ""
            ]
        }
    ],
    "version": 4
}

Which you can see, no ESP8266 library folder.

I googled a few of similar threads, some of which advised to use lib_extra_dirs options. Didn’t try that out because 1) I’m not sure if that option works for IntelliSense and 2) If I include that this project may be platform-specific, which I don’t want to because I’ll never know if in some cases I’ll have to build on school PCs

Does anyone have idea or better solution?
HUGE appreciate.

PlatformIO and VSCode can only give you code completion for one environment, not two. What header is it supposed to show you when you ask for Arduino.h? framework-arduino-avr\cores\arduino\Arduino.h or framework-arduinoespressif8266\cores\esp8266\Arduino.h? Or the result of sizeof(int)? 2 for an Uno, but 4 for an ESP8266…

Uncomment the default_envs = d1_th line and execute Rebuild Intellisense index.

4 Likes

Ah, many thanks!
I changed that line to d1_th, arduino, now it worked.
I don’t know if this kind of workaround (putting two in that line) is a recommended or some strange tactics - yes, I did not take the environment into account before. But as far as I know that framework-arduinoespressif8266 framework is the superset of framework-arduino-avr framework, right? It does not get compilation involved, just for IntelliSense to work.

Since IntelliSense can only target one environment at a time, it’s usually the last one you built, and will only change when you change environments. So if you specifically build for arduino, intellisense will update/index for atmelavr. So when you then try to build d1_th, intellisense will be wrong (because the index is still for atmelavr), but once you’ve built d1_th1 (meaning espressif8266 is the active platform), intellisense should now be correct for that environment (and any atmelavr specific stuff will now be 'wrong'). If IntelliSense has been IntelliStupid, well, that's what the Rebuild IntelliSense` button is for.

btw, ‘Wrong environment’ type stuff will be a thing of the past with the VSCode 2.0 extension and PlatformIO 4.4, which will be released shortly… when you switch environments in the VSCode 2.0 extension, it forces a IntelliSense rebuild on the spot.

By doing default_envs = arduino, d1_th, you’ve simply told PlatformIO to always build both environments when you hit the ‘Build’ option.

framework-arduinoespressif8266 is the alternative of framework-arduino-avr, if that’s what you mean… one isn’t dependent on the other.

1 Like

Thank you so much for the explanation!

And sorry for using wrong words…XD, By framework I mean built-in libraries. You see, on Arduino side it has SPI, Wire and some more; on ESP8266 side it has not only all Arduino builtins but also its exclusive libraries (ESP8266WiFi etc.). So if I specify d1_th as the active env, code completion would be still available for Arduino code, but not vice versa. Is that correct?

1 Like

Ah, gotcha.

Yeah, the espressif8266 platform should have almost everything in the atmelavr one, so while there may be the odd variation in behaviour, having d1_th as the default/active env should work ok for both to make IntelliSense work properly.

1 Like