UndefinedEnvPlatformError: Please specify platform for 'esp' environment

UndefinedEnvPlatformError: Please specify platform for 'esp' environment

I’m receiving this error every time I run or test with platformio on an environment that is not the one mentioned in the error. I have two environments in my platformio.ini file, native and esp. The actual builds and tests run perfectly for both environments, but this error always shows when I run/test the native environment. A google search of "UndefinedEnvPlatformError" "platformio" returns only 3 results, none of which resolve this issue for me.

Any tips appreciated, thanks!

platformio version 6.1.15 running in the esphome/esphome docker container.

Please show the content of your platformio.ini

Here’s my .ini file. The project is an esphome external component, and this .ini file is for running tests. This file, and all other test files are located in a directory <external-component-dir>/test/.

The command I’m running is pio test --without-uploading -c test/platformio.ini -e native

[common]
lib_deps =
build_flags =
upload_flags =
;
[platformio]
description = ESPHome 2024.7.3
;
[env:native]
platform = native
test_framework = unity
build_flags =
    -fexceptions
    -std=gnu++17
    -D IS_NATIVE=1
build_unflags =
    -fno-exceptions
    -std=gnu++11
lib_compat_mode = off
; Loading order matters!
lib_deps =
    Croncpp = https://github.com/mariusbancila/croncpp.git
    ArduinoFake
    throwtheswitch/Unity@^2.5.2
    ${common.lib_deps}
