nox icon indicating copy to clipboard operation
nox copied to clipboard

Add option to limit the logged arguments

Open FredStober opened this issue 1 year ago • 6 comments

When running nox from within pre-commit, the argument list can be quite large and fill several screens. In most cases, the interesting thing is not the whole used command line argument but just the first few options. Therefore, I've added a new option 'max_log_args' that allows to limit the number of arguments that are shown in the logging output.

FredStober avatar May 04 '24 13:05 FredStober

Sorry, I forgot to commit a line to tests/test_sessions.py

FredStober avatar May 04 '24 20:05 FredStober

Does this need to be a built in Nox feature? I suppose you're passing the files as session arguments (session.posargs) so in your session function you have full control over what gets logged. You can pass the silent flag to session.run and produce the log line yourself.

cjolowicz avatar May 05 '24 12:05 cjolowicz

I tried to implement it outside with silence and log=False, but in case of an error, these flags do not apply and my command line with 500 file names (args are managed by pre-commit) gets logged to the console.

Here is one example for something I tried. But even with this command, the failing command line is shown in the console:

with Path('test.err').open('w') as stderr:
   output = session.run('ruff', 'check', *session.posargs, stderr=stderr, silent=True, log=False)

Every other approach I could think of would have modified the behavior of the run function in some more substantial way.

If I add all possible error codes to success_codes to really suppress any kind of logging by nox, I would still need to somehow get to the error code, but the run command does not expose that information - so I would have to extend the run interface (that would be a breaking change I guess...) or manually try to parse the output to determine the outcome.

Hm - on second thought I guess I could use subprocess instead of session.run, since I mostly care about the venv management. But that seems like a pretty strange thing to do.

In the end I don't mind that there is a log of the failing command line - I want it there. It's just that it's too verbose (and I assume I'm not the only one with session.run commands with long argument lists) - hence the PR :)

FredStober avatar May 05 '24 13:05 FredStober

I agree with @cjolowicz, I want to see the full command on failure. BTW ruff supports pre-commit directly without the need of using a box session.

Spacetown avatar Oct 26 '25 22:10 Spacetown

This is a typical command line. I had to pixelate details related to the project - but from the font size of the rest of the console, you can guess how it looks like: image

ruff was just an example - and in our case, the nox session for ruff is configured with different options, depending on if it runs locally or in a CICD pipeline. So running it directly doesn't help here. For other hooks like pylint or mypy, running pre-commit with nox avoids the need to specify libraries twice - once in the pyproject.toml and then in the pre-commit yaml file. (This setup is also detailed in the "Hypermodern Python Cookiecutter" guide - btw).

The point of the new option is that you can see the full arguments - at least until the max_log_args are reached - at which point you probably don't care. You can also configure that dynamically, so in a CICD pipeline that limit could be unset - while locally you could set it to some sane value.

FredStober avatar Oct 27 '25 14:10 FredStober

I would solve this on noxfile level by using response file or direct usage of subprocess package. If you still want to solve this in not, what about a option --no-log-command-on-failure?

            logger.error(
                f"Command {(full_cmd + ' ') if log_command_on_failure else ''}failed with exit code {return_code}{suffix}"
            )

Spacetown avatar Oct 27 '25 18:10 Spacetown