cargo-deny 0.16.1

Cargo plugin to help you manage large dependency graphs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use crate::{
    cfg::ValidationContext,
    diag::{self, ErrorSink, FileId, Files, KrateSpans, PackChannel},
    CheckCtx, PathBuf,
};

#[derive(Default, Clone)]
pub struct KrateGather<'k> {
    pub name: &'k str,
    pub features: &'k [&'k str],
    pub targets: &'k [&'k str],
    pub all_features: bool,
    pub no_default_features: bool,
}

impl<'k> KrateGather<'k> {
    pub fn new(name: &'k str) -> Self {
        Self {
            name,
            ..Default::default()
        }
    }

    pub fn gather(self) -> crate::Krates {
        let mut project_dir = crate::PathBuf::from("./tests/test_data");
        project_dir.push(self.name);

        let mut cmd = krates::Cmd::new();
        cmd.current_dir(project_dir);

        if self.all_features {
            cmd.all_features();
        }

        if self.no_default_features {
            cmd.no_default_features();
        }

        if !self.features.is_empty() {
            cmd.features(self.features.iter().map(|f| (*f).to_owned()));
        }

        let mut kb = krates::Builder::new();

        if !self.targets.is_empty() {
            kb.include_targets(self.targets.iter().map(|t| (t, vec![])));
        }

        kb.build(cmd, krates::NoneFilter)
            .expect("failed to build crate graph")
    }
}

pub struct Config<C> {
    pub deserialized: C,
    pub config: String,
}

impl<C> Default for Config<C>
where
    C: Default,
{
    fn default() -> Self {
        Self {
            deserialized: C::default(),
            config: "".to_owned(),
        }
    }
}

impl<C> Config<C>
where
    C: toml_span::DeserializeOwned,
{
    pub fn new(config: impl Into<String>) -> Self {
        let config = config.into();
        let mut val = toml_span::parse(&config).expect("failed to parse test config");
        let deserialized = C::deserialize(&mut val).expect("failed to deserialize test config");
        Self {
            deserialized,
            config,
        }
    }
}

impl<'de, C> From<&'de str> for Config<C>
where
    C: toml_span::DeserializeOwned,
{
    fn from(s: &'de str) -> Self {
        Self::new(s)
    }
}

impl<C> From<String> for Config<C>
where
    C: toml_span::DeserializeOwned + Default,
{
    fn from(s: String) -> Self {
        Self::new(s)
    }
}

pub struct ConfigData<T> {
    pub config: T,
    pub files: Files,
    pub id: FileId,
}

impl<T> ConfigData<T> {
    pub fn file(&self) -> &str {
        self.files.source(self.id)
    }
}

impl<T> ConfigData<T>
where
    T: toml_span::DeserializeOwned,
{
    pub fn load_str(name: impl Into<crate::PathBuf>, contents: impl Into<String>) -> Self {
        let contents: String = contents.into();

        let res = {
            let mut cval = toml_span::parse(&contents).expect("failed to parse toml");
            T::deserialize(&mut cval)
        };

        let mut files = Files::new();
        let id = files.add(name, contents);

        let config = match res {
            Ok(v) => v,
            Err(derr) => {
                let diag_str = write_diagnostics(
                    &files,
                    derr.errors.into_iter().map(|err| err.to_diagnostic(id)),
                );
                panic!("failed to deserialize:\n---\n{diag_str}\n---");
            }
        };

        ConfigData { config, files, id }
    }

    pub fn load(path: impl Into<PathBuf>) -> Self {
        let path = path.into();
        let contents = std::fs::read_to_string(&path).unwrap();

        Self::load_str(path, contents)
    }
}

impl<T> ConfigData<T> {
    pub fn validate<IV, V>(mut self, conv: impl FnOnce(T) -> IV) -> V
    where
        IV: super::UnvalidatedConfig<ValidCfg = V>,
    {
        let uvc = conv(self.config);

        let mut diagnostics = Vec::new();
        let vcfg = uvc.validate(ValidationContext {
            cfg_id: self.id,
            files: &mut self.files,
            diagnostics: &mut diagnostics,
        });

        if diagnostics.is_empty() {
            vcfg
        } else {
            let diag_str = write_diagnostics(&self.files, diagnostics.into_iter());

            panic!("failed to validate config:\n---\n{diag_str}\n---");
        }
    }

    pub fn validate_with_diags<IV, V>(
        mut self,
        conv: impl FnOnce(T) -> IV,
        on_diags: impl FnOnce(&Files, Vec<crate::diag::Diagnostic>),
    ) -> V
    where
        IV: super::UnvalidatedConfig<ValidCfg = V>,
    {
        let uvc = conv(self.config);

        let mut diagnostics = Vec::new();
        let vcfg = uvc.validate(ValidationContext {
            cfg_id: self.id,
            files: &mut self.files,
            diagnostics: &mut diagnostics,
        });

        on_diags(&self.files, diagnostics);
        vcfg
    }
}

pub(crate) fn write_diagnostics(
    files: &Files,
    errors: impl Iterator<Item = crate::diag::Diagnostic>,
) -> String {
    let mut s = codespan_reporting::term::termcolor::NoColor::new(Vec::new());
    let config = crate::diag::codespan_config();

    for diag in errors {
        codespan_reporting::term::emit(&mut s, &config, files, &diag).unwrap();
    }

    String::from_utf8(s.into_inner()).unwrap()
}

