The real reason is that in Marlin/buildroot/share/PlatformIO/scripts/marlin.py the iterated environment is getting modified during the iteration in the replace_define() function, update it as follows and the error will be gone:
def replace_define(field, value):
found_define = None
for define in env['CPPDEFINES']:
if define[0] == field:
found_define = define
break
if found_define:
env['CPPDEFINES'].remove(found_define)
env['CPPDEFINES'].append((field, value))
I’m writing this, to help people facing this error, because sometimes (as I do now) you need to go back and compile an older version of Marlin to solve when a bug is introduced (bisect)…