Skip to content

Commit cafc039

Browse files
authored
fix: automatically refresh the cache when required (#870)
Fix #869 by detecting a moved isolated environment and issuing `--fresh`. Also go ahead and use this for the moved source dir case too. --------- Signed-off-by: Henry Schreiner <[email protected]>
1 parent 26ca7ed commit cafc039

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

src/scikit_build_core/cmake.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class CMaker:
8585

8686
def __post_init__(self) -> None:
8787
self.init_cache_file = self.build_dir / "CMakeInit.txt"
88+
source_dir = self.source_dir.resolve()
8889

8990
if not self.source_dir.is_dir():
9091
msg = f"source directory {self.source_dir} does not exist"
@@ -95,26 +96,46 @@ def __post_init__(self) -> None:
9596
msg = f"build directory {self.build_dir} must be a (creatable) directory"
9697
raise CMakeConfigError(msg)
9798

98-
# If these were the same, the following check could wipe the source directory!
99-
if self.build_dir.resolve() != self.source_dir.resolve():
100-
skbuild_info = self.build_dir / ".skbuild-info.json"
101-
# If building via SDist, this could be pre-filled, so delete it if it exists
99+
skbuild_info = self.build_dir / ".skbuild-info.json"
100+
stale = False
101+
102+
info: dict[str, str] = {}
103+
with contextlib.suppress(FileNotFoundError), skbuild_info.open(
104+
"r", encoding="utf-8"
105+
) as f:
106+
info = json.load(f)
107+
108+
if info:
109+
# If building via SDist, this could be pre-filled
110+
cached_source_dir = Path(info["source_dir"])
111+
if cached_source_dir != source_dir:
112+
logger.warning(
113+
"Original src {} != {}, clearing cache",
114+
cached_source_dir,
115+
source_dir,
116+
)
117+
stale = True
118+
119+
# Isolated environments can cause this
120+
cached_skbuild_dir = Path(info["skbuild_path"])
121+
if cached_skbuild_dir != DIR:
122+
logger.info(
123+
"New isolated environment {} -> {}, clearing cache",
124+
cached_skbuild_dir,
125+
DIR,
126+
)
127+
stale = True
128+
129+
# Not using --fresh here, not just due to CMake 3.24+, but also just in
130+
# case it triggers an extra FetchContent pull in CMake 3.30+
131+
if stale:
132+
# Python 3.8+ can use missing_ok=True
102133
with contextlib.suppress(FileNotFoundError):
103-
with skbuild_info.open("r", encoding="utf-8") as f:
104-
info = json.load(f)
105-
106-
cached_source_dir = Path(info["source_dir"])
107-
if cached_source_dir.resolve() != self.source_dir.resolve():
108-
logger.warning(
109-
"Original src {} != {}, wiping build directory",
110-
cached_source_dir,
111-
self.source_dir,
112-
)
113-
shutil.rmtree(self.build_dir)
114-
self.build_dir.mkdir()
115-
116-
with skbuild_info.open("w", encoding="utf-8") as f:
117-
json.dump(self._info_dict(), f, indent=2)
134+
self.build_dir.joinpath("CMakeCache.txt").unlink()
135+
shutil.rmtree(self.build_dir.joinpath("CMakeFiles"), ignore_errors=True)
136+
137+
with skbuild_info.open("w", encoding="utf-8") as f:
138+
json.dump(self._info_dict(), f, indent=2)
118139

119140
def _info_dict(self) -> dict[str, str]:
120141
"""

0 commit comments

Comments
 (0)