From 83643e43dffd3461226d26f1899d2efcaf917ece Mon Sep 17 00:00:00 2001 From: QianEric Date: Wed, 25 Nov 2026 17:22:50 +0800 Subject: [PATCH] An unwritable `--junit-xml` path is a usage error, not an internal error ``` $ mypy --junit-xml /nope/junit.xml ok.py error: INTERNAL ERROR -- Please try using mypy master on GitHub: https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build If this issue continues with mypy master, please report a bug at https://github.com/python/mypy/issues version: 2.5.0+dev note: please use --show-traceback to print a traceback when reporting a bug ``` Nothing is wrong with mypy there: `util.write_junit_xml()` calls `os.makedirs()` and `open()` on a path the user cannot write, the `OSError` escapes `run_build()`, and the top-level handler reports it as an internal error and asks for a bug report. Options that name a bad path are reported normally elsewhere: ``` $ mypy --config-file /nope.ini ok.py mypy: error: Cannot find config file '/nope.ini' ``` ## Change `mypy/main.py`, around the `maybe_write_junit_xml()` call which sits outside the `except CompileError` in `run_build()`: ```python py_version = f"{options.python_version[0]}_{options.python_version[1]}" - if options.junit_format == "global": - util.write_junit_xml(...) - else: - # per_file - util.write_junit_xml(...) + try: + if options.junit_format == "global": + util.write_junit_xml(...) + else: + # per_file + util.write_junit_xml(...) + except OSError as err: + # A path we cannot write to is a usage problem, not an internal error + sys.stderr.write(f"error: Cannot write junit-xml to {options.junit_xml}: {err}\n") + sys.exit(2) ``` | invocation | before | after | |---|---|---| | `--junit-xml /nope/junit.xml` | `error: INTERNAL ERROR -- Please try using mypy master ... please report a bug`, exit 2 | `error: Cannot write junit-xml to /nope/junit.xml: [Errno 13] Permission denied: '/nope'`, exit 2 | | `--junit-xml junit.xml` | report written | unchanged | | `--junit-xml sub/junit.xml` (missing dir) | dirs created | unchanged | | mypy errors + a bad `--junit-xml` | the error report is lost behind the internal error | the mypy messages are printed as usual, then the path error | Exit 2 is what mypy already uses for serious errors (`fail()`), and `sys.exit(2)` after a message is the same shape as the existing `--exclude` regex check in `modulefinder.py`. The other report writers (`--linecoverage-report`, `--any-exprs-report`, `--html-report`) take the same internal-error route today; this change deliberately covers `--junit-xml` only, since it is the one written from `main()`. ## Test `mypy/test/testutil.py`, next to the other `maybe_write_junit_xml` tests: ```python def test_junit_xml_unwritable_path_is_a_reported_error(self) -> None: with tempfile.TemporaryDirectory() as tmpdir: blocker = os.path.join(tmpdir, "blocker") with open(blocker, "w") as f: f.write("this is a file, not a directory\n") options = Options() options.junit_xml = os.path.join(blocker, "junit.xml") with mock.patch.object(sys, "stderr", new_callable=io.StringIO) as stderr: with pytest.raises(SystemExit) as exc_info: maybe_write_junit_xml(1.23, False, [], {}, options) assert exc_info.value.code == 2 assert "Cannot write junit-xml to" in stderr.getvalue() assert "INTERNAL ERROR" not in stderr.getvalue() ``` `pytest mypy/test/testutil.py`: 8 passed. The new test fails on an unmodified checkout with `FileExistsError` from `os.makedirs()` (the "file used as a directory" trick keeps it root-proof). --- mypy/main.py | 33 +++++++++++++++++++-------------- mypy/test/testutil.py | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/mypy/main.py b/mypy/main.py index 9982d7a6eace5..b31febe0d93d4 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -1723,20 +1723,25 @@ def maybe_write_junit_xml( # Elapsed time would make the output non-reproducible. td = 0.0 py_version = f"{options.python_version[0]}_{options.python_version[1]}" - if options.junit_format == "global": - util.write_junit_xml( - td, - serious, - {None: all_messages} if all_messages else {}, - options.junit_xml, - py_version, - options.platform, - ) - else: - # per_file - util.write_junit_xml( - td, serious, messages_by_file, options.junit_xml, py_version, options.platform - ) + try: + if options.junit_format == "global": + util.write_junit_xml( + td, + serious, + {None: all_messages} if all_messages else {}, + options.junit_xml, + py_version, + options.platform, + ) + else: + # per_file + util.write_junit_xml( + td, serious, messages_by_file, options.junit_xml, py_version, options.platform + ) + except OSError as err: + # A path we cannot write to is a usage problem, not an internal error + sys.stderr.write(f"error: Cannot write junit-xml to {options.junit_xml}: {err}\n") + sys.exit(2) def fail(msg: str, stderr: TextIO, options: Options) -> NoReturn: diff --git a/mypy/test/testutil.py b/mypy/test/testutil.py index e8b0257ff2ebd..e5a356690e703 100644 --- a/mypy/test/testutil.py +++ b/mypy/test/testutil.py @@ -1,9 +1,13 @@ from __future__ import annotations +import io import os +import sys import tempfile from unittest import TestCase, mock +import pytest + from mypy.inspections import parse_location from mypy.main import maybe_write_junit_xml from mypy.options import Options @@ -113,6 +117,20 @@ def test_serious_error(self) -> None: ) assert result == expected + def test_junit_xml_unwritable_path_is_a_reported_error(self) -> None: + with tempfile.TemporaryDirectory() as tmpdir: + blocker = os.path.join(tmpdir, "blocker") + with open(blocker, "w") as f: + f.write("this is a file, not a directory\n") + options = Options() + options.junit_xml = os.path.join(blocker, "junit.xml") + with mock.patch.object(sys, "stderr", new_callable=io.StringIO) as stderr: + with pytest.raises(SystemExit) as exc_info: + maybe_write_junit_xml(1.23, False, [], {}, options) + assert exc_info.value.code == 2 + assert "Cannot write junit-xml to" in stderr.getvalue() + assert "INTERNAL ERROR" not in stderr.getvalue() + def test_junit_time_is_zero_in_bazel_mode(self) -> None: # Output must be reproducible under Bazel, so elapsed time is not included. with tempfile.TemporaryDirectory() as tmpdir: