Wire.h and other core libraries not found during build process

I'm continually getting the error:

fatal error: Wire.h: No such file or directory
#include <Wire.h>
          ^~~~~~~~

while trying to use Wire.h and other Arduino libraries located in packages/arduino/hardware/samd/1.8.6/libraries.

I've tried to create a containerized environment for my project so my arduino-cli.yaml looks like this:

board_manager:
  additional_urls: []
daemon:
  port: "50051"
directories:
  data: ./.arduino15
  downloads: ./.arduino15/staging
  user: ./
logging:
  file: ""
  format: text
  level: info
telemetry:
  addr: :9090
  enabled: true

and I consider my top level directory to be my user directory with hardware and libraries directories there. All other Arduino core files seem to be recognized correctly with this setup and I've temporarily been able to fix it by just copying over those core libraries to my user libraries directory.

Here is the output of my build process:

Using board 'adafruit_feather_m4' from platform in folder: C:\Users\micro\Desktop\primary\hardware\adafruit\samd
Using core 'arduino' from platform in folder: C:\Users\micro\Desktop\primary\hardware\adafruit\samd
Detecting libraries used...
"C:\\Users\\micro\\Desktop\\primary\\.arduino15\\packages\\arduino\\tools\\arm-none-eabi-gcc\\7-2017q4/bin/arm-none-eabi-g++" -mcpu=cortex-m4 -mthumb -c -g -Os -w -std=gnu++11 -ffunction-sections -fdata-sections -fno-threadsafe-statics -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions "-D__SKETCH_NAME__=\"\"\"main.ino\"\"\"" -w -x c++ -E -CC -DF_CPU=120000000L -DARDUINO=10607 -DARDUINO_FEATHER_M4 -DARDUINO_ARCH_SAMD -D__SAMD51J19A__ -DADAFRUIT_FEATHER_M4_EXPRESS -D__SAMD51__ -DUSB_VID=0x239A -DUSB_PID=0x8022 -DUSBCON -DUSB_CONFIG_POWER=100 "-DUSB_MANUFACTURER=\"Urban Sky LLC\"" "-DUSB_PRODUCT=\"Urban Sky Monarch\"" "-IC:\\Users\\micro\\Desktop\\primary\\hardware\\adafruit\\samd\\cores\\arduino/TinyUSB" "-IC:\\Users\\micro\\Desktop\\primary\\hardware\\adafruit\\samd\\cores\\arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore" "-IC:\\Users\\micro\\Desktop\\primary\\hardware\\adafruit\\samd\\cores\\arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore/tinyusb/src" -D__FPU_PRESENT -DARM_MATH_CM4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -DENABLE_CACHE -Os -DVARIANT_QSPI_BAUD_DEFAULT=50000000 -D__SAMD51J19A__ -DADAFRUIT_FEATHER_M4_EXPRESS -D__SAMD51__ -DUSB_VID=0x239A -DUSB_PID=0x8022 -DUSBCON -DUSB_CONFIG_POWER=100 "-DUSB_MANUFACTURER=\"Urban Sky LLC\"" "-DUSB_PRODUCT=\"Urban Sky Monarch\"" "-IC:\\Users\\micro\\Desktop\\primary\\hardware\\adafruit\\samd\\cores\\arduino/TinyUSB" "-IC:\\Users\\micro\\Desktop\\primary\\hardware\\adafruit\\samd\\cores\\arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore" "-IC:\\Users\\micro\\Desktop\\primary\\hardware\\adafruit\\samd\\cores\\arduino/TinyUSB/Adafruit_TinyUSB_ArduinoCore/tinyusb/src" -D__FPU_PRESENT -DARM_MATH_CM4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 "-IC:\\Users\\micro\\Desktop\\primary\\.arduino15\\packages\\arduino\\tools\\CMSIS\\4.5.0/CMSIS/Include/" "-IC:\\Users\\micro\\Desktop\\primary\\.arduino15\\packages\\arduino\\tools\\CMSIS-Atmel\\1.2.0/CMSIS/Device/ATMEL/" "-IC:\\Users\\micro\\Desktop\\primary\\hardware\\adafruit\\samd\\cores\\arduino" "-IC:\\Users\\micro\\Desktop\\primary\\hardware\\adafruit\\samd\\variants\\feather_m4" "C:\\Users\\micro\\AppData\\Local\\Temp\\arduino-sketch-FAD58DE7366495DB4650CFEFAC2FCD61\\sketch\\main.ino.cpp" -o nul -DARDUINO_LIB_DISCOVERY_PHASE

Has anyone else experienced this and does anything here look fishy? Thanks!

