Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions hatch_rs/structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,6 +25,8 @@

from .symbols import exported_symbols

_logger = getLogger(__name__)

__all__ = (
"CargoInvocation",
"CargoLicenseConfig",
Expand Down Expand Up @@ -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)}")

Expand Down
23 changes: 20 additions & 3 deletions hatch_rs/tests/test_licenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading