From e1e0f8c88d88386e032f9269414eb8e75602e432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Klaim=20=28Jo=C3=ABl=20Lamotte=29?= <142265+Klaim@users.noreply.github.com> Date: Wed, 20 Nov 2024 11:27:55 +0100 Subject: [PATCH] Prevent pip "rich" ouput (#3607) Co-authored-by: Johan Mabille Co-authored-by: Hind-M <70631848+Hind-M@users.noreply.github.com> --- libmamba/src/core/prefix_data.cpp | 78 ++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 28 deletions(-) diff --git a/libmamba/src/core/prefix_data.cpp b/libmamba/src/core/prefix_data.cpp index c7031dbaff..50e574506d 100644 --- a/libmamba/src/core/prefix_data.cpp +++ b/libmamba/src/core/prefix_data.cpp @@ -17,6 +17,7 @@ #include "mamba/core/output.hpp" #include "mamba/core/prefix_data.hpp" #include "mamba/core/util.hpp" +#include "mamba/core/util_scope.hpp" #include "mamba/specs/conda_url.hpp" #include "mamba/util/environment.hpp" #include "mamba/util/graph.hpp" @@ -215,37 +216,58 @@ namespace mamba const auto get_python_path = [&] { return util::which_in("python", util::get_path_dirs(m_prefix_path)).string(); }; - const auto args = std::array{ get_python_path(), - "-m", - "pip", - "inspect", - "--local" }; + const auto args = std::array{ get_python_path(), "-q", "-m", "pip", + "inspect", "--local" }; - const std::vector> env{ { "PYTHONIOENCODING", "utf-8" } }; + const std::vector> env{ + { "PYTHONIOENCODING", "utf-8" }, + { "NO_COLOR", "1" }, + { "PIP_NO_COLOR", "1" }, + { "PIP_NO_PYTHON_VERSION_WARNING", "1" }, + }; reproc::options run_options; run_options.env.extra = reproc::env{ env }; - LOG_TRACE << "Running command: " - << fmt::format("{}\n env options:{}", fmt::join(args, " "), fmt::join(env, " ")); - - auto [status, ec] = reproc::run( - args, - run_options, - reproc::sink::string(out), - reproc::sink::string(err) - ); - - if (ec) - { - const auto message = fmt::format( - "failed to run python command :\n error: {}\n command ran: {}\n env options:{}\n-> output:\n{}\n\n-> error output:{}", - ec.message(), - fmt::join(args, " "), - fmt::join(env, " "), - out, - err + { // Scoped environment changes + + // We need FORCE_COLOR to be removed to avoid rich output, + // we restore it as soon as the command is run. + const auto maybe_previous_force_color = util::get_env("FORCE_COLOR"); + util::unset_env("FORCE_COLOR"); + on_scope_exit _{ [&] + { + if (maybe_previous_force_color) + { + util::set_env("FORCE_COLOR", maybe_previous_force_color.value()); + } + } }; + + LOG_TRACE << "Running command: " + << fmt::format( + "{}\n env options (FORCE_COLOR is unset):{}", + fmt::join(args, " "), + fmt::join(env, " ") + ); + + auto [status, ec] = reproc::run( + args, + run_options, + reproc::sink::string(out), + reproc::sink::string(err) ); - throw mamba_error{ message, mamba_error_code::internal_failure }; + + if (ec) + { + const auto message = fmt::format( + "failed to run python command :\n error: {}\n command ran: {}\n env options:{}\n-> output:\n{}\n\n-> error output:{}", + ec.message(), + fmt::join(args, " "), + fmt::join(env, " "), + out, + err + ); + throw mamba_error{ message, mamba_error_code::internal_failure }; + } } // Nothing installed with `pip` @@ -261,11 +283,11 @@ namespace mamba { j = nlohmann::json::parse(out); } - catch (const std::exception& exc) + catch (const std::exception& parse_error) { const auto message = fmt::format( "failed to parse python command output:\n error: {}\n command ran: {}\n env options:{}\n-> output:\n{}\n\n-> error output:{}", - exc.what(), + parse_error.what(), fmt::join(args, " "), fmt::join(env, " "), out,