Skip to content

Commit d11e114

Browse files
committed
Support {root:uri} expansion similar to hatchling
1 parent 5f2782f commit d11e114

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/scikit_build_core/builder/get_requires.py

Lines changed: 18 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,21 @@ def _load_scikit_build_settings(
6667
return SettingsReader.from_file("pyproject.toml", config_settings).settings
6768

6869

70+
class RootPath(type(Path())): # type: ignore[misc]
71+
"""Handle ``{root:uri}`` like formatting similar to ``hatchling``."""
72+
73+
def __format__(self: Path, fmt: str) -> str:
74+
command, _, rest = fmt.partition(":")
75+
if command == "parent":
76+
return self.parent.__format__(rest)
77+
if command == "uri" and rest == "":
78+
return self.absolute().as_uri()
79+
if command == "" and rest == "":
80+
return str(self)
81+
msg = f"Could not handle format: {fmt}"
82+
raise ValueError(msg)
83+
84+
6985
@dataclasses.dataclass(frozen=True)
7086
class GetRequires:
7187
settings: ScikitBuildSettings = dataclasses.field(
@@ -140,7 +156,8 @@ def dynamic_metadata(self) -> Generator[str, None, None]:
140156
if self.settings.fail:
141157
return
142158

143-
yield from self.settings.build.build_requires
159+
for build_require in self.settings.build.build_requires:
160+
yield build_require.format(root=RootPath())
144161

145162
for dynamic_metadata in self.settings.metadata.values():
146163
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)