Skip to content

Commit 264715e

Browse files
authored
Always write the cache (unless cache_dir is /dev/null) (#3133)
1 parent 3ed7394 commit 264715e

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

mypy/build.py

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -860,8 +860,7 @@ def write_cache(id: str, path: str, tree: MypyFile,
860860

861861
# Make sure directory for cache files exists
862862
parent = os.path.dirname(data_json)
863-
if not os.path.isdir(parent):
864-
os.makedirs(parent)
863+
os.makedirs(parent, exist_ok=True)
865864
assert os.path.dirname(meta_json) == parent
866865

867866
# Construct temp file names
@@ -877,6 +876,20 @@ def write_cache(id: str, path: str, tree: MypyFile,
877876
data_str = json.dumps(data, sort_keys=True)
878877
interface_hash = compute_hash(data_str)
879878

879+
# Obtain and set up metadata
880+
try:
881+
st = manager.get_stat(path)
882+
except OSError as err:
883+
manager.log("Cannot get stat for {}: {}".format(path, err))
884+
# Remove apparently-invalid cache files.
885+
for filename in [data_json, meta_json]:
886+
try:
887+
os.remove(filename)
888+
except OSError:
889+
pass
890+
# Still return the interface hash we computed.
891+
return interface_hash
892+
880893
# Write data cache file, if applicable
881894
if old_interface_hash == interface_hash:
882895
# If the interface is unchanged, the cached data is guaranteed
@@ -891,8 +904,6 @@ def write_cache(id: str, path: str, tree: MypyFile,
891904
os.replace(data_json_tmp, data_json)
892905
manager.trace("Interface for {} has changed".format(id))
893906

894-
# Obtain and set up metadata
895-
st = manager.get_stat(path) # TODO: Handle errors
896907
mtime = st.st_mtime
897908
size = st.st_size
898909
options = manager.options.clone_for_module(id)
@@ -1524,26 +1535,26 @@ def valid_references(self) -> Set[str]:
15241535
return valid_refs
15251536

15261537
def write_cache(self) -> None:
1527-
ok = self.path and self.options.incremental
1528-
if ok:
1529-
if self.manager.options.quick_and_dirty:
1530-
is_errors = self.manager.errors.is_errors_for_file(self.path)
1531-
else:
1532-
is_errors = self.manager.errors.is_errors()
1533-
ok = not is_errors
1534-
if ok:
1535-
dep_prios = [self.priorities.get(dep, PRI_HIGH) for dep in self.dependencies]
1536-
new_interface_hash = write_cache(
1537-
self.id, self.path, self.tree,
1538-
list(self.dependencies), list(self.suppressed), list(self.child_modules),
1539-
dep_prios, self.interface_hash,
1540-
self.manager)
1541-
if new_interface_hash == self.interface_hash:
1542-
self.manager.log("Cached module {} has same interface".format(self.id))
1543-
else:
1544-
self.manager.log("Cached module {} has changed interface".format(self.id))
1545-
self.mark_interface_stale()
1546-
self.interface_hash = new_interface_hash
1538+
if not self.path or self.options.cache_dir == os.devnull:
1539+
return
1540+
if self.manager.options.quick_and_dirty:
1541+
is_errors = self.manager.errors.is_errors_for_file(self.path)
1542+
else:
1543+
is_errors = self.manager.errors.is_errors()
1544+
if is_errors:
1545+
return
1546+
dep_prios = [self.priorities.get(dep, PRI_HIGH) for dep in self.dependencies]
1547+
new_interface_hash = write_cache(
1548+
self.id, self.path, self.tree,
1549+
list(self.dependencies), list(self.suppressed), list(self.child_modules),
1550+
dep_prios, self.interface_hash,
1551+
self.manager)
1552+
if new_interface_hash == self.interface_hash:
1553+
self.manager.log("Cached module {} has same interface".format(self.id))
1554+
else:
1555+
self.manager.log("Cached module {} has changed interface".format(self.id))
1556+
self.mark_interface_stale()
1557+
self.interface_hash = new_interface_hash
15471558

15481559

15491560
def dispatch(sources: List[BuildSource], manager: BuildManager) -> Graph:

0 commit comments

Comments
 (0)