#[inline]
pub fn gather_diagnostics<C, VC, R>(
    krates: &crate::Krates,
    test_name: &str,
    cfg: Config<C>,
    runner: R,
) -> Vec<serde_json::Value>
where
    C: crate::UnvalidatedConfig<ValidCfg = VC>,
    VC: Send,
    R: FnOnce(CheckCtx<'_, VC>, PackChannel) + Send,
{
    let ctx = setup(krates, test_name, cfg);
    run_gather(ctx, runner)
}

pub struct GatherCtx<'k, VC> {
    pub krates: &'k crate::Krates,
    pub files: crate::diag::Files,
    pub valid_cfg: VC,
    spans: crate::diag::KrateSpans<'k>,
}

pub fn setup<'k, C, VC>(
    krates: &'k crate::Krates,
    test_name: &str,
    cfg: Config<C>,
) -> GatherCtx<'k, VC>
where
    C: crate::UnvalidatedConfig<ValidCfg = VC>,
    VC: Send,
{
    let mut files = crate::diag::Files::new();
    let spans = KrateSpans::synthesize(krates, test_name, &mut files);

    let config = cfg.deserialized;
    let cfg_id = files.add(format!("{test_name}.toml"), cfg.config);

    let mut cfg_diags = Vec::new();
    let valid_cfg = config.validate(crate::cfg::ValidationContext {
        cfg_id,
        files: &mut files,
        diagnostics: &mut cfg_diags,
    });

    if cfg_diags
        .iter()
        .any(|d| d.severity >= crate::diag::Severity::Error)
    {
        panic!("encountered errors validating config: {cfg_diags:#?}");
    }

    GatherCtx {
        krates,
        files,
        valid_cfg,
        spans,
    }
}

pub fn run_gather<VC, R>(ctx: GatherCtx<'_, VC>, runner: R) -> Vec<serde_json::Value>
where
    VC: Send,
    R: FnOnce(CheckCtx<'_, VC>, PackChannel) + Send,
{
    let (tx, rx) = crossbeam::channel::unbounded();

    let grapher = diag::InclusionGrapher::new(ctx.krates);

    let (_, gathered) = rayon::join(
        || {
            let cctx = crate::CheckCtx {
                krates: ctx.krates,
                krate_spans: &ctx.spans,
                cfg: ctx.valid_cfg,
                serialize_extra: true,
                colorize: false,
                log_level: log::LevelFilter::Info,
                files: &ctx.files,
            };
            runner(cctx, tx);
        },
        || {
            let mut diagnostics = Vec::new();

            let default = if std::env::var_os("CI").is_some() {
                60
            } else {
                30
            };

            let timeout = std::env::var("CARGO_DENY_TEST_TIMEOUT_SECS")
                .ok()
                .and_then(|ts| ts.parse().ok())
                .unwrap_or(default);
            let timeout = std::time::Duration::from_secs(timeout);

            let trx = crossbeam::channel::after(timeout);
            loop {
                crossbeam::select! {
                    recv(rx) -> msg => {
                        if let Ok(pack) = msg {
                            diagnostics.extend(pack);
                        } else {
                            // Yay, the sender was dopped (i.e. check was finished)
                            break;
                        }
                    }
                    recv(trx) -> _ => {
                        anyhow::bail!("Timed out after {timeout:?}");
                    }
                }
            }

            Ok(diagnostics)
        },
    );

    gathered
        .unwrap()
        .into_iter()
        .map(|d| diag::diag_to_json(d, &ctx.files, Some(&grapher)))
        .collect()
}

#[macro_export]
macro_rules! field_eq {
    ($obj:expr, $field:expr, $expected:expr) => {
        $obj.pointer($field) == Some(&serde_json::json!($expected))
    };
}

#[macro_export]
macro_rules! assert_field_eq {
    ($obj:expr, $field:expr, $expected:expr) => {
        assert_eq!($obj.pointer($field), Some(&serde_json::json!($expected)));
    };
}

#[macro_export]
macro_rules! func_name {
    () => {{
        fn f() {}
        fn type_name_of<T>(_: T) -> &'static str {
            std::any::type_name::<T>()
        }
        let name = type_name_of(f);
        &name[..name.len() - 3]
    }};
}

#[macro_export]
macro_rules! overrides {
    ($($code:expr => $severity:ident),* $(,)?) => {
        {
            let mut map = std::collections::BTreeMap::new();

            $(map.insert($code, $crate::diag::Severity::$severity);)*

            $crate::diag::DiagnosticOverrides {
                code_overrides: map,
                level_overrides: Vec::new(),
            }
        }
    }
}

#[inline]
pub fn gather_bans(
    name: &str,
    kg: KrateGather<'_>,
    cfg: impl Into<Config<crate::bans::cfg::Config>>,
) -> Vec<serde_json::Value> {
    let krates = kg.gather();
    let cfg = cfg.into();

    gather_diagnostics::<crate::bans::cfg::Config, _, _>(&krates, name, cfg, |ctx, tx| {
        crate::bans::check(ctx, None, tx);
    })
}

#[inline]
pub fn gather_bans_with_overrides(
    name: &str,
    kg: KrateGather<'_>,
    cfg: impl Into<Config<crate::bans::cfg::Config>>,
    overrides: diag::DiagnosticOverrides,
) -> Vec<serde_json::Value> {
    let krates = kg.gather();
    let cfg = cfg.into();

    gather_diagnostics::<crate::bans::cfg::Config, _, _>(&krates, name, cfg, |ctx, tx| {
        crate::bans::check(
            ctx,
            None,
            ErrorSink {
                overrides: Some(std::sync::Arc::new(overrides)),
                channel: tx,
            },
        );
    })
}