build_src_filter =
    -<**/esphome/*>
test_ignore =
    esphome_tests
test_build_src = no
lib_ldf_mode = off
;
[env:esp]
board = esp32doit-devkit-v1
board_build.partitions = test/partitions.csv
board_upload.flash_size = 4MB
build_flags =
    -DESPHOME_LOG_LEVEL=ESPHOME_LOG_LEVEL_DEBUG
    -DUSE_ARDUINO
    -DUSE_ESP32
    -DUSE_ESP32_FRAMEWORK_ARDUINO
    -DUSE_ESP32_VARIANT_ESP32
    -Wno-sign-compare
    -Wno-unused-but-set-variable
    -Wno-unused-variable
    -fexceptions
    -fno-exceptions
    -std=gnu++17
    -I /esphome
    -D IS_NATIVE=0
build_unflags =
    -fno-exceptions
    -std=gnu++11
extra_scripts =
    post:test/post_build.py
framework = arduino
lib_deps =
    Croncpp=https://github.com/mariusbancila/croncpp.git
    Preferences
    FS
    esphome/noise-c@0.1.4
    bblanchon/ArduinoJson@6.18.5
    ${common.lib_deps}
build_src_filter =
    +<**/core/application.cpp>
    +<**/core/component.cpp>
    +<**/core/component_iterator.cpp>
    +<**/core/application.cpp>
    +<**/core/controller.cpp>
    +<**/core/entity_base.cpp>
    +<**/core/helpers.cpp>
    +<**/core/log.cpp>
    +<**/core/ring_buffer.cpp>
    +<**/core/string_ref.cpp>
    +<**/core/time.cpp>
    +<**/core/util.cpp>
    +<**/components/esp32/*.cpp>
    +<**/components/esphome/*.cpp>
    +<**/components/json/*.cpp>
    +<**/components/logger/*.cpp>
    +<**/components/md5/*.cpp>
    +<**/components/safe_mode/*.cpp>
    +<**/components/sntp/*.cpp>
    +<**/components/socket/*.cpp>
    +<**/components/switch/*.cpp>
    +<**/components/text/*.cpp>
    +<**/components/text_sensor/*.cpp>
    +<**/components/time/*.cpp>
test_ignore =
    esphome_tests
lib_ldf_mode = deep+
lib_compat_mode = off
platform = platformio/espressif32@^6.1.0
platform_packages =
    platformio/framework-arduinoespressif32@~3.20005.0
test_framework = unity
test_build_src = yes
test_port = boblobla

Thanks!

There are a few things in here to think about, but not related to the error you mentioned.

Can you post the whole error message?

But this is true: The esp environment doesn’t have the platform set, like

platform = espressif32

It is there, look at the 4th and 5th line from the end of the esp env:

Intersting. But I’ve never seen it written in author/name@^version like that. I would have written

platform = espressif32@^6.1.0

Me too :slight_smile: But it works… I’ve tested it and I think it’s a valid notation.

@sivar2311 and @maxgerhardt, thanks for having a look!

Unfortunately, the topic title is the entire error. I’ll paste the output below. I just noticed that this error only shows if I add the verbosity flag(s) -v to the pio test command. Without the flag(s), there are no errors printed to the output. Here are examples of both.

Running pio test with native environment, no verbosity flags:

Processing * in native environment
---------------------------------------------------------------------------------------------------------------
Building...
Testing...
test/main.cpp:116: test_schedule_receives_name	[PASSED]
test/main.cpp:117: test_schedule_receives_id	[PASSED]
test/main.cpp:118: test_schedule_receives_lambda	[PASSED]
test/main.cpp:119: test_schedule_contains_schedules	[PASSED]
test/main.cpp:120: test_schedule_calculates_cronnext	[PASSED]
test/main.cpp:121: test_schedule_cronNextExpired	[PASSED]
test/main.cpp:122: test_schedule_cronLoop	[PASSED]
------------------------------------- native:* [PASSED] Took 2.43 seconds -------------------------------------

=================================================== SUMMARY ===================================================
Environment    Test    Status    Duration
-------------  ------  --------  ------------
native         *       PASSED    00:00:02.434
================================== 7 test cases: 7 succeeded in 00:00:02.434 ==================================

Running pio test with native environment, one verbosity flag -v:

// I removed the bulk of the testing output here

[D][dynamic_cron]: setCronNext() 'test-name' to [1739098921, 2025-02-09 03:02:01], while crontab: 1 2 3 * * *, bypass: 0
test/main.cpp:122:test_schedule_cronLoop:PASS

-----------------------
7 Tests 0 Failures 0 Ignored 
OK

------------------------------------- native:* [PASSED] Took 1.17 seconds -------------------------------------
UndefinedEnvPlatformError: Please specify platform for 'esp' environment

Note that last line of the output. Also note that this error only appears when I run the native environment, not when I run the the esp environment.

Re my .ini file, I figured there would be other issues unrelated to this error. Some of the lines were copied verbatim from the esphome-generated platformio.ini file of a project that incorporates this external component (that’s where the platformio/espressif32@^6.1.0 came from). I’m happy to hear your thoughts and critique, thanks!

Hm… :thinking:

One thing I don’t have is the extra script. So i commented it out for my test.
Can you share the content of the test/post_build.py ?

Ah, yes! It didn’t occur to me that could be part of the issue. Here it is.

# Source https://github.com/letscontrolit/ESPEasy/pull/3845#issuecomment-1005864664

# pylint: disable=E0602
Import("env")  # noqa

import os
import shutil

if os.environ.get("ESPHOME_USE_SUBPROCESS") is None:
    try:
        import esptool
    except ImportError:
        env.Execute("$PYTHONEXE -m pip install esptool")
else:
    import subprocess
from SCons.Script import ARGUMENTS

# Copy over the default sdkconfig.
from os import path

if path.exists("./sdkconfig.defaults"):
    os.makedirs(".temp", exist_ok=True)
    shutil.copy("./sdkconfig.defaults", "./.temp/sdkconfig-esp32-idf")


def esp32_create_combined_bin(source, target, env):
    verbose = bool(int(ARGUMENTS.get("PIOVERBOSE", "0")))
    if verbose:
        print("Generating combined binary for serial flashing")
    app_offset = 0x10000

    new_file_name = env.subst("$BUILD_DIR/${PROGNAME}.factory.bin")
    sections = env.subst(env.get("FLASH_EXTRA_IMAGES"))
    firmware_name = env.subst("$BUILD_DIR/${PROGNAME}.bin")
    chip = env.get("BOARD_MCU")
    flash_size = env.BoardConfig().get("upload.flash_size")
    cmd = [
        "--chip",
        chip,
        "merge_bin",
        "-o",
        new_file_name,
        "--flash_size",
        flash_size,
    ]
    if verbose:
        print("    Offset | File")
    for section in sections:
        sect_adr, sect_file = section.split(" ", 1)
        if verbose:
            print(f" -  {sect_adr} | {sect_file}")
        cmd += [sect_adr, sect_file]

    cmd += [hex(app_offset), firmware_name]

    if verbose:
        print(f" - {hex(app_offset)} | {firmware_name}")
        print()
        print(f"Using esptool.py arguments: {' '.join(cmd)}")
        print()

    if os.environ.get("ESPHOME_USE_SUBPROCESS") is None:
        esptool.main(cmd)
    else:
        subprocess.run(["esptool.py", *cmd])


def esp32_copy_ota_bin(source, target, env):
    firmware_name = env.subst("$BUILD_DIR/${PROGNAME}.bin")
    new_file_name = env.subst("$BUILD_DIR/${PROGNAME}.ota.bin")

    shutil.copyfile(firmware_name, new_file_name)


# pylint: disable=E0602
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", esp32_create_combined_bin)  # noqa
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", esp32_copy_ota_bin)  # noqa

Ok, now i have several other errors as this scripts starts downloading some packages etc etc. I have never seen something like this before.

What is the goal here?
Did you write this?

All LDF modes beside of chain are more or less buggy / broken.
Change your project to work with chain in mode strict