|
28 | 28 | from typing import TYPE_CHECKING, Any, Callable, ClassVar, Final, NoReturn, TextIO, TypedDict |
29 | 29 | from typing_extensions import TypeAlias as _TypeAlias |
30 | 30 |
|
| 31 | +from librt.internal import cache_version |
| 32 | + |
31 | 33 | import mypy.semanal_main |
32 | | -from mypy.cache import Buffer, CacheMeta |
| 34 | +from mypy.cache import CACHE_VERSION, Buffer, CacheMeta |
33 | 35 | from mypy.checker import TypeChecker |
34 | 36 | from mypy.error_formatter import OUTPUT_CHOICES, ErrorFormatter |
35 | 37 | from mypy.errors import CompileError, ErrorInfo, Errors, report_internal_error |
@@ -1334,12 +1336,18 @@ def find_cache_meta(id: str, path: str, manager: BuildManager) -> CacheMeta | No |
1334 | 1336 | return None |
1335 | 1337 | t1 = time.time() |
1336 | 1338 | if isinstance(meta, bytes): |
1337 | | - data_io = Buffer(meta) |
| 1339 | + # If either low-level buffer format or high-level cache layout changed, we |
| 1340 | + # cannot use the cache files, even with --skip-version-check. |
| 1341 | + # TODO: switch to something like librt.internal.read_byte() if this is slow. |
| 1342 | + if meta[0] != cache_version() or meta[1] != CACHE_VERSION: |
| 1343 | + manager.log(f"Metadata abandoned for {id}: incompatible cache format") |
| 1344 | + return None |
| 1345 | + data_io = Buffer(meta[2:]) |
1338 | 1346 | m = CacheMeta.read(data_io, data_file) |
1339 | 1347 | else: |
1340 | 1348 | m = CacheMeta.deserialize(meta, data_file) |
1341 | 1349 | if m is None: |
1342 | | - manager.log(f"Metadata abandoned for {id}: attributes are missing") |
| 1350 | + manager.log(f"Metadata abandoned for {id}: cannot deserialize data") |
1343 | 1351 | return None |
1344 | 1352 | t2 = time.time() |
1345 | 1353 | manager.add_stats( |
@@ -1671,7 +1679,9 @@ def write_cache_meta(meta: CacheMeta, manager: BuildManager, meta_file: str) -> |
1671 | 1679 | if manager.options.fixed_format_cache: |
1672 | 1680 | data_io = Buffer() |
1673 | 1681 | meta.write(data_io) |
1674 | | - meta_bytes = data_io.getvalue() |
| 1682 | + # Prefix with both low- and high-level cache format versions for future validation. |
| 1683 | + # TODO: switch to something like librt.internal.write_byte() if this is slow. |
| 1684 | + meta_bytes = bytes([cache_version(), CACHE_VERSION]) + data_io.getvalue() |
1675 | 1685 | else: |
1676 | 1686 | meta_dict = meta.serialize() |
1677 | 1687 | meta_bytes = json_dumps(meta_dict, manager.options.debug_cache) |
|
0 commit comments