Skip to content

Commit 01a79df

Browse files
authored
fix: avoid providing prepare-metadata methods if rebuild is True (#904)
Reported in rapidfuzz/RapidFuzz#397. This avoids declaring the optional hooks if there's a good chance it can't be computed reliably. Having an override with `failed` can't be computed without trying to build. --------- Signed-off-by: Henry Schreiner <[email protected]>
1 parent e903ec9 commit 01a79df

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

docs/overrides.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ wheel.cmake = false
212212

213213
:::
214214

215+
If this override is present in your pyproject.toml file, scikit-build-core will not
216+
provide the `prepare_metadata_*` hooks, as it can't know without building if the build
217+
will fail.
218+
215219
## Any matching condition
216220

217221
If you use `if.any` instead of `if`, then the override is true if any one of the

src/scikit_build_core/build/__init__.py

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import sys
88

9+
from .._compat import tomllib
10+
911
__all__ = [
1012
"build_editable",
1113
"build_sdist",
@@ -66,29 +68,51 @@ def build_editable(
6668
raise SystemExit(1) from None
6769

6870

69-
def prepare_metadata_for_build_wheel(
70-
metadata_directory: str,
71-
config_settings: dict[str, list[str] | str] | None = None,
72-
) -> str:
73-
"""Prepare metadata for building a wheel. Does not build the wheel. Returns the dist-info directory."""
74-
from .wheel import _build_wheel_impl
71+
def _has_safe_metadata() -> bool:
72+
try:
73+
with open("pyproject.toml", "rb") as f: # noqa: PTH123
74+
pyproject = tomllib.load(f)
75+
except FileNotFoundError:
76+
return True
7577

76-
return _build_wheel_impl(
77-
None, config_settings, metadata_directory, editable=False
78-
).wheel_filename # actually returns the dist-info directory
78+
overrides = pyproject.get("tool", {}).get("scikit-build", {}).get("overrides", [])
79+
for override in overrides:
80+
if_override = override.get("if", {})
81+
if "failed" in if_override or "failed" in if_override.get("any", {}):
82+
return False
7983

84+
return True
8085

81-
def prepare_metadata_for_build_editable(
82-
metadata_directory: str,
83-
config_settings: dict[str, list[str] | str] | None = None,
84-
) -> str:
85-
"""Prepare metadata for building a wheel. Does not build the wheel. Returns the dist-info directory."""
8686

87-
from .wheel import _build_wheel_impl
87+
if _has_safe_metadata():
8888

89-
return _build_wheel_impl(
90-
None, config_settings, metadata_directory, editable=True
91-
).wheel_filename # actually returns the dist-info directory
89+
def prepare_metadata_for_build_wheel(
90+
metadata_directory: str,
91+
config_settings: dict[str, list[str] | str] | None = None,
92+
) -> str:
93+
"""Prepare metadata for building a wheel. Does not build the wheel. Returns the dist-info directory."""
94+
from .wheel import _build_wheel_impl
95+
96+
return _build_wheel_impl(
97+
None, config_settings, metadata_directory, editable=False
98+
).wheel_filename # actually returns the dist-info directory
99+
100+
def prepare_metadata_for_build_editable(
101+
metadata_directory: str,
102+
config_settings: dict[str, list[str] | str] | None = None,
103+
) -> str:
104+
"""Prepare metadata for building a wheel. Does not build the wheel. Returns the dist-info directory."""
105+
106+
from .wheel import _build_wheel_impl
107+
108+
return _build_wheel_impl(
109+
None, config_settings, metadata_directory, editable=True
110+
).wheel_filename # actually returns the dist-info directory
111+
112+
__all__ += [
113+
"prepare_metadata_for_build_wheel",
114+
"prepare_metadata_for_build_editable",
115+
]
92116

93117

94118
def build_sdist(

0 commit comments

Comments
 (0)