Repository: conan-io/conan
Pull request: Exception errors to stderr
Changelog: Feature: Print errors and warnings to `stderr`
Docs: omit

Close #5207

Problem statement:
stderr not used by Conan
At least, the exception messages printed in the `command.py` should go to stderr. This is important to improve the CI automation feedback on errors.

Maybe we should have the stderr stream on the conan output and print there also the .error messages

Hints from issue discussion:

Commit message: Exception errors to stderr

Patch to review:
diff --git a/conans/client/command.py b/conans/client/command.py
index a1085b3985d..585e64914ec 100644
--- a/conans/client/command.py
+++ b/conans/client/command.py
@@ -1711,19 +1711,23 @@ def run(self, *args):
             if exc.code != 0:
                 logger.error(exc)
                 self._user_io.out.error("Exiting with code: %d" % exc.code)
+                sys.stderr.write("Exiting with code: %d" % exc.code)
             ret_code = exc.code
         except ConanInvalidConfiguration as exc:
             ret_code = ERROR_INVALID_CONFIGURATION
             self._user_io.out.error(exc)
+            sys.stderr.write(str(exc))
         except ConanException as exc:
             ret_code = ERROR_GENERAL
             self._user_io.out.error(exc)
+            sys.stderr.write(str(exc))
         except Exception as exc:
             import traceback
             print(traceback.format_exc())
             ret_code = ERROR_GENERAL
             msg = exception_message_safe(exc)
             self._user_io.out.error(msg)
+            sys.stderr.write(msg)
 
         return ret_code
 


Code review comments:
diff_hunk: @@ -1711,19 +1711,23 @@ def run(self, *args):
             if exc.code != 0:
                 logger.error(exc)
                 self._user_io.out.error("Exiting with code: %d" % exc.code)
+                sys.stderr.write("Exiting with code: %d" % exc.code)
             ret_code = exc.code
         except ConanInvalidConfiguration as exc:
             ret_code = ERROR_INVALID_CONFIGURATION
             self._user_io.out.error(exc)
+            sys.stderr.write(str(exc))
         except ConanException as exc:
             ret_code = ERROR_GENERAL
             self._user_io.out.error(exc)
+            sys.stderr.write(str(exc))
path: conans/client/command.py
text: @user2:
This duplicates errors in users output:
```bash
$ conan install zlib/1.2.11@user1/stable -s os=AIX
ERROR: Invalid setting 'AIX' is not a valid 'settings.os' value.
Possible values are ['Android', 'Arduino', 'FreeBSD', 'Linux', 'Macos', 'SunOS', 'Windows', 'WindowsStore', 'iOS', 'tvOS', 'watchOS']
Read "http://docs.conan.io/en/latest/faq/troubleshooting.html#error-invalid-setting"
Invalid setting 'AIX' is not a valid 'settings.os' value.
Possible values are ['Android', 'Arduino', 'FreeBSD', 'Linux', 'Macos', 'SunOS', 'Windows', 'WindowsStore', 'iOS', 'tvOS', 'watchOS']
Read "http://docs.conan.io/en/latest/faq/troubleshooting.html#error-invalid-setting"
```

@author:
So then we have to do it correctly managing both streams in the conan ouput.

@author:
I'll update