espressif / esptool

Espressif SoC serial bootloader utility
https://docs.espressif.com/projects/esptool
GNU General Public License v2.0
5.56k stars 1.38k forks source link

Important fatal errors should print to stderr (ESPTOOL-863) #981

Open egnor opened 5 months ago

egnor commented 5 months ago

Operating System

(relevant to most)

Esptool Version

4.7 (applicable to all)

Python Version

3.12.3 (applicable to all)

Full Esptool Command Line that Was Run

No response

Esptool Output

when pyserial is not installed, you see on STDERR (if stdout is filtered):

Traceback (most recent call last):
  File "/home/egnor/source/gadget_play/pintest/build/arduino/internal/esp32_esptool_py_4.5.1_7c51dd92fc1b703e/esptool.py", line 31, in <module>
    import esptool
  File "/home/egnor/source/gadget_play/pintest/build/arduino/internal/esp32_esptool_py_4.5.1_7c51dd92fc1b703e/esptool/__init__.py", line 41, in <module>
    from esptool.cmds import (
  File "/home/egnor/source/gadget_play/pintest/build/arduino/internal/esp32_esptool_py_4.5.1_7c51dd92fc1b703e/esptool/cmds.py", line 14, in <module>
    from .bin_image import ELFFile, ImageSegment, LoadFirmwareImage
  File "/home/egnor/source/gadget_play/pintest/build/arduino/internal/esp32_esptool_py_4.5.1_7c51dd92fc1b703e/esptool/bin_image.py", line 14, in <module>
    from .loader import ESPLoader
  File "/home/egnor/source/gadget_play/pintest/build/arduino/internal/esp32_esptool_py_4.5.1_7c51dd92fc1b703e/esptool/loader.py", line 30, in <module>
    import serial
ModuleNotFoundError: No module named 'serial'

What is the Expected Behaviour?

esptool should print the more-helpful error message on STDERR (which currently only goes to STDOUT):

Pyserial is not installed for [python binary]. Check the README for installation instructions.

More Information

Some fatal errors also print important messages the user should see https://github.com/espressif/esptool/blob/4394a658c40b73cc6d593797a42b665e68f84cf7/esptool/loader.py#L35 https://github.com/espressif/esptool/blob/4394a658c40b73cc6d593797a42b665e68f84cf7/esptool/loader.py#L61

However, these print() to stdout, and in many cases (e.g. arduino-cli), esptool is run with stdout suppressed, so all you see is the native Python stack trace error.

Probably instead of print() and raise, these should raise an error with the error message, the way this does: https://github.com/espressif/esptool/blob/4394a658c40b73cc6d593797a42b665e68f84cf7/esptool/loader.py#L44

Other Steps to Reproduce

No response

radimkarnis commented 5 months ago

Hello @egnor, thank you for the suggestion!

We are already tracking the request to send error messages to stderr (see e.g. https://github.com/espressif/esptool/issues/888). It will be implemented as a part of the next major release (v5.0) since it is a breaking change for current scripts relying on reading the stdout output.