Skip to content

Commit e133d8f

Browse files
authored
Warn instead of failing when cargo-bundle-licenses is missing (#96)
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>
1 parent 067ba3c commit e133d8f

3 files changed

Lines changed: 30 additions & 6 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ Destination templates support `{module}`, `{target}`, `{profile}`, `{name}`,
5151

5252
Configure the `licenses` table to collect dependency license texts with
5353
[cargo-bundle-licenses](https://github.com/sstadick/cargo-bundle-licenses).
54-
Install the collector before building; hatch-rs does not install Cargo tools:
54+
Install the collector where you build wheels for release; hatch-rs does not install
55+
Cargo tools. When it is not installed, hatch-rs logs a warning and builds the artifact
56+
without a license bundle, so source installs from an sdist do not need it. Once the
57+
collector runs, missing license texts and collector errors still fail the build.
5558

5659
```sh
5760
cargo install cargo-bundle-licenses --version 4.2.0 --locked

‎hatch_rs/structs.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from glob import glob
88
from importlib.util import find_spec
99
from json import dumps, loads
10+
from logging import getLogger
1011
from os import environ
1112
from os.path import relpath
1213
from pathlib import Path
@@ -24,6 +25,8 @@
2425

2526
from .symbols import exported_symbols
2627

28+
_logger = getLogger(__name__)
29+
2730
__all__ = (
2831
"CargoInvocation",
2932
"CargoLicenseConfig",
@@ -2108,8 +2111,9 @@ def _collect_licenses(self, planned_artifact: PlannedArtifact, *, build_root: Pa
21082111
command.append("--check-previous")
21092112
try:
21102113
completed = run(command, cwd=manifest.parent, env=environment, check=False)
2111-
except FileNotFoundError as error:
2112-
raise RuntimeError(f"Artifact '{label}' license collection requires cargo-bundle-licenses. Install it before building.") from error
2114+
except FileNotFoundError:
2115+
_logger.warning("Artifact '%s' is built without a license bundle because cargo-bundle-licenses is not installed.", label)
2116+
return
21132117
if completed.returncode != 0:
21142118
raise RuntimeError(f"Artifact '{label}' license collection failed with exit code {completed.returncode}: {shell_join(command)}")
21152119

‎hatch_rs/tests/test_licenses.py‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,32 @@ def test_missing_license_text_fails_the_build(tmp_path, cargo_runner, monkeypatc
210210
assert not (tmp_path / "project/licenses/cargo.json").exists()
211211

212212

213-
@pytest.mark.parametrize("failure", ["missing-tool", "command-failed", "missing-output", "invalid-json"])
213+
def test_missing_collector_warns_and_skips_the_bundle(tmp_path, cargo_runner, monkeypatch, caplog):
214+
original_run = structs.run
215+
216+
def missing_collector(args, **kwargs):
217+
if args[0] == "cargo-bundle-licenses":
218+
raise FileNotFoundError("cargo-bundle-licenses")
219+
return original_run(args, **kwargs)
220+
221+
monkeypatch.setattr("hatch_rs.structs.run", missing_collector)
222+
plan = make_plan(tmp_path, licenses={})
223+
224+
try:
225+
plan.execute()
226+
finally:
227+
plan.cleanup()
228+
assert "cargo-bundle-licenses is not installed" in caplog.text
229+
assert not (tmp_path / "project/licenses/cargo.json").exists()
230+
231+
232+
@pytest.mark.parametrize("failure", ["command-failed", "missing-output", "invalid-json"])
214233
def test_collector_failures_are_not_silently_packaged(tmp_path, cargo_runner, monkeypatch, failure):
215234
original_run = structs.run
216235

217236
def failing_run(args, **kwargs):
218237
if args[0] != "cargo-bundle-licenses":
219238
return original_run(args, **kwargs)
220-
if failure == "missing-tool":
221-
raise FileNotFoundError("cargo-bundle-licenses")
222239
if failure == "invalid-json":
223240
Path(args[args.index("--output") + 1]).write_text("not JSON")
224241
return CompletedProcess(args, 1 if failure == "command-failed" else 0)

0 commit comments

Comments
 (0)