Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- next-header -->
## [Unreleased] - ReleaseDate
### Changed
- [PR#802](https://github.com/EmbarkStudios/cargo-deny/pull/802) made relative paths passed to `--config` be resolved relative to the current working directory (rather than the resolved manifest path's directory).
### Fixed
- [PR#802](https://github.com/EmbarkStudios/cargo-deny/pull/802) fixed path handling of paths passed to `--config` ([#748](https://github.com/EmbarkStudios/krates/issues/748)).


## [0.18.5] - 2025-09-22
### Changed
- [PR#789](https://github.com/EmbarkStudios/cargo-deny/pull/789) changed it so that release binaries are now built with LTO.
Expand Down
2 changes: 1 addition & 1 deletion src/cargo-deny/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub(crate) fn cmd(
graph,
output,
} = ValidConfig::load(
krate_ctx.get_config_path(args.config.clone()),
krate_ctx.get_config_path(args.config.as_deref())?,
krate_ctx.get_local_exceptions_path(),
&mut files,
log_ctx,
Expand Down
120 changes: 94 additions & 26 deletions src/cargo-deny/common.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::Context;
use cargo_deny::{
PathBuf,
Path, PathBuf,
diag::{self, FileId, Files, Severity},
licenses::LicenseStore,
};
Expand All @@ -12,6 +13,17 @@ pub(crate) fn load_license_store() -> Result<LicenseStore, anyhow::Error> {
LicenseStore::from_cache()
}

pub(crate) fn current_dir_utf8() -> anyhow::Result<PathBuf> {
let current_dir =
std::env::current_dir().context("could not access current working directory")?;

let current_dir = PathBuf::from_path_buf(current_dir)
.map_err(|pb| anyhow::anyhow!("path '{}' is not utf-8", pb.display()))
.context("could not access current working directory")?;

Ok(current_dir)
}

pub struct KrateContext {
pub manifest_path: PathBuf,
pub workspace: bool,
Expand All @@ -28,41 +40,51 @@ pub struct KrateContext {
}

impl KrateContext {
pub fn get_config_path(&self, config_path: Option<PathBuf>) -> Option<PathBuf> {
if let Some(cp) = config_path {
if cp.is_absolute() {
Some(cp)
} else {
Some(self.manifest_path.parent().unwrap().join(cp))
}
pub fn get_config_path(&self, config_path: Option<&Path>) -> anyhow::Result<Option<PathBuf>> {
if let Some(config_path) = config_path {
let current_dir = current_dir_utf8()?;
Self::resolve_config_path(&current_dir, config_path).map(Some)
} else {
let mut p = self.manifest_path.parent();
Ok(Self::default_config_path(&self.manifest_path))
}
}

while let Some(parent) = p {
let mut config_path = parent.join("deny.toml");
fn resolve_config_path(current_dir: &Path, config_path: &Path) -> anyhow::Result<PathBuf> {
if config_path.is_absolute() {
Ok(config_path.to_owned())
} else {
Ok(current_dir.join(config_path))
}
}

if config_path.exists() {
return Some(config_path);
}
fn default_config_path(manifest_path: &Path) -> Option<PathBuf> {
let mut p = manifest_path.parent();

config_path.pop();
config_path.push(".deny.toml");
while let Some(parent) = p {
let mut config_path = parent.join("deny.toml");

if config_path.exists() {
return Some(config_path);
}
if config_path.exists() {
return Some(config_path);
}

config_path.pop();
config_path.push(".cargo/deny.toml");
if config_path.exists() {
return Some(config_path);
}
config_path.pop();
config_path.push(".deny.toml");

if config_path.exists() {
return Some(config_path);
}

p = parent.parent();
config_path.pop();
config_path.push(".cargo/deny.toml");

if config_path.exists() {
return Some(config_path);
}

None
p = parent.parent();
}

None
}

pub fn get_local_exceptions_path(&self) -> Option<PathBuf> {
Expand Down Expand Up @@ -545,3 +567,49 @@ impl<'a> DiagPrinter<'a> {
self.which.lock(self.max_severity)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[inline]
fn temp_dir() -> tempfile::TempDir {
tempfile::tempdir().unwrap()
}

#[test]
fn test_resolve_config_path() {
let temp_dir = temp_dir();

let current_dir = PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
std::fs::File::create(current_dir.join("deny.toml")).unwrap();

let expected = current_dir.join("deny.toml");

let abs_path = current_dir.join("deny.toml");
let actual = KrateContext::resolve_config_path(&current_dir, &abs_path).unwrap();
assert_eq!(actual, abs_path);

let rel_path = Path::new("./deny.toml");
let actual = KrateContext::resolve_config_path(&current_dir, rel_path).unwrap();
assert_eq!(actual, expected);
}

#[test]
fn test_default_config_path() {
let temp_dir = temp_dir();

let current_dir = PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
std::fs::File::create(current_dir.join("Cargo.toml")).unwrap();
let manifest_path = current_dir.join("Cargo.toml");

let actual = KrateContext::default_config_path(&manifest_path);
let expected = None;
assert_eq!(actual, expected);

std::fs::File::create(current_dir.join("deny.toml")).unwrap();
let actual = KrateContext::default_config_path(&manifest_path);
let expected = Some(current_dir.join("deny.toml"));
assert_eq!(actual, expected);
}
}
2 changes: 1 addition & 1 deletion src/cargo-deny/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn cmd(
args: Args,
krate_ctx: crate::common::KrateContext,
) -> Result<(), Error> {
let cfg_path = krate_ctx.get_config_path(args.config.clone());
let cfg_path = krate_ctx.get_config_path(args.config.as_deref())?;

let mut files = Files::new();
let ValidConfig { advisories, .. } = ValidConfig::load(
Expand Down
9 changes: 5 additions & 4 deletions src/cargo-deny/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ pub struct Args {
const CONTENTS: &[u8] = include_bytes!("../../deny.template.toml");

pub fn cmd(args: Args, ctx: crate::common::KrateContext) -> Result<(), Error> {
let cfg_path = args.config.unwrap_or_else(|| PathBuf::from("deny.toml"));
let cfg_path = ctx
.get_config_path(Some(cfg_path))
.context("unable to get full path to config")?;
let cfg_path = {
let cfg_path = args.config.unwrap_or_else(|| PathBuf::from("deny.toml"));
ctx.get_config_path(Some(&cfg_path))?
.context("unable to get full path to config")?
};

// make sure the file does not exist yet
ensure!(
Expand Down
2 changes: 1 addition & 1 deletion src/cargo-deny/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn cmd(
use licenses::LicenseInfo;
use std::{collections::BTreeMap, fmt::Write};

let cfg_path = krate_ctx.get_config_path(args.config.clone());
let cfg_path = krate_ctx.get_config_path(args.config.as_deref())?;

let mut files = Files::new();
let ValidConfig { graph, .. } = ValidConfig::load(
Expand Down