Restoring default platform after using platform_packages to install custom one

I’ve had some good success using the platform_packages directive in platformio.ini to use a customized version of the framework-arduino-samd-adafruit package for a custom board I’m working on. That board has two SAMD51 CPUs on it, and I’ve got the platformio.ini set up with two environments which overwrite the default package from a local .zip file for each CPU, and platformIO is clever enough to overwrite the package from the zip file when I switch environments.

The problem I now face is that if I switch to an unrelated workspace & project, the custom library is still sitting in my C:\Users\Chris.platformio\packages folder, and that project doesn’t notice that it’s a customized library and fails to build. Is there a way for PlatformIO to automagically restore the default package? I’d like my custom workspace to not break other builds, particularly for other people in the company who might have any number of other workspaces & projects. My initial thought is to maybe have a post_build step which deletes the custom platform after every build, but that seems a bit slow and clunky.

My platformio.ini:

`; ---------------------------- Project Env ----------------------------
[env]
platform = atmelsam
framework = arduino
board_build.variants_dir = variants
debug_tool = atmel-ice
upload_protocol = atmel-ice
; ---------------------------------------------------------------------


; ---------------------------- Main MCU Env ---------------------------
[env:main]
board = cep-400-main
board_build.variant = cep-400-main

; cwp: override the original framework with modified startup.c code from a local .zip file, in the same
; directory as this platformio.ini file.
platform_packages = framework-arduino-samd-adafruit @ file://framework-arduino-samd-adafruit-ml-main-cpu.zip

build_src_filter = 
    ${env.src_filter} 
    -<data/*> 
    -<data_m8.cpp>

test_framework = unity
test_filter = main/*

lib_deps =
    ; Internal
    Wire
    SPI
    ; External 
    adafruit/Adafruit Zero DMA Library@^1.1.1
    vshymanskyy/TinyGSM@^0.11.7
    adafruit/SdFat - Adafruit Fork@^2.2.1
    adafruit/Adafruit SPIFlash@^4.2.0
    adafruit/RTClib@^2.1.1
    bblanchon/ArduinoJson @ ^6.21.3
    densaugeo/base64 @ ^1.4.0

; build at 0x8000 for use with SD card bootloader
board_upload.offset_address = 0x00008000
board_build.arduino.ldscript = flash_with_bootloader.ld

; ---------------------------- Data MCU Env ---------------------------
[env:data]
board = cep-400-data
board_build.variant = cep-400-data

; cwp: override the original framework with modified startup.c code from a local .zip file, in the same
; directory as this platformio.ini file.
platform_packages = framework-arduino-samd-adafruit @ file://framework-arduino-samd-adafruit-ml-data-cpu.zip
build_src_filter = 
    ${env.src_filter}
    -<main/*>
    -<data_m8.cpp>

test_framework = unity
test_filter = data/*
;test_port = /dev/ttyUSB0
test_port = com7
test_speed = 9600

lib_deps =
    ; Internal
    Wire
    SPI
    ; External  
    adafruit/Adafruit Zero DMA Library@^1.1.1
    sparkfun/SparkFun u-blox GNSS v3@^3.0.9
    sparkfun/SparkFun u-blox GNSS Arduino Library@^2.2.24
    bblanchon/ArduinoJson @ ^6.21.3
    mikalhart/TinyGPSPlus@^1.0.3
board_upload.offset_address = 0x00008000
board_build.arduino.ldscript = flash_with_bootloader.ld

build_unflags = '-DUSBCON'

I’ve noticed that too with overwritten packages, they “stick around” for the builds in other projects too, probably because the package specifications in regards to the version field in the package.json is also met for the other platform. Maybe changing it to a different version that is out-of-spec for the other platform makes PlatformIO do the proper revert? If not, I think it’s best if an issue was opened at Issues · platformio/platformio-core · GitHub.

Thanks for the suggestion @maxgerhardt, I will experiment with changing version numbers - perhaps my custom libraries can be one version lower than the default build?

Whatever does not satisfy the platform’s requirement per its package specification, e.g.,

matches all “1.7.X” versions, so you can go lower or higher than that in major (x) or minor version (y) (x.y.z)

@maxgerhardt, your suggestion seems to work - I have edited the package.json and platform.txt files in my zipped-up, custom versions of the framework-arduino-samd-adafruit package, changing the version number from 1.7.5 to 1.6.5. PlatfomIO does the right thing when building either environment in that project, and also restores the default package if I open an older project with no custom packages. I do see multiple folders in my .platformio/packages folder after this, like:

framework-arduino-samd-adafruit
framework-arduino-samd-adafruit@src-2760fda182f45c8a6c40cb2e32ac21ee
framework-arduino-samd-adafruit@src-929c6ee7d2233bbd999cdffe467b7034

but it is doing the right thing.

Thanks again for your feedback.