From d41060d91995777304047795662bfced7f1c8c7a Mon Sep 17 00:00:00 2001 From: Tim Paine <3105306+timkpaine@users.noreply.github.com> Date: Wed, 11 Nov 2026 23:20:51 -0500 Subject: [PATCH] Warn instead of failing when cargo-bundle-licenses is missing Building from an sdist does not need a license bundle; only release builds do. A missing collector now logs a warning and skips the bundle. Collector errors and missing license texts still fail the build. Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com> --- README.md | 5 ++++- hatch_rs/structs.py | 8 ++++++-- hatch_rs/tests/test_licenses.py | 23 ++++++++++++++++++++--- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index efdf01a..e67ee46 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,10 @@ Destination templates support `{module}`, `{target}`, `{profile}`, `{name}`, Configure the `licenses` table to collect dependency license texts with [cargo-bundle-licenses](https://github.com/sstadick/cargo-bundle-licenses). -Install the collector before building; hatch-rs does not install Cargo tools: +Install the collector where you build wheels for release; hatch-rs does not install +Cargo tools. When it is not installed, hatch-rs logs a warning and builds the artifact +without a license bundle, so source installs from an sdist do not need it. Once the +collector runs, missing license texts and collector errors still fail the build. ```sh cargo install cargo-bundle-licenses --version 4.2.0 --locked diff --git a/hatch_rs/structs.py b/hatch_rs/structs.py index 3524927..faf0594 100644 --- a/hatch_rs/structs.py +++ b/hatch_rs/structs.py @@ -7,6 +7,7 @@ from glob import glob from importlib.util import find_spec from json import dumps, loads +from logging import getLogger from os import environ from os.path import relpath from pathlib import Path @@ -24,6 +25,8 @@ from .symbols import exported_symbols +_logger = getLogger(__name__) + __all__ = ( "CargoInvocation", "CargoLicenseConfig", @@ -2108,8 +2111,9 @@ def _collect_licenses(self, planned_artifact: PlannedArtifact, *, build_root: Pa command.append("--check-previous") try: completed = run(command, cwd=manifest.parent, env=environment, check=False) - except FileNotFoundError as error: - raise RuntimeError(f"Artifact '{label}' license collection requires cargo-bundle-licenses. Install it before building.") from error + except FileNotFoundError: + _logger.warning("Artifact '%s' is built without a license bundle because cargo-bundle-licenses is not installed.", label) + return if completed.returncode != 0: raise RuntimeError(f"Artifact '{label}' license collection failed with exit code {completed.returncode}: {shell_join(command)}") diff --git a/hatch_rs/tests/test_licenses.py b/hatch_rs/tests/test_licenses.py index 9cd05fa..6adf84c 100644 --- a/hatch_rs/tests/test_licenses.py +++ b/hatch_rs/tests/test_licenses.py @@ -210,15 +210,32 @@ def test_missing_license_text_fails_the_build(tmp_path, cargo_runner, monkeypatc assert not (tmp_path / "project/licenses/cargo.json").exists() -@pytest.mark.parametrize("failure", ["missing-tool", "command-failed", "missing-output", "invalid-json"]) +def test_missing_collector_warns_and_skips_the_bundle(tmp_path, cargo_runner, monkeypatch, caplog): + original_run = structs.run + + def missing_collector(args, **kwargs): + if args[0] == "cargo-bundle-licenses": + raise FileNotFoundError("cargo-bundle-licenses") + return original_run(args, **kwargs) + + monkeypatch.setattr("hatch_rs.structs.run", missing_collector) + plan = make_plan(tmp_path, licenses={}) + + try: + plan.execute() + finally: + plan.cleanup() + assert "cargo-bundle-licenses is not installed" in caplog.text + assert not (tmp_path / "project/licenses/cargo.json").exists() + + +@pytest.mark.parametrize("failure", ["command-failed", "missing-output", "invalid-json"]) def test_collector_failures_are_not_silently_packaged(tmp_path, cargo_runner, monkeypatch, failure): original_run = structs.run def failing_run(args, **kwargs): if args[0] != "cargo-bundle-licenses": return original_run(args, **kwargs) - if failure == "missing-tool": - raise FileNotFoundError("cargo-bundle-licenses") if failure == "invalid-json": Path(args[args.index("--output") + 1]).write_text("not JSON") return CompletedProcess(args, 1 if failure == "command-failed" else 0)