Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: automatically refresh the cache when required #870

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 40 additions & 19 deletions src/scikit_build_core/cmake.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@

def __post_init__(self) -> None:
self.init_cache_file = self.build_dir / "CMakeInit.txt"
source_dir = self.source_dir.resolve()

if not self.source_dir.is_dir():
msg = f"source directory {self.source_dir} does not exist"
Expand All @@ -95,26 +96,46 @@
msg = f"build directory {self.build_dir} must be a (creatable) directory"
raise CMakeConfigError(msg)

# If these were the same, the following check could wipe the source directory!
if self.build_dir.resolve() != self.source_dir.resolve():
skbuild_info = self.build_dir / ".skbuild-info.json"
# If building via SDist, this could be pre-filled, so delete it if it exists
skbuild_info = self.build_dir / ".skbuild-info.json"
stale = False

info: dict[str, str] = {}
with contextlib.suppress(FileNotFoundError), skbuild_info.open(
"r", encoding="utf-8"
) as f:
info = json.load(f)

if info:
# If building via SDist, this could be pre-filled
cached_source_dir = Path(info["source_dir"])
if cached_source_dir != source_dir:
logger.warning(

Check warning on line 112 in src/scikit_build_core/cmake.py

View check run for this annotation

Codecov / codecov/patch

src/scikit_build_core/cmake.py#L110-L112

Added lines #L110 - L112 were not covered by tests
"Original src {} != {}, clearing cache",
cached_source_dir,
source_dir,
)
stale = True

Check warning on line 117 in src/scikit_build_core/cmake.py

View check run for this annotation

Codecov / codecov/patch

src/scikit_build_core/cmake.py#L117

Added line #L117 was not covered by tests

# Isolated environments can cause this
cached_skbuild_dir = Path(info["skbuild_path"])
if cached_skbuild_dir != DIR:
logger.info(

Check warning on line 122 in src/scikit_build_core/cmake.py

View check run for this annotation

Codecov / codecov/patch

src/scikit_build_core/cmake.py#L120-L122

Added lines #L120 - L122 were not covered by tests
"New isolated environment {} -> {}, clearing cache",
cached_skbuild_dir,
DIR,
)
stale = True

Check warning on line 127 in src/scikit_build_core/cmake.py

View check run for this annotation

Codecov / codecov/patch

src/scikit_build_core/cmake.py#L127

Added line #L127 was not covered by tests

# Not using --fresh here, not just due to CMake 3.24+, but also just in
# case it triggers an extra FetchContent pull in CMake 3.30+
if stale:
# Python 3.8+ can use missing_ok=True
with contextlib.suppress(FileNotFoundError):
with skbuild_info.open("r", encoding="utf-8") as f:
info = json.load(f)

cached_source_dir = Path(info["source_dir"])
if cached_source_dir.resolve() != self.source_dir.resolve():
logger.warning(
"Original src {} != {}, wiping build directory",
cached_source_dir,
self.source_dir,
)
shutil.rmtree(self.build_dir)
self.build_dir.mkdir()

with skbuild_info.open("w", encoding="utf-8") as f:
json.dump(self._info_dict(), f, indent=2)
self.build_dir.joinpath("CMakeCache.txt").unlink()
shutil.rmtree(self.build_dir.joinpath("CMakeFiles"), ignore_errors=True)

Check warning on line 135 in src/scikit_build_core/cmake.py

View check run for this annotation

Codecov / codecov/patch

src/scikit_build_core/cmake.py#L134-L135

Added lines #L134 - L135 were not covered by tests

with skbuild_info.open("w", encoding="utf-8") as f:
json.dump(self._info_dict(), f, indent=2)

def _info_dict(self) -> dict[str, str]:
"""
Expand Down
Loading