From d73a7680404432bc960622397cd9fd7fa2619a92 Mon Sep 17 00:00:00 2001 From: Martin Haug Date: Thu, 19 Nov 2026 23:36:32 +0100 Subject: [PATCH] Search NOTICE in parents --- README.md | 2 +- src/cargo/config.rs | 46 +++++++++++++++++++++++++-------------------- src/cargo/mod.rs | 1 + src/cli/release.rs | 11 +++++++---- tests/cargo.rs | 12 +++++++++++- 5 files changed, 46 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 5554a5f..cee5d3c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/cargo/config.rs b/src/cargo/config.rs index 76a9b61..508d42f 100644 --- a/src/cargo/config.rs +++ b/src/cargo/config.rs @@ -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 { - 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::().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. @@ -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 { + 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::().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)] diff --git a/src/cargo/mod.rs b/src/cargo/mod.rs index 90ffa32..f3c4e9f 100644 --- a/src/cargo/mod.rs +++ b/src/cargo/mod.rs @@ -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}; diff --git a/src/cli/release.rs b/src/cli/release.rs index bae03a4..1eaab03 100644 --- a/src/cli/release.rs +++ b/src/cli/release.rs @@ -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. @@ -41,8 +42,10 @@ pub fn run(args: &Args) -> ExitCode { fn write(args: &Args) -> Result { 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(¬ice)?) } 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:#}"))?; diff --git a/tests/cargo.rs b/tests/cargo.rs index b3a6e9e..399739c 100644 --- a/tests/cargo.rs +++ b/tests/cargo.rs @@ -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. @@ -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(¬ice, "").unwrap(); + assert_eq!(find_in_workspace(&manifest, "NOTICE"), Some(notice)); } }