diff --git a/mypy/build.py b/mypy/build.py index 62367c35915e..c1a442296aa7 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -2465,6 +2465,12 @@ def write_cache(self) -> None: or self.options.cache_dir == os.devnull or self.options.fine_grained_incremental ): + if self.options.debug_serialize: + try: + self.tree.serialize() + except Exception: + print(f"Error serializing {self.id}", file=self.manager.stdout) + raise # Propagate to display traceback return is_errors = self.transitive_error if is_errors: diff --git a/mypy/main.py b/mypy/main.py index 405596c20991..a40e5d5ae5a8 100644 --- a/mypy/main.py +++ b/mypy/main.py @@ -1117,6 +1117,9 @@ def add_invertible_flag( parser.add_argument( "--cache-map", nargs="+", dest="special-opts:cache_map", help=argparse.SUPPRESS ) + # --debug-serialize will run tree.serialize() even if cache generation is disabled. + # Useful for mypy_primer to detect serialize errors earlier. + parser.add_argument("--debug-serialize", action="store_true", help=argparse.SUPPRESS) # This one is deprecated, but we will keep it for few releases. parser.add_argument( "--enable-incomplete-features", action="store_true", help=argparse.SUPPRESS diff --git a/mypy/options.py b/mypy/options.py index 3a08ff9455ee..d2de2bb09166 100644 --- a/mypy/options.py +++ b/mypy/options.py @@ -249,6 +249,9 @@ def __init__(self) -> None: # Read cache files in fine-grained incremental mode (cache must include dependencies) self.use_fine_grained_cache = False + # Run tree.serialize() even if cache generation is disabled + self.debug_serialize = False + # Tune certain behaviors when being used as a front-end to mypyc. Set per-module # in modules being compiled. Not in the config file or command line. self.mypyc = False