Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output is appended to the report file when using --output-file #1491

Closed
ghost opened this issue Dec 13, 2021 · 9 comments
Closed

Output is appended to the report file when using --output-file #1491

ghost opened this issue Dec 13, 2021 · 9 comments

Comments

@ghost
Copy link

ghost commented Dec 13, 2021

We are using flake 4.0.1 in a clean virtual environment together with tox 3.42.2.

$ mkvirtualenv test
$ pip install flake8==4.0.1
$ pip install tox==3.42.2

We need to use the --output-file option to store the flake8 results to a file because tox does not support stdout redirection to a file. This report will then be used to publish the findings on our build server.
Also developers will use the same flow to check their code before committing it to the repository. During these checks we found out that flake8 does append new findings to an existing output file instead of replacing it with a new version. This makes it unnecessarily hard to check if reported findings have been fixed or new ones have been added to the report.

Appending to a log/report file without explicitly telling the tool to do so is not intuitive. In fact flake8 seems to be the only tool I know that does not overwrite output files by default. Also deleting the file manually before running flake8 again is error prone and has to be communicated to the developers.

Unfortunately there is no useful information in the issue #801 that is associated with the commit that changed the behavior from opening the file in write mode to opening it in append mode.

I propose to change the file mode back to 'w' to give flake8 the expected behavior.

@ghost ghost changed the title Output is appended to thereport file when using --output-file Output is appended to the report file when using --output-file Dec 13, 2021
@asottile
Copy link
Member

you could probably use redirection in tox fairly easily:

allow_externals = bash
commands =
    bash -exc 'flake8 whatever > output.txt'

@asottile
Copy link
Member

#535 is the rationale for #810 by the way

@ghost
Copy link
Author

ghost commented Dec 14, 2021

We are running our checks on Windows and Linux and unfortunately there is no bash available on our Windows machines. Therefore we would need to introduce OS-dependent solutions, which are also just workarounds for the real problem.

@asottile
Copy link
Member

how are you getting the code onto the windows machines? if you're using git then you have bash

@ghost
Copy link
Author

ghost commented Dec 14, 2021

Sorry, but we are using SVN as version control for some Python packages and we are having some restrictions on what is allowed on out build nodes. So no chance for getting bash there.

@asottile
Copy link
Member

surely you can use python then:

commands =
    python -c 'import subprocess; raise SystemExit(subprocess.call(("flake8",), stdout=open("logfile", "w")))'

@ghost
Copy link
Author

ghost commented Dec 15, 2021

After taking some time to look at the code it looks like the problem is that the logger and the formatter both open the same file and that the formatter does not know if the file has already been filled with log output.
Would it not be possible to just flush the file if it exists, so that both tools start with an empty file? This could be done before initializing the logger.

@sigmavirus24
Copy link
Member

@smartSBA why can't you ensure the file is truncated before running flake8? This is a requirement you have

@ghost
Copy link
Author

ghost commented Dec 15, 2021

I just think that appending stuff to a report file by default is somehow unusual. For log files it would be OK. But in this case where the program is run once it does not make sense for me. At least it could be mentioned somewhere in the help output.

From my point of view changing this behavior would be an improvement. But it is your decision to do so or leave it as it is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants