fix: Don't log if --json or --quiet#4202
Conversation
|
Might fix #3763 |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #4202 +/- ##
==========================================
- Coverage 54.75% 54.58% -0.18%
==========================================
Files 240 240
Lines 30145 30254 +109
Branches 3214 3234 +20
==========================================
+ Hits 16505 16513 +8
- Misses 13637 13738 +101
Partials 3 3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
d8e6bdd to
91e8d11
Compare
|
The failing test is actually not outputting any json, which suggests that the error is not properly handled with |
475f7cd to
c42b253
Compare
|
I had to go a bit farther for this pr:
The missing part is the message to use |
Yes, the output should be handle via the
To me the behavior that you describe is consistent with what we are expecting. |
d07bc8e to
c68cff6
Compare
It looks like the YAML output is only when |
ae08936 to
d7a5aab
Compare
da0d44a to
0ac5416
Compare
0ac5416 to
5e50a61
Compare
5e50a61 to
f413d1c
Compare
… warning by default)
jjerphan
left a comment
There was a problem hiding this comment.
LGTM from a functional point of view modulo a few comments and questions.
As discussed privately, there is still some fuzz between directs usage of std::{cout,cerr} and of the Console, but this goes way beyond the scope of this PR.
There was a problem hiding this comment.
We can simplify / fix the explicit environment export in another PR.
There was a problem hiding this comment.
I'm not 100% sure I understand what you mean, but to clarify why this file is modified:
this PR is fixing the whole json output so that it all go into the same system and we can properly make sure that no non-json (usually errors) is emitted in that case. The change in this file is necessary to achieve that (along with any other json output code that was not using Console).
Note that the resulting output was not changed, it just goes into the proper system but the output json object is the same (with just the "log_history" field added, but otherwise same).
Or am I missing something/misunderstanding your comment?
There was a problem hiding this comment.
I meant that there are still many direct usages of stdout here and some TODO which we can treat later in another PR.
| assert any(pkg["name"] == "x264" for pkg in res["actions"]["LINK"]) | ||
|
|
||
|
|
||
| @pytest.mark.timeout(30) |
There was a problem hiding this comment.
With the recent changes regarding should_be_emitted, might have logs been reduced? On my machine, it now takes 14.47s to run.
Can we try reverting this?
There was a problem hiding this comment.
It's the same issue we see in the rest of the CI so I dont think it has anything to do with any of the changes here?
Reverting this will probably work as long as the issue is random.
There was a problem hiding this comment.
I guess I meant that we can ignore this failure and treat it independently so that the timeout does not get increased in this PR.
| mamba::OutputParams output_params; | ||
| for (const char* arg : std::ranges::subrange(argv, argv + argc)) | ||
| { | ||
| if (arg == "--json"sv) | ||
| { | ||
| output_params.json = true; | ||
| } | ||
| else if (arg == "--quiet"sv) | ||
| { | ||
| output_params.quiet = true; | ||
| } | ||
| } | ||
|
|
||
| if (output_params.json or output_params.quiet) | ||
| { | ||
| options.output_params = output_params; | ||
| } |
There was a problem hiding this comment.
Nit: Why use an intermediary mamba::OutputParams?
There was a problem hiding this comment.
mamba::ContextOptions::output_params is an optional which is empty by default, so here we only set it if one of the parameters is set. The behavior of the program is different if that optional is empty vs not-empty with members set to false. That's why we need to set the optional only if the right condition is met.
For reading clarity I will rewrite this with .emplace.
|
|
||
| if (this->at("print_config_only").value<bool>()) | ||
| { | ||
| // TODO: fix this for the case where `--json` is used |
There was a problem hiding this comment.
Should we open an issue which tracks all the TODO introduced in this PR such as this one?
There was a problem hiding this comment.
I'll double check but I thinkt his TODO is not up to date, print_dump now outputs to the normal json output, if I'm not mistaken, when --json is used, was not the case before.
| */ | ||
| void cancel_json_print(); | ||
|
|
||
| // FIXME: documentation |
There was a problem hiding this comment.
Did you mean to introduce a small docstring?
There was a problem hiding this comment.
I forgot to add the docstring yes, will add.
| @@ -67,14 +96,15 @@ namespace mamba::logging | |||
| */ | |||
| auto push_if_enabled(LogRecord& record) -> bool | |||
There was a problem hiding this comment.
There was a problem hiding this comment.
When this function returns false, in both usage we move the record in another container, so I can't just take by r-value-reference without at least returning the object again so that it can be used by the caller, otherwise at the call point it would look like we move the object twice, which is even weirder.
if (not backtrace.push_if_enabled(std::move(record)))
{
something_else(std::move(record)); // looks like UB
}My goal here is to have this algorithm written only once, as having the same logic in both place lead to some annoying errors while developing. But I also agree it's quite weird api.
I'll try to find another way to achieve that goal with a nicer api. I was thinking maybe something in that direction:
// free function:
auto push_to_backtrace_or(Record&& record, BasicBacktrace& backtrace, std::invocable<Record&&> auto or_func);
// in call site:
push_to_backtrace_or(std::move(record), backtrace, [&](LogRecord&& record){
// do the work if not pushed to backtrace
something_else(std::move(record));
});The passing of the same object to the lambda is a bit weird but maybe less weird than this current api?
There was a problem hiding this comment.
I was thinking of having push_to_backtrace returning the log record if not pushed (can be an optional, or a pair<bool, LogRecord>), but that would make the syntax heavier. Passing a lambda as you suggest seems the best option in the end, I don't find it weird to pass the same object to the lambda; and it is not that obvious at the call site.
This reverts commit e495113.
… the backtrace is disabled
JohanMabille
left a comment
There was a problem hiding this comment.
LGTM, the error on Windows is unrelated to this PR. The failure on OSX is flaky (timeout), restarting the job should fix it.
Yes, the OSX timeout was observed in other PRs (also on Linux runs, here and there) and I had doubled the timeout in this pr but we agreed with @jjerphan that it should be treated in another pr. However, restarting the job is not guaranteed to fix it, it seems to happen often. |
jjerphan
left a comment
There was a problem hiding this comment.
LGTM. Can we use conventional commit for this PR title?
Sure. |
--json or --quiet--json or --quiet
|
I agree that this the types of commit of conventional commits are a bit too coarse (I find it useful when considering doing a release), but is is this PR doing something else than fixing a wrong behavior of mamba? |
It does fix the wrong general behavior around the json output but the issue is what that implies, the consequences of the fix:
I wouldnt call that a feature either so fix is the closest I guess. Personally for helping with release changelogs (and general communication around releases) in other projects I worke(d) on I tend to prefer distinguishing "changes" from "fixes" only. This means "fixes" are only changing the implementation, not the interfaces, and correcting behaviors. "Changes" are everything else. Some are big, some small, some might or not break the interface or widely change the behavior. But really I'm nitpicking and acting like an old man waving his cane to the clouds, don't mind me 🙈 It's not that important :) |
|
I see and I agree. My assumption is/was that one can split changes into smaller ones fitting the types of conventional commits. Here "enhancement" ( Anyway, it is not really a big deal. |
Yes, IFF it does make sense to split. Not the case here though IMO. The things I pointed are direct consequences of the core changes of the pr, not really separable except if we accept weird situations where (to clarify, the only things that could be separated here is the improvement + fixes on
If we could I would say it's both fix and enhancement. My understanding is that we cannot set both? |
|
To me, fixes are one kind of enhancements which themselves are more generic (enhancements as improvements of existing aspect of a project). |
Description
While working on #4126 when checking issues on the CI I regularly hit failing tests where the json output failed to parse because of an error (usually because of #4201) plus the error was logged while
--jsonwas used, which should never happen.This is a known and old issue, mainly visible while PRs are not stabilized yet.
This PR relies on the recently introduced logging system by completely deactivating it when
--jsonor--quietis parsed or if the related params passed to theContextconstructor aretrue.--quietis used, the logging system is then stopped, which means whatever existing logging handler will be removed, leading to logging records being completely ignored.--jsonis used, a special log-handler is setup to keep track of the log-records which pass the log-level filter (with no maximum count limit) and these records will be added to an array"log_history"as member of the JSON output object.--jsonalso guarantees that a JSON output will appear even if it's an empty JSON object (when returning without a log, which should be rare).Some consequences of the solution implemented in this PR:
(micro)mambawill never output any log-record as long as--jsonor--quiet(this doesnt apply to outputs which are not logs, like the json output);libmambauser code to inject a new log-handler after processing the output params, which will potentially renabled log-record being output iflibmamba's code doesnt properly prevent it because of--jsonor--quiet; although in the present codebase we never do that and don't expect users to try switching log-handlers after initialization (except for testing).Contextconstruction or when--jsonor--quietare parsed (we are not doing that in (micro)mamba).Some alternative solutions we could have used here but rejected for now:
libmambausers want to use another log-handler (python handling for example).Note: I also fixed an incorrect log-level filter in
LogHandler_Historywhich is now revealed by a more complete set of tests.Type of Change
Checklist
pre-commit run --alllocally in the source folder and confirmed that there are no linter errors.