Skip to content

Commit a212129

Browse files
marbreclaude
andauthored
Fix IndexError when only generic artifacts exist (#3767)
`Parameters.__init__` raised `IndexError` when only generic artifacts were present (e.g. a core-only or headers-only build). Make `default_target_family` `Optional[str]`, set to `None` when the family set is empty, and guard the `DEFAULT_TARGET_FAMILY` line in the generated `dist_info` contents accordingly. The template's fallback value `DEFAULT_TARGET_FAMILY = "DEFAULT"` is a sentinel for the no-families case; since `AVAILABLE_TARGET_FAMILIES` is also empty in that case, any call to `determine_target_family()` will raise a `ValueError` regardless. Co-authored-by: Claude <[email protected]>
1 parent ad52488 commit a212129

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

build_tools/_therock_utils/py_packaging.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ def __init__(
8080
self.version_suffix = version_suffix
8181
self.artifacts = artifacts
8282
self.all_target_families = artifacts.all_target_families
83-
self.default_target_family = sorted(self.all_target_families)[0]
83+
_sorted_families = sorted(self.all_target_families)
84+
self.default_target_family: str | None = (
85+
_sorted_families[0] if _sorted_families else None
86+
)
8487
self.populated_packages: list["PopulatedDistPackage"] = []
8588
self.runtime_artifact_names: set[str] = set()
8689

@@ -96,9 +99,10 @@ def __init__(
9699
# Full: base extended with all families. Used by most packages and by
97100
# the dynamically loaded self.dist_info module below.
98101
dist_info_contents = dist_info_base
99-
dist_info_contents += (
100-
f"DEFAULT_TARGET_FAMILY = '{self.default_target_family}'\n"
101-
)
102+
if self.default_target_family is not None:
103+
dist_info_contents += (
104+
f"DEFAULT_TARGET_FAMILY = '{self.default_target_family}'\n"
105+
)
102106
for target_family in self.all_target_families:
103107
dist_info_contents += (
104108
f"AVAILABLE_TARGET_FAMILIES.append('{target_family}')\n"

build_tools/tests/py_packaging_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,26 @@ def test_find_populated_falls_back_to_generic_package(self):
396396
)
397397

398398

399+
# ---------------------------------------------------------------------------
400+
# Tests for Parameters construction edge cases
401+
# ---------------------------------------------------------------------------
402+
403+
404+
class ParametersConstructionTest(TmpDirTestCase):
405+
def test_no_arch_specific_artifacts_does_not_crash(self):
406+
# Regression: Parameters.__init__ raised IndexError when all_target_families
407+
# was empty because it did sorted(...)[0] unconditionally.
408+
artifact_dir = self.temp_dir / "artifacts"
409+
artifact_dir.mkdir()
410+
params = Parameters(
411+
dest_dir=self.temp_dir / "packages",
412+
version="0.0.1.test",
413+
version_suffix="",
414+
artifacts=ArtifactCatalog(artifact_dir),
415+
)
416+
self.assertIsNone(params.default_target_family)
417+
418+
399419
# ---------------------------------------------------------------------------
400420
# Unit tests for restrict_families (per-family meta package)
401421
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)