nedbat / coveragepy

The code coverage tool for Python
https://coverage.readthedocs.io
Apache License 2.0
2.9k stars 419 forks source link

PermissionError: [Errno 13] Permission denied: '/dev/stdout' #1804

Open senyai opened 2 weeks ago

senyai commented 2 weeks ago

coverage 7.5.4

When running python -m coverage json -q -o /dev/stdout I get this error

  python -m coverage run -m unittest
  python -m coverage json -q -o /dev/stdout |
  python -c 'import sys,json;cov=json.load(sys.stdin)["totals"]["percent_covered"];assert cov > 90, f"{cov} > 90"'
  shell: /usr/bin/bash -e {0}
  env:
    pythonLocation: /opt/hostedtoolcache/Python/3.10.14/x64
    PKG_CONFIG_PATH: /opt/hostedtoolcache/Python/3.10.14/x64/lib/pkgconfig
    Python_ROOT_DIR: /opt/hostedtoolcache/Python/3.10.14/x64
    Python2_ROOT_DIR: /opt/hostedtoolcache/Python/3.10.14/x64
    Python3_ROOT_DIR: /opt/hostedtoolcache/Python/3.10.14/x64
    LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.10.14/x64/lib
..............
----------------------------------------------------------------------
Ran 14 tests in 0.400s

OK
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/report_core.py", line 61, in render_report
    ret = reporter.report(morfs, outfile=outfile)
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/jsonreport.py", line 59, in report
    for file_reporter, analysis in get_analysis_to_report(self.coverage, morfs):
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/report_core.py", line 100, in get_analysis_to_report
    analysis = coverage._analyze(morf)
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/control.py", line 942, in _analyze
    return analysis_from_file_reporter(data, self.config.precision, file_reporter, filename)
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/results.py", line 31, in analysis_from_file_reporter
    statements = file_reporter.lines()
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/python.py", line 194, in lines
    return self.parser.statements
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/python.py", line 185, in parser
    self._parser = PythonParser(
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/parser.py", line 59, in __init__
    self.text = get_python_source(self.filename)
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/python.py", line 64, in get_python_source
    raise NoSource(f"No source for code: '{filename}'.")
coverage.exceptions.NoSource: No source for code: '/home/runner/work/hsirs/hsirs/config-3.py'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/__main__.py", line 10, in <module>
    sys.exit(main())
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/cmdline.py", line 970, in main
    status = CoverageScript().command_line(argv)
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/cmdline.py", line 736, in command_line
    total = self.coverage.json_report(
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/control.py", line 1244, in json_report
    return render_report(self.config.json_output, JsonReporter(self), morfs, self._message)
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/report_core.py", line 70, in render_report
    file_be_gone(output_path)           # pragma: part covered (doesn't return)
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/coverage/misc.py", line 138, in file_be_gone
    os.remove(path)
PermissionError: [Errno 13] Permission denied: '/dev/stdout'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

So, I guess https://github.com/nedbat/coveragepy/blob/fa36ebddb92853c0b154f415277fe79566b8fc2b/coverage/misc.py#L139 should also catch PermissionError.

nedbat commented 1 week ago

A simple way to reproduce:

% cat > hello.py
print("hello")
% coverage run hello.py
hello
% rm hello.py
% coverage json -q -o /dev/stdout
... (the error shown above) ...
PermissionError: [Errno 1] Operation not permitted: '/dev/stdout'
diazona commented 1 week ago

This piqued my curiosity on Mastodon, and after investigating for a little while I have a suggestion: does it seem reasonable that render_report() should not delete a file that it didn't originally create? If so, it could try to open the file in exclusive mode, falling back to write mode, and set delete_file to True or False depending on which one worked.

Alternatively, it could try to delete the file first, before creating it, which should be safe in the sense that it's going to overwrite the file anyway. If the initial deletion fails, that's probably a good sign that it shouldn't try to delete the file afterwards either. The only way I can think of that this goes awry is if it's told to use an output file that exists and is writable but cannot be deleted, and then the report generation fails, leaving the file with potentially garbage content. But if that's a concern, it could just check whether the file is seekable() and if so, truncate it in place if deletion fails; if not, there's probably no way to undo writing that garbage content anyway.

If any of this sounds good, I'd be happy to contribute a PR (I've gotten more than enough good use out of coverage.py that it seems only fair to offer a contribution), or someone else is welcome to take these ideas and make their own PR.