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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ files = [{ path = "LICENSE.txt", checksum = "88abfa95…", start = "rust-lang/li

A release reads the NOTICE files of dependencies, too: items of a dependency's Typst NOTICE file that have no `Package` get the dependency as their package, so their paths are interpreted relative to the package root, which equals the repository root for crates at the root of their repository.

`release` does not check licenses, so run `check --deps` first, for example in the same workflow. It fails for dependencies from git repositories, whose licenses are not pinned to a published version. The `--target` option of both commands replaces `targets`.
`release` does not check licenses, so run `check --deps` first, for example in the same workflow. It includes the repository's NOTICE file, which it looks for next to the manifest and then in each parent directory up to the workspace root. It fails for dependencies from git repositories, whose licenses are not pinned to a published version. The `--target` option of both commands replaces `targets`.

## GitHub Action

Expand Down
46 changes: 26 additions & 20 deletions src/cargo/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,10 @@ impl Config {
text.parse().map_err(|err| format!("{}: {err}", path.display()))
}

/// Finds the configuration file for the project with the given manifest.
///
/// Looks for [`CONFIG_PATH`] in the manifest's directory and then in each
/// parent directory. The search ends after the root of the workspace, i.e.
/// the first directory whose `Cargo.toml` has a `[workspace]` table, so that
/// the configuration of an unrelated enclosing project is never picked up.
/// Finds the configuration file for the project with the given manifest:
/// [`CONFIG_PATH`] in the workspace root, see [`find_in_workspace`].
pub fn find(manifest_path: &Path) -> Option<PathBuf> {
let manifest_path = std::path::absolute(manifest_path).ok()?;
for dir in manifest_path.parent()?.ancestors() {
let candidate = dir.join(CONFIG_PATH);
if candidate.is_file() {
return Some(candidate);
}

let manifest = std::fs::read_to_string(dir.join("Cargo.toml"));
let table = manifest.ok().and_then(|text| text.parse::<toml::Table>().ok());
if table.is_some_and(|table| table.contains_key("workspace")) {
break;
}
}
None
find_in_workspace(manifest_path, CONFIG_PATH)
}

/// Replaces the targets, e.g. with ones given on the command line.
Expand All @@ -100,6 +83,29 @@ impl FromStr for Config {
}
}

/// Finds a file of the project with the given manifest.
///
/// Looks for `relative` in the manifest's directory and then in each parent
/// directory. The search ends after the root of the workspace, i.e. the first
/// directory whose `Cargo.toml` has a `[workspace]` table, so that a file of an
/// unrelated enclosing project is never picked up.
pub fn find_in_workspace(manifest_path: &Path, relative: &str) -> Option<PathBuf> {
let manifest_path = std::path::absolute(manifest_path).ok()?;
for dir in manifest_path.parent()?.ancestors() {
let candidate = dir.join(relative);
if candidate.is_file() {
return Some(candidate);
}

let manifest = std::fs::read_to_string(dir.join("Cargo.toml"));
let table = manifest.ok().and_then(|text| text.parse::<toml::Table>().ok());
if table.is_some_and(|table| table.contains_key("workspace")) {
break;
}
}
None
}

/// Configures which crates are private and thus not checked.
#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
Expand Down
1 change: 1 addition & 0 deletions src/cargo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::path::PathBuf;

pub use self::config::{
CONFIG_PATH, Clarification, ClarificationFile, Config, Exception, Private,
find_in_workspace,
};

use crate::{LicenseBlock, Usage};
Expand Down
11 changes: 7 additions & 4 deletions src/cli/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ use std::process::ExitCode;

use clap::Parser;
use notices::ErrorKind;
use notices::cargo::release;
use notices::cargo::{find_in_workspace, release};

use super::ProjectArgs;

/// Writes the NOTICE file and JSON document for a release of a Cargo project.
///
/// Merges the licenses of all dependencies with the project's own NOTICE file,
/// which is `NOTICE` next to the manifest, if there is one.
/// if there is one. It is the first file named `NOTICE` in the directory of the
/// manifest or a parent directory up to the workspace root.
#[derive(Parser)]
pub struct Args {
/// Where to write the licenses. Can be repeated or separated by commas.
Expand Down Expand Up @@ -41,8 +42,10 @@ pub fn run(args: &Args) -> ExitCode {
fn write(args: &Args) -> Result<bool, String> {
let (config, options) = args.project.load(Path::new("."))?;

let notice = options.manifest_path.with_file_name("NOTICE");
let repo_notice = if notice.is_file() { Some(read_notice(&notice)?) } else { None };
let repo_notice = match find_in_workspace(&options.manifest_path, "NOTICE") {
Some(path) => Some(read_notice(&path)?),
None => None,
};

let (blocks, report) =
release(repo_notice, &config, &options).map_err(|e| format!("{e:#}"))?;
Expand Down
12 changes: 11 additions & 1 deletion tests/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use std::path::Path;

use notices::cargo::{CONFIG_PATH, Config, MetadataOptions, Report, check, release};
use notices::cargo::{
CONFIG_PATH, Config, MetadataOptions, Report, check, find_in_workspace, release,
};
use notices::{Inclusion, LicenseBlock};

/// The checksum of `crates/custom/LICENSE` in the `cargo` fixture.
Expand Down Expand Up @@ -170,6 +172,14 @@ mod config {
// A workspace root ends the search.
let (_, manifest) = tree("stop", "[workspace]\nmembers = [\"package\"]\n");
assert_eq!(Config::find(&manifest), None);

// Other files are found the same way, up to and including the
// workspace root.
let (root, manifest) = tree("notice", "[workspace]\nmembers = [\"package\"]\n");
assert_eq!(find_in_workspace(&manifest, "NOTICE"), None);
let notice = root.join("workspace/NOTICE");
std::fs::write(&notice, "").unwrap();
assert_eq!(find_in_workspace(&manifest, "NOTICE"), Some(notice));
}
}

Expand Down