Skip to content

Commit 189c5ce

Browse files
committed
Properly resolve paths passed to --config, relative to $PWD, rather than --manifest-path
1 parent 7e0cff3 commit 189c5ce

File tree

5 files changed

+102
-33
lines changed

5 files changed

+102
-33
lines changed

src/cargo-deny/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ pub(crate) fn cmd(
137137
graph,
138138
output,
139139
} = ValidConfig::load(
140-
krate_ctx.get_config_path(args.config.clone()),
140+
krate_ctx.get_config_path(args.config.as_deref())?,
141141
krate_ctx.get_local_exceptions_path(),
142142
&mut files,
143143
log_ctx,

src/cargo-deny/common.rs

Lines changed: 94 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use anyhow::Context;
12
use cargo_deny::{
2-
PathBuf,
3+
Path, PathBuf,
34
diag::{self, FileId, Files, Severity},
45
licenses::LicenseStore,
56
};
@@ -12,6 +13,17 @@ pub(crate) fn load_license_store() -> Result<LicenseStore, anyhow::Error> {
1213
LicenseStore::from_cache()
1314
}
1415

16+
pub(crate) fn current_dir_utf8() -> anyhow::Result<PathBuf> {
17+
let current_dir =
18+
std::env::current_dir().context("could not access current working directory")?;
19+
20+
let current_dir = PathBuf::from_path_buf(current_dir)
21+
.map_err(|pb| anyhow::anyhow!("path '{}' is not utf-8", pb.display()))
22+
.context("could not access current working directory")?;
23+
24+
Ok(current_dir)
25+
}
26+
1527
pub struct KrateContext {
1628
pub manifest_path: PathBuf,
1729
pub workspace: bool,
@@ -28,41 +40,51 @@ pub struct KrateContext {
2840
}
2941

3042
impl KrateContext {
31-
pub fn get_config_path(&self, config_path: Option<PathBuf>) -> Option<PathBuf> {
32-
if let Some(cp) = config_path {
33-
if cp.is_absolute() {
34-
Some(cp)
35-
} else {
36-
Some(self.manifest_path.parent().unwrap().join(cp))
37-
}
43+
pub fn get_config_path(&self, config_path: Option<&Path>) -> anyhow::Result<Option<PathBuf>> {
44+
if let Some(config_path) = config_path {
45+
let current_dir = current_dir_utf8()?;
46+
Self::resolve_config_path(&current_dir, config_path).map(Some)
3847
} else {
39-
let mut p = self.manifest_path.parent();
48+
Ok(Self::default_config_path(&self.manifest_path))
49+
}
50+
}
4051

41-
while let Some(parent) = p {
42-
let mut config_path = parent.join("deny.toml");
52+
fn resolve_config_path(current_dir: &Path, config_path: &Path) -> anyhow::Result<PathBuf> {
53+
if config_path.is_absolute() {
54+
Ok(config_path.to_owned())
55+
} else {
56+
Ok(current_dir.join(config_path))
57+
}
58+
}
4359

44-
if config_path.exists() {
45-
return Some(config_path);
46-
}
60+
fn default_config_path(manifest_path: &Path) -> Option<PathBuf> {
61+
let mut p = manifest_path.parent();
4762

48-
config_path.pop();
49-
config_path.push(".deny.toml");
63+
while let Some(parent) = p {
64+
let mut config_path = parent.join("deny.toml");
5065

51-
if config_path.exists() {
52-
return Some(config_path);
53-
}
66+
if config_path.exists() {
67+
return Some(config_path);
68+
}
5469

55-
config_path.pop();
56-
config_path.push(".cargo/deny.toml");
57-
if config_path.exists() {
58-
return Some(config_path);
59-
}
70+
config_path.pop();
71+
config_path.push(".deny.toml");
72+
73+
if config_path.exists() {
74+
return Some(config_path);
75+
}
6076

61-
p = parent.parent();
77+
config_path.pop();
78+
config_path.push(".cargo/deny.toml");
79+
80+
if config_path.exists() {
81+
return Some(config_path);
6282
}
6383

64-
None
84+
p = parent.parent();
6585
}
86+
87+
None
6688
}
6789

6890
pub fn get_local_exceptions_path(&self) -> Option<PathBuf> {
@@ -545,3 +567,49 @@ impl<'a> DiagPrinter<'a> {
545567
self.which.lock(self.max_severity)
546568
}
547569
}
570+
571+
#[cfg(test)]
572+
mod tests {
573+
use super::*;
574+
575+
#[inline]
576+
fn temp_dir() -> tempfile::TempDir {
577+
tempfile::tempdir().unwrap()
578+
}
579+
580+
#[test]
581+
fn test_resolve_config_path() {
582+
let temp_dir = temp_dir();
583+
584+
let current_dir = PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
585+
std::fs::File::create(current_dir.join("deny.toml")).unwrap();
586+
587+
let expected = current_dir.join("deny.toml");
588+
589+
let abs_path = current_dir.join("deny.toml");
590+
let actual = KrateContext::resolve_config_path(&current_dir, &abs_path).unwrap();
591+
assert_eq!(actual, abs_path);
592+
593+
let rel_path = Path::new("./deny.toml");
594+
let actual = KrateContext::resolve_config_path(&current_dir, rel_path).unwrap();
595+
assert_eq!(actual, expected);
596+
}
597+
598+
#[test]
599+
fn test_default_config_path() {
600+
let temp_dir = temp_dir();
601+
602+
let current_dir = PathBuf::from_path_buf(temp_dir.path().to_path_buf()).unwrap();
603+
std::fs::File::create(current_dir.join("Cargo.toml")).unwrap();
604+
let manifest_path = current_dir.join("Cargo.toml");
605+
606+
let actual = KrateContext::default_config_path(&manifest_path);
607+
let expected = None;
608+
assert_eq!(actual, expected);
609+
610+
std::fs::File::create(current_dir.join("deny.toml")).unwrap();
611+
let actual = KrateContext::default_config_path(&manifest_path);
612+
let expected = Some(current_dir.join("deny.toml"));
613+
assert_eq!(actual, expected);
614+
}
615+
}

src/cargo-deny/fetch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn cmd(
2626
args: Args,
2727
krate_ctx: crate::common::KrateContext,
2828
) -> Result<(), Error> {
29-
let cfg_path = krate_ctx.get_config_path(args.config.clone());
29+
let cfg_path = krate_ctx.get_config_path(args.config.as_deref())?;
3030

3131
let mut files = Files::new();
3232
let ValidConfig { advisories, .. } = ValidConfig::load(

src/cargo-deny/init.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ pub struct Args {
1212
const CONTENTS: &[u8] = include_bytes!("../../deny.template.toml");
1313

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

2021
// make sure the file does not exist yet
2122
ensure!(

src/cargo-deny/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn cmd(
5454
use licenses::LicenseInfo;
5555
use std::{collections::BTreeMap, fmt::Write};
5656

57-
let cfg_path = krate_ctx.get_config_path(args.config.clone());
57+
let cfg_path = krate_ctx.get_config_path(args.config.as_deref())?;
5858

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

0 commit comments

Comments
 (0)