Skip to content

Commit

Permalink
Support an exclude list in the config file (#315)
Browse files Browse the repository at this point in the history
* Support an exclude list in the config file

* Run cargo fmt

* Update socket2 to fixed version

Co-authored-by: Jake Shadle <jake.shadle@embark-studios.com>
  • Loading branch information
luser and Jake-Shadle committed Dec 9, 2020
1 parent 52b6135 commit 22359a3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 22 additions & 17 deletions src/cargo-deny/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ struct Config {
sources: Option<sources::Config>,
#[serde(default)]
targets: Vec<crate::common::Target>,
#[serde(default)]
exclude: Vec<String>,
}

struct ValidConfig {
Expand All @@ -77,6 +79,7 @@ struct ValidConfig {
licenses: licenses::ValidConfig,
sources: sources::ValidConfig,
targets: Vec<(krates::Target, Vec<String>)>,
exclude: Vec<String>,
}

impl ValidConfig {
Expand Down Expand Up @@ -127,6 +130,7 @@ impl ValidConfig {
let sources = cfg.sources.unwrap_or_default().validate(id, &mut diags);

let targets = crate::common::load_targets(cfg.targets, &mut diags, id);
let exclude = cfg.exclude;

(
diags,
Expand All @@ -136,6 +140,7 @@ impl ValidConfig {
licenses,
sources,
targets,
exclude,
},
)
};
Expand Down Expand Up @@ -177,7 +182,14 @@ pub(crate) fn cmd(
krate_ctx: crate::common::KrateContext,
) -> Result<AllStats, Error> {
let mut files = Files::new();
let mut cfg = ValidConfig::load(
let ValidConfig {
advisories,
bans,
licenses,
sources,
targets,
exclude,
} = ValidConfig::load(
krate_ctx.get_config_path(args.config.clone()),
&mut files,
log_ctx,
Expand Down Expand Up @@ -212,11 +224,9 @@ pub(crate) fn cmd(
let mut advisory_lockfile = None;
let mut krate_spans = None;

let targets = std::mem::replace(&mut cfg.targets, Vec::new());

rayon::scope(|s| {
s.spawn(|_| {
let gathered = krate_ctx.gather_krates(targets);
let gathered = krate_ctx.gather_krates(targets, exclude);

if let Ok(ref krates) = gathered {
rayon::scope(|s| {
Expand All @@ -238,8 +248,8 @@ pub(crate) fn cmd(
if check_advisories {
s.spawn(|_| {
advisory_dbs = Some(advisories::DbSet::load(
cfg.advisories.db_path.clone(),
cfg.advisories
advisories.db_path.clone(),
advisories
.db_urls
.iter()
.map(|us| us.as_ref().clone())
Expand Down Expand Up @@ -290,9 +300,9 @@ pub(crate) fn cmd(
let store = license_store.unwrap()?;
let gatherer = licenses::Gatherer::default()
.with_store(std::sync::Arc::new(store))
.with_confidence_threshold(cfg.licenses.confidence_threshold);
.with_confidence_threshold(licenses.confidence_threshold);

Some(gatherer.gather(&krates, &mut files, Some(&cfg.licenses)))
Some(gatherer.gather(&krates, &mut files, Some(&licenses)))
} else {
None
};
Expand Down Expand Up @@ -347,10 +357,9 @@ pub(crate) fn cmd(

if let Some(summary) = license_summary {
let lic_tx = tx.clone();
let lic_cfg = cfg.licenses;

let ctx = CheckCtx {
cfg: lic_cfg,
cfg: licenses,
krates: &krates,
krate_spans: &krate_spans,
serialize_extra,
Expand Down Expand Up @@ -398,10 +407,9 @@ pub(crate) fn cmd(
});

let ban_tx = tx.clone();
let ban_cfg = cfg.bans;

let ctx = CheckCtx {
cfg: ban_cfg,
cfg: bans,
krates: &krates,
krate_spans: &krate_spans,
serialize_extra,
Expand All @@ -419,10 +427,9 @@ pub(crate) fn cmd(

if check_sources {
let sources_tx = tx.clone();
let sources_cfg = cfg.sources;

let ctx = CheckCtx {
cfg: sources_cfg,
cfg: sources,
krates: &krates,
krate_spans: &krate_spans,
serialize_extra,
Expand All @@ -439,10 +446,8 @@ pub(crate) fn cmd(
}

if let Some((db, lockfile)) = advisory_ctx {
let adv_cfg = cfg.advisories;

let ctx = CheckCtx {
cfg: adv_cfg,
cfg: advisories,
krates: &krates,
krate_spans: &krate_spans,
serialize_extra,
Expand Down
4 changes: 3 additions & 1 deletion src/cargo-deny/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl KrateContext {
pub fn gather_krates(
self,
cfg_targets: Vec<(krates::Target, Vec<String>)>,
cfg_excludes: Vec<String>,
) -> Result<cargo_deny::Krates, anyhow::Error> {
log::info!("gathering crates for {}", self.manifest_path.display());
let start = std::time::Instant::now();
Expand All @@ -116,10 +117,11 @@ impl KrateContext {
gb.ignore_kind(DepKind::Dev, krates::Scope::NonWorkspace);
gb.workspace(self.workspace);

if !self.exclude.is_empty() {
if !self.exclude.is_empty() || !cfg_excludes.is_empty() {
gb.exclude(
self.exclude
.into_iter()
.chain(cfg_excludes)
.filter_map(|spec| match spec.parse() {
Ok(spec) => Some(spec),
Err(e) => {
Expand Down
8 changes: 7 additions & 1 deletion src/cargo-deny/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ struct Config {
advisories: Option<advisories::cfg::Config>,
#[serde(default)]
targets: Vec<crate::common::Target>,
#[serde(default)]
exclude: Vec<String>,
}

struct ValidConfig {
advisories: advisories::cfg::ValidConfig,
targets: Vec<(krates::Target, Vec<String>)>,
exclude: Vec<String>,
}

impl ValidConfig {
Expand Down Expand Up @@ -92,6 +95,7 @@ impl ValidConfig {
let mut diags = Vec::new();
let advisories = cfg.advisories.unwrap_or_default().validate(id, &mut diags);
let targets = crate::common::load_targets(cfg.targets, &mut diags, id);
let exclude = cfg.exclude;

let has_errors = diags
.iter()
Expand All @@ -107,6 +111,7 @@ impl ValidConfig {
Ok(Self {
advisories,
targets,
exclude,
})
}
}
Expand All @@ -123,6 +128,7 @@ pub fn cmd(
let ValidConfig {
advisories,
targets,
exclude,
} = ValidConfig::load(cfg_path, &mut files, log_ctx)?;

// Parallel all the things
Expand Down Expand Up @@ -154,7 +160,7 @@ pub fn cmd(
});

s.spawn(|_| {
krates = Some(ctx.gather_krates(targets));
krates = Some(ctx.gather_krates(targets, exclude));
if let Ok(krates) = krates.as_ref().unwrap() {
lockfile = Some(advisories::load_lockfile(krates.lock_path()));
}
Expand Down
10 changes: 8 additions & 2 deletions src/cargo-deny/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ pub struct Args {
struct Config {
#[serde(default)]
targets: Vec<crate::common::Target>,
#[serde(default)]
exclude: Vec<String>,
}

struct ValidConfig {
targets: Vec<(krates::Target, Vec<String>)>,
exclude: Vec<String>,
}

impl ValidConfig {
Expand All @@ -92,13 +95,15 @@ impl ValidConfig {

return Ok(Self {
targets: Vec::new(),
exclude: Vec::new(),
});
}
None => {
log::warn!("unable to find a config path, falling back to default config");

return Ok(Self {
targets: Vec::new(),
exclude: Vec::new(),
});
}
};
Expand All @@ -116,8 +121,9 @@ impl ValidConfig {
let validate = || -> Result<(Vec<Diagnostic>, Self), Vec<Diagnostic>> {
let mut diagnostics = Vec::new();
let targets = crate::common::load_targets(cfg.targets, &mut diagnostics, id);
let exclude = cfg.exclude;

Ok((diagnostics, Self { targets }))
Ok((diagnostics, Self { targets, exclude }))
};

let print = |diags: Vec<Diagnostic>| {
Expand Down Expand Up @@ -163,7 +169,7 @@ pub fn cmd(
let cfg = ValidConfig::load(krate_ctx.get_config_path(args.config), &mut files, log_ctx)?;

let (krates, store) = rayon::join(
|| krate_ctx.gather_krates(cfg.targets),
|| krate_ctx.gather_krates(cfg.targets, cfg.exclude),
crate::common::load_license_store,
);

Expand Down

0 comments on commit 22359a3

Please sign in to comment.