Skip to content

Commit d123fd2

Browse files
authored
fix: support overriding renamed options (#1054)
Fix #912. Now envvar and config options won't trigger the error, only statically defined ones will. And you can override a static option with the renamed new option. Also minor fix for a missing `env=env` passthrough for the API. Signed-off-by: Henry Schreiner <[email protected]>
1 parent a8d15c5 commit d123fd2

File tree

2 files changed

+151
-24
lines changed

2 files changed

+151
-24
lines changed

src/scikit_build_core/settings/skbuild_read_settings.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,18 @@ def _handle_move(
9292
after: T,
9393
minimum_version: Version | None,
9494
introduced_in: Version,
95+
*,
96+
static: bool,
9597
) -> T:
9698
"""
9799
Backward_compat for moving names around. The default must be false-like.
98100
"""
99-
100-
if after and minimum_version is not None and minimum_version < introduced_in:
101+
if (
102+
static
103+
and after
104+
and minimum_version is not None
105+
and minimum_version < introduced_in
106+
):
101107
rich_error(
102108
f"Cannot set {after_name} if minimum-version is set to less than {introduced_in} (which is where it was introduced)"
103109
)
@@ -112,6 +118,8 @@ def _handle_move(
112118
)
113119

114120
if before is not None and after:
121+
if not static:
122+
return after
115123
rich_error(f"Cannot set {before_name} and {after_name} at the same time")
116124

117125
if before is None:
@@ -190,14 +198,18 @@ def __init__(
190198
k: v for k, v in config_settings.items() if not k.startswith("skbuild.")
191199
}
192200
self.sources = SourceChain(
193-
EnvSource("SKBUILD"),
201+
EnvSource("SKBUILD", env=env),
194202
ConfSource("skbuild", settings=prefixed, verify=verify_conf),
195203
ConfSource(settings=remaining, verify=verify_conf),
196204
*toml_srcs,
197205
prefixes=["tool", "scikit-build"],
198206
)
199207
self.settings = self.sources.convert_target(ScikitBuildSettings)
200208

209+
static_settings = SourceChain(
210+
*toml_srcs, prefixes=["tool", "scikit-build"]
211+
).convert_target(ScikitBuildSettings)
212+
201213
if self.settings.minimum_version:
202214
current_version = Version(__version__)
203215
minimum_version = self.settings.minimum_version
@@ -285,6 +297,8 @@ def __init__(
285297
self.settings.build.verbose,
286298
self.settings.minimum_version,
287299
Version("0.10"),
300+
static=static_settings.cmake.verbose == self.settings.cmake.verbose
301+
and static_settings.build.verbose == self.settings.build.verbose,
288302
)
289303
self.settings.build.targets = _handle_move(
290304
"cmake.targets",
@@ -293,6 +307,8 @@ def __init__(
293307
self.settings.build.targets,
294308
self.settings.minimum_version,
295309
Version("0.10"),
310+
static=static_settings.cmake.targets == self.settings.cmake.targets
311+
and static_settings.build.targets == self.settings.build.targets,
296312
)
297313

298314
def unrecognized_options(self) -> Generator[str, None, None]:

tests/test_skbuild_settings.py

Lines changed: 132 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -624,27 +624,6 @@ def test_backcompat_cmake_build_env(tmp_path: Path, monkeypatch: pytest.MonkeyPa
624624
assert settings_reader.settings.build.targets == ["a", "b"]
625625

626626

627-
def test_backcompat_cmake_build_both_specified(
628-
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
629-
):
630-
monkeypatch.setattr(
631-
scikit_build_core.settings.skbuild_read_settings, "__version__", "0.10.0"
632-
)
633-
pyproject_toml = tmp_path / "pyproject.toml"
634-
pyproject_toml.write_text(
635-
textwrap.dedent(
636-
"""\
637-
[tool.scikit-build]
638-
cmake.verbose = true
639-
"""
640-
),
641-
encoding="utf-8",
642-
)
643-
644-
with pytest.raises(SystemExit):
645-
SettingsReader.from_file(pyproject_toml, {"build.verbose": "1"})
646-
647-
648627
def test_auto_minimum_version(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
649628
monkeypatch.setattr(
650629
scikit_build_core.settings.skbuild_read_settings, "__version__", "0.10.0"
@@ -813,3 +792,135 @@ def test_skbuild_settings_cmake_define_list():
813792
"NESTED_LIST": r"Apple;Lemon\;Lime;Banana",
814793
"ONE_LEVEL_LIST": "Foo;Bar;ExceptionallyLargeListEntryThatWouldOverflowTheLine;Baz",
815794
}
795+
796+
797+
def test_skbuild_override_renamed(
798+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
799+
) -> None:
800+
monkeypatch.setattr(
801+
scikit_build_core.settings.skbuild_read_settings, "__version__", "0.10.0"
802+
)
803+
pyproject_toml = tmp_path / "pyproject.toml"
804+
pyproject_toml.write_text(
805+
textwrap.dedent(
806+
"""\
807+
[tool.scikit-build]
808+
minimum-version = "0.7"
809+
"""
810+
),
811+
encoding="utf-8",
812+
)
813+
814+
settings_reader = SettingsReader.from_file(pyproject_toml, {"build.verbose": "1"})
815+
assert settings_reader.settings.build.verbose
816+
817+
settings_reader = SettingsReader.from_file(
818+
pyproject_toml, env={"SKBUILD_BUILD_VERBOSE": "TRUE"}
819+
)
820+
assert settings_reader.settings.build.verbose
821+
822+
823+
def test_skbuild_override_renamed_fail(
824+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
825+
) -> None:
826+
monkeypatch.setattr(
827+
scikit_build_core.settings.skbuild_read_settings, "__version__", "0.10.0"
828+
)
829+
pyproject_toml = tmp_path / "pyproject.toml"
830+
pyproject_toml.write_text(
831+
textwrap.dedent(
832+
"""\
833+
[tool.scikit-build]
834+
minimum-version = "0.7"
835+
build.verbose = true
836+
"""
837+
),
838+
encoding="utf-8",
839+
)
840+
841+
with pytest.raises(SystemExit):
842+
SettingsReader.from_file(pyproject_toml)
843+
844+
845+
def test_skbuild_override_static(
846+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
847+
) -> None:
848+
monkeypatch.setattr(
849+
scikit_build_core.settings.skbuild_read_settings, "__version__", "0.10.0"
850+
)
851+
pyproject_toml = tmp_path / "pyproject.toml"
852+
pyproject_toml.write_text(
853+
textwrap.dedent(
854+
"""\
855+
[tool.scikit-build]
856+
minimum-version = "0.7"
857+
build.verbose = true
858+
"""
859+
),
860+
encoding="utf-8",
861+
)
862+
with pytest.raises(SystemExit):
863+
SettingsReader.from_file(pyproject_toml)
864+
865+
866+
def test_backcompat_cmake_build_both_specified(
867+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
868+
):
869+
monkeypatch.setattr(
870+
scikit_build_core.settings.skbuild_read_settings, "__version__", "0.10.0"
871+
)
872+
pyproject_toml = tmp_path / "pyproject.toml"
873+
pyproject_toml.write_text(
874+
textwrap.dedent(
875+
"""\
876+
[tool.scikit-build]
877+
cmake.verbose = true
878+
"""
879+
),
880+
encoding="utf-8",
881+
)
882+
883+
settings_reader = SettingsReader.from_file(pyproject_toml, {"build.verbose": "1"})
884+
assert settings_reader.settings.build.verbose
885+
886+
887+
def test_backcompat_cmake_build_both_specified_static(
888+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
889+
):
890+
monkeypatch.setattr(
891+
scikit_build_core.settings.skbuild_read_settings, "__version__", "0.10.0"
892+
)
893+
pyproject_toml = tmp_path / "pyproject.toml"
894+
pyproject_toml.write_text(
895+
textwrap.dedent(
896+
"""\
897+
[tool.scikit-build]
898+
build.verbose = true
899+
"""
900+
),
901+
encoding="utf-8",
902+
)
903+
904+
settings_reader = SettingsReader.from_file(pyproject_toml, {"cmake.verbose": "1"})
905+
assert settings_reader.settings.build.verbose
906+
907+
908+
def test_backcompat_cmake_build_targets_both_specified(
909+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
910+
):
911+
monkeypatch.setattr(
912+
scikit_build_core.settings.skbuild_read_settings, "__version__", "0.10.0"
913+
)
914+
pyproject_toml = tmp_path / "pyproject.toml"
915+
pyproject_toml.write_text(
916+
textwrap.dedent(
917+
"""\
918+
[tool.scikit-build]
919+
cmake.targets = ["a", "b"]
920+
"""
921+
),
922+
encoding="utf-8",
923+
)
924+
925+
settings_reader = SettingsReader.from_file(pyproject_toml, {"build.targets": "c;d"})
926+
assert settings_reader.settings.build.targets == ["c", "d"]

0 commit comments

Comments
 (0)