diff --git a/sqlmesh/core/context.py b/sqlmesh/core/context.py index 18df3a01fd..c0d9b21ff8 100644 --- a/sqlmesh/core/context.py +++ b/sqlmesh/core/context.py @@ -2537,8 +2537,8 @@ def clear_caches(self) -> None: if self.cache_dir.exists(): rmtree(self.cache_dir) - if isinstance(self.state_sync, CachingStateSync): - self.state_sync.clear_cache() + if isinstance(self._state_sync, CachingStateSync): + self._state_sync.clear_cache() def export_state( self, diff --git a/tests/core/test_context.py b/tests/core/test_context.py index 0f14fd4a8b..e0567e8168 100644 --- a/tests/core/test_context.py +++ b/tests/core/test_context.py @@ -647,6 +647,9 @@ def test_clear_caches(tmp_path: pathlib.Path): assert not cache_dir.exists() assert models_dir.exists() + # Ensure that we don't initialize a CachingStateSync only to clear its (empty) caches + assert context._state_sync is None + # Test clearing caches when cache directory doesn't exist # This should not raise an exception context.clear_caches()