Skip to content

Commit 96e089d

Browse files
committed
Support {root:uri} expansion similar to hatchling
Signed-off-by: Cristian Le <[email protected]>
1 parent d18a266 commit 96e089d

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/scikit_build_core/builder/get_requires.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import importlib.util
66
import os
77
import sysconfig
8+
from pathlib import Path
89
from typing import TYPE_CHECKING, Literal
910

1011
from packaging.tags import sys_tags
@@ -66,6 +67,25 @@ def _load_scikit_build_settings(
6667
return SettingsReader.from_file("pyproject.toml", config_settings).settings
6768

6869

70+
class RootPathResolver:
71+
"""Handle ``{root:uri}`` like formatting similar to ``hatchling``."""
72+
73+
def __init__(self, path: Path | None = None) -> None:
74+
self.path = path if path else Path()
75+
76+
def __format__(self, fmt: str) -> str:
77+
command, _, rest = fmt.partition(":")
78+
if command == "parent":
79+
parent = RootPathResolver(self.path.parent)
80+
return parent.__format__(rest)
81+
if command == "uri" and rest == "":
82+
return self.path.absolute().as_uri()
83+
if command == "" and rest == "":
84+
return str(self)
85+
msg = f"Could not handle format: {fmt}"
86+
raise ValueError(msg)
87+
88+
6989
@dataclasses.dataclass(frozen=True)
7090
class GetRequires:
7191
settings: ScikitBuildSettings = dataclasses.field(
@@ -140,7 +160,8 @@ def dynamic_metadata(self) -> Generator[str, None, None]:
140160
if self.settings.fail:
141161
return
142162

143-
yield from self.settings.build.build_requires
163+
for build_require in self.settings.build.build_requires:
164+
yield build_require.format(root=RootPathResolver())
144165

145166
for dynamic_metadata in self.settings.metadata.values():
146167
if "provider" in dynamic_metadata:

tests/packages/dynamic_metadata/build_requires_project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ build-requires = ["foo"]
1010

1111
[[tool.scikit-build.overrides]]
1212
if.env.LOCAL_FOO = true
13-
build.build-requires = ["foo @ {root:uri}/foo"]
13+
build.build-requires = ["foo @ {root:parent:uri}/foo"]

tests/test_dynamic_metadata.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,8 @@ def test_build_requires_field(override, monkeypatch) -> None:
360360
else:
361361
monkeypatch.delenv("LOCAL_FOO", raising=False)
362362

363-
with Path("pyproject.toml").open("rb") as ft:
363+
pyproject_path = Path("pyproject.toml")
364+
with pyproject_path.open("rb") as ft:
364365
pyproject = tomllib.load(ft)
365366
state: Literal["sdist", "metadata_wheel"] = (
366367
"sdist" if override == "sdist" else "metadata_wheel"
@@ -374,9 +375,11 @@ def test_build_requires_field(override, monkeypatch) -> None:
374375
"foo",
375376
}
376377
elif override == "env":
378+
# evaluate ../foo as uri
379+
foo_path = pyproject_path.parent.parent / "foo"
380+
foo_path = foo_path.absolute()
377381
assert set(GetRequires().dynamic_metadata()) == {
378-
# TODO: This should be resolved to actual path
379-
"foo @ {root:uri}/foo",
382+
f"foo @ {foo_path.as_uri()}",
380383
}
381384
elif override == "sdist":
382385
assert set(GetRequires().dynamic_metadata()) == {

0 commit comments

Comments
 (0)