I just gave it a try with your exact configuration and FQBN and had no such error.

But I'm curious about this statement:

fostac:
while trying to use Wire.h and other Arduino libraries located in packages/arduino/hardware/samd/1.8.6/libraries.

The libraries bundled with each boards platform (e.g., Adafruit SAMD Boards, Arduino SAMD Boards) are only accessible when you are compiling for a board of that platform. So if you're compiling for adafruit:samd:adafruit_feather_m4, then the libraries in packages/arduino/hardware/samd/1.8.6/libraries won't be in the include path, but the libraries in packages/adafruit/hardware/samd/1.5.14/libraries will be in the include path.

So it's not surprising that you would get a "No such file or directory" error for an #include directive for a file in one of the Arduino SAMD Boards platform's bundled libraries when compiling for a board of another platform, except for the fact that Adafruit SAMD Boards platform bundles copies of all the same libraries bundled with the Arduino SAMD Boards platform, including the Wire library.

Does the file ./.arduino15/packages/adafruit/hardware/samd/1.5.14/libraries/Wire/Wire.h exist?

fostac:
I've temporarily been able to fix it by just copying over those core libraries to my user libraries directory.

There is an issue you might run into from this sort of workaround, because each platform has its own version of the Wire library. Even though the API is the same, the underlying implementation is architecture-specific.

That works fine when the library is bundled with the platform, because then the right Wire library is always used no matter which board you are compiling for, but if you move the library to the user directory, then it can be accessed no matter which board you are compiling for, so you might end up compiling the Wire library in the user directory for a board it's not compatible with, rather than the bundled version of the library in the board's platform. Arduino CLI does have a mechanism where the library which has the best match to the architecture of the board you're compiling for will get priority when there are multiple libraries that match the #include directive, which will likely prevent the issue in this specific case, but I've seen cases where people ended up with a lot of confusion after copying the platform bundled libraries to the user directory.

pert:
So if you're compiling for adafruit:samd:adafruit_feather_m4, then the libraries in packages/arduino/hardware/samd/1.8.6/libraries won't be in the include path, but the libraries in packages/adafruit/hardware/samd/1.5.14/libraries will be in the include path.

I'm thinking I had a misunderstanding of the include path's scope and how the build process works. This makes much more sense now, thank you!

pert:
Does the file ./.arduino15/packages/adafruit/hardware/samd/1.5.14/libraries/Wire/Wire.h exist?

No, it was missing and I had assumed that was because it inherited the arduino:samd core libraries.

Thanks for your help! This really just leaves me with the question of what is the best option for adding my own SAMD51 board variant? As I understand it now, there isn't a way to have a variant defined in my user/hardware directory while referencing an installed arduino core located somewhere like /.arduino15/packages/adafruit/hardware/samd/. Does this mean all those core files need to be included in my user hardware directory along with my variant?

fostac:
This makes much more sense now, thank you!

You're welcome. I'm very glad if I'm able to be of assistance. This sort of thing is pretty confusing, but I've had a good deal of experience messing around with it by now.

fostac:
As I understand it now, there isn't a way to have a variant defined in my user/hardware directory while referencing an installed arduino core located somewhere like /.arduino15/packages/adafruit/hardware/samd/. Does this mean all those core files need to be included in my user hardware directory along with my variant?

No, you can reference it if you like. There is documentation on this here:
https://arduino.github.io/arduino-cli/latest/platform-specification/#referencing-another-core-variant-or-tool
So your custom platform could consist of as little as a boards.txt under ./hardware/myvendorname/samd, which references its core, variant, and tools from Adafruit SAMD Boards. Of course, you can have more than that in your custom platform and reference less from other platforms if you like.

One thing I didn't mention in my previous reply is that if your custom platform references the core of another platform, then the bundled libraries of that platform do become available when compiling for a board of your platform. That's not the case with Adafruit SAMD Boards and Arduino SAMD Boards because Adafruit SAMD Boards doesn't reference the Arduino SAMD Boards core (it actually doesn't reference anything at all from that platform now, so Arduino SAMD Boards is no longer a dependency of Adafruit SAMD Boards).

pert:
So your custom platform could consist of as little as a boards.txt under ./hardware/myvendorname/samd, which references its core, variant, and tools from Adafruit SAMD Boards. Of course, you can have more than that in your custom platform and reference less from other platforms if you like.

Exactly what I was looking for! Thanks again, I'm much happier with how everything is configured now.

Juraj:
samd21g - Adding a custom board to the Arduino IDE - Arduino Stack Exchange

I was getting caught on using custom.build.core=adafruit:samd instead of custom.build.core=adafruit:arduino before reviewing this - thank!