cargo-hack 0.6.44

Cargo subcommand to provide various options useful for testing and continuous integration.
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![forbid(unsafe_code)]

#[macro_use]
mod term;

#[macro_use]
mod process;

mod cargo;
mod cli;
mod context;
mod features;
mod fs;
mod manifest;
mod metadata;
mod restore;
mod rustup;
mod version;

use std::{
    collections::{BTreeMap, HashSet},
    env,
    ffi::OsString,
    fmt::{self, Write as _},
    process::ExitCode,
    str::FromStr,
};

use anyhow::{Error, Result, bail, format_err};

use crate::{
    context::Context,
    features::Feature,
    metadata::PackageId,
    process::ProcessBuilder,
    rustup::Rustup,
    version::{MaybeVersion, Version, VersionRange},
};

fn main() -> ExitCode {
    term::init_coloring();
    if let Err(e) = try_main() {
        error!("{e:#}");
    }
    if term::error() || term::warn() && env::var_os("CARGO_HACK_DENY_WARNINGS").is_some() {
        ExitCode::FAILURE
    } else {
        ExitCode::SUCCESS
    }
}

fn try_main() -> Result<()> {
    let cargo = env::var_os("CARGO_HACK_CARGO_SRC")
        .unwrap_or_else(|| env::var_os("CARGO").unwrap_or_else(|| OsString::from("cargo")));
    let Some(args) = cli::Args::parse(&cargo)? else { return Ok(()) };
    let cx = &Context::new(args, cargo)?;

    manifest::with(cx, || {
        if cx.subcommand.is_none() {
            return Ok(());
        }

        let packages = determine_package_list(cx)?;
        let mut progress = Progress::default();
        let mut keep_going = KeepGoing::default();
        if let Some(range) = cx.version_range {
            let mut versions = BTreeMap::new();
            let steps = rustup::version_range(range, cx.version_step, &packages, cx)?;
            for pkg in packages {
                let msrv = cx
                    .rust_version(pkg.id)
                    .map(str::parse::<Version>)
                    .transpose()?
                    .map(Version::strip_patch);
                if range == VersionRange::msrv() {
                    let msrv = msrv.ok_or_else(|| {
                        format_err!(
                            "no rust-version field in {}'s Cargo.toml is specified",
                            cx.packages(pkg.id).name
                        )
                    })?;
                    versions.entry(msrv).or_insert_with(Vec::new).push(pkg);
                } else {
                    let mut seen = false;
                    for cargo_version in &steps {
                        if msrv.is_some() && Some(*cargo_version) < msrv {
                            continue;
                        }
                        if !seen {
                            if range.start_inclusive == MaybeVersion::Msrv
                                && Some(*cargo_version) != msrv
                            {
                                if let Some(msrv) = msrv {
                                    versions.entry(msrv).or_insert_with(Vec::new).push(pkg.clone());
                                }
                            }
                            seen = true;
                        }
                        versions.entry(*cargo_version).or_insert_with(Vec::new).push(pkg.clone());
                    }
                    if !seen {
                        let package = cx.packages(pkg.id);
                        let name = &package.name;
                        let msrv = msrv.expect("always `seen` if no msrv");
                        warn!(
                            "skipping {name}, rust-version ({msrv}) is not in specified range ({range})"
                        );
                    }
                }
            }
            let versions = versions; // make immutable
            if versions.is_empty() {
                // TODO: emit warning
                return Ok(());
            }

            for (cargo_version, packages) in &versions {
                for package in packages {
                    if cx.target.is_empty() || cargo_version.minor >= 64 {
                        progress.total += package.feature_count;
                    } else {
                        progress.total += package.feature_count * cx.target.len();
                    }
                }
            }

            // First, generate the lockfile using the oldest cargo specified.
            // https://github.com/taiki-e/cargo-hack/issues/105
            // For now, only generate lockfile if min version is pre-1.60.
            // (If future cargo introduces compatibility issues, the number of
            // versions requiring generate-lockfile will probably increase.)
            // https://github.com/taiki-e/cargo-hack/issues/234#issuecomment-2028517197
            let mut generate_lockfile =
                !cx.locked && versions.first_key_value().unwrap().0.minor < 60;
            // Workaround for spurious "failed to select a version" error.
            // (This does not work around the underlying cargo bug: https://github.com/rust-lang/cargo/issues/10623)
            let mut regenerate_lockfile_on_51_or_up = false;
            for (cargo_version, packages) in versions {
                versioned_cargo_exec_on_packages(
                    cx,
                    &packages,
                    cargo_version.minor,
                    &mut progress,
                    &mut keep_going,
                    &mut generate_lockfile,
                    &mut regenerate_lockfile_on_51_or_up,
                )?;
            }
        } else {
            let total = packages.iter().map(|p| p.feature_count).sum();
            progress.total = total;
            default_cargo_exec_on_packages(cx, &packages, &mut progress, &mut keep_going)?;
        }
        if keep_going.count > 0 {
            eprintln!();
            error!("{keep_going}");
        }
        Ok(())
    })
}

#[derive(Default)]
struct Progress {
    total: usize,
    count: usize,
}

impl Progress {
    fn in_partition(&self, partition: &Partition) -> bool {
        let current_index = self.count / self.total.div_ceil(partition.count);
        current_index == partition.index
    }
}

#[derive(Clone)]
enum Kind<'a> {
    Normal,
    Each { features: Vec<&'a Feature> },
    Powerset { features: Vec<Vec<&'a Feature>> },
}

fn determine_kind(cx: &Context, id: PackageId, multiple_packages: bool) -> Option<PackageRuns<'_>> {
    assert!(cx.subcommand.is_some());
    if cx.ignore_private && cx.is_private(id) {
        info!("skipped running on private package `{}`", cx.name_verbose(id));
        return None;
    }
    if !cx.each_feature && !cx.feature_powerset {
        let feature_count = 1;
        let kind = Kind::Normal;
        return Some(PackageRuns { id, kind, feature_count });
    }

    let package = cx.packages(id);
    let pkg_features = cx.pkg_features(id);
    let filter = |&f: &&Feature| {
        !cx.exclude_features.iter().any(|s| f == s)
            && !cx.group_features.iter().any(|g| g.matches(f.name()))
    };
    let features = if cx.include_features.is_empty() {
        // TODO
        if !multiple_packages {
            for name in &cx.exclude_features {
                if !pkg_features.contains(name) {
                    warn!("specified feature `{name}` not found in package `{}`", package.name);
                }
            }
        }

        let mut features: Vec<_> = pkg_features.normal().iter().filter(filter).collect();

        if let Some(opt_deps) = &cx.optional_deps {
            if opt_deps.len() == 1 && opt_deps[0].is_empty() {
                // --optional-deps=
            } else if !multiple_packages {
                // TODO
                for d in opt_deps {
                    if !pkg_features.optional_deps().iter().any(|f| f == d) {
                        warn!(
                            "specified optional dependency `{d}` not found in package `{}`",
                            package.name
                        );
                    }
                }
            }

            features.extend(pkg_features.optional_deps().iter().filter(|f| {
                filter(f) && (opt_deps.is_empty() || opt_deps.iter().any(|x| *f == x))
            }));
        }

        if cx.include_deps_features {
            features.extend(pkg_features.deps_features().iter().filter(filter));
        }

        if !cx.group_features.is_empty() {
            if cx.ignore_unknown_features {
                let all_valid_features: HashSet<_> = pkg_features
                    .normal()
                    .iter()
                    .chain(pkg_features.optional_deps())
                    .flat_map(Feature::as_group)
                    .map(String::as_str)
                    .collect();
                features.extend(cx.group_features.iter().filter(|&f| {
                    let all_valid =
                        f.as_group().iter().all(|f| all_valid_features.contains(f.as_str()));
                    if !all_valid {
                        info!(
                            "skipped applying group `{}` to {}",
                            f.as_group().join(","),
                            package.name
                        );
                    }
                    all_valid
                }));
            } else {
                features.extend(cx.group_features.iter());
            }
        }

        features
    } else {
        cx.include_features.iter().filter(filter).collect()
    };

    if cx.each_feature {
        if (pkg_features.normal().is_empty() && pkg_features.optional_deps().is_empty()
            || !cx.include_features.is_empty())
            && features.is_empty()
        {
            let feature_count = 1;
            let kind = Kind::Normal;
            Some(PackageRuns { id, kind, feature_count })
        } else {
            // See exec_on_package
            let feature_count = features.len()
                + (!cx.exclude_no_default_features) as usize
                + (!(cx.exclude_all_features
                    || pkg_features.optional_deps().is_empty() && pkg_features.normal().len() <= 1))
                    as usize;
            let kind = Kind::Each { features };
            Some(PackageRuns { id, kind, feature_count })
        }
    } else if cx.feature_powerset {
        let features = features::feature_powerset(
            features,
            cx.depth,
            &cx.at_least_one_of,
            &cx.mutually_exclusive_features,
            &package.features,
        );

        if (pkg_features.normal().is_empty() && pkg_features.optional_deps().is_empty()
            || !cx.include_features.is_empty())
            && features.is_empty()
        {
            let feature_count = 1;
            let kind = Kind::Normal;
            Some(PackageRuns { id, kind, feature_count })
        } else {
            // See exec_on_package
            let feature_count = features.len()
                + (!cx.exclude_no_default_features) as usize
                + (!(cx.exclude_all_features
                    || (pkg_features.optional_deps().is_empty()
                        || match &cx.optional_deps {
                            // Skip when all optional deps are already included in powerset
                            Some(opt_deps) => opt_deps.is_empty(),
                            None => false,
                        }))) as usize;
            let kind = Kind::Powerset { features };
            Some(PackageRuns { id, kind, feature_count })
        }
    } else {
        unreachable!()
    }
}

#[derive(Clone)]
struct PackageRuns<'a> {
    id: PackageId,
    kind: Kind<'a>,
    feature_count: usize,
}

fn determine_package_list(cx: &Context) -> Result<Vec<PackageRuns<'_>>> {
    for spec in &cx.exclude {
        if !cx.workspace_members().any(|&id| *cx.packages(id).name == **spec) {
            warn!(
                "excluded package(s) `{spec}` not found in workspace `{}`",
                cx.workspace_root().display()
            );
        }
    }
    Ok(if cx.workspace {
        let ids: Vec<_> = cx
            .workspace_members()
            .filter(|&&id| !cx.exclude.iter().any(|e| *cx.packages(id).name == **e))
            .collect();
        let multiple_packages = ids.len() > 1;
        ids.iter().filter_map(|&&id| determine_kind(cx, id, multiple_packages)).collect()
    } else if !cx.package.is_empty() {
        if let Some(spec) = cx
            .package
            .iter()
            .find(|&spec| !cx.workspace_members().any(|&id| *cx.packages(id).name == **spec))
        {
            bail!("package ID specification `{spec}` matched no packages")
        }

        let ids: Vec<_> = cx
            .workspace_members()
            .filter(|&&id| cx.package.iter().any(|p| *cx.packages(id).name == **p))
            .filter(|&&id| !cx.exclude.iter().any(|e| *cx.packages(id).name == **e))
            .collect();
        let multiple_packages = ids.len() > 1;
        ids.iter().filter_map(|&&id| determine_kind(cx, id, multiple_packages)).collect()
    } else if cx.current_package().is_none() {
        let ids: Vec<_> = cx
            .workspace_members()
            .filter(|&&id| !cx.exclude.iter().any(|e| *cx.packages(id).name == **e))
            .collect();
        let multiple_packages = ids.len() > 1;
        ids.iter().filter_map(|&&id| determine_kind(cx, id, multiple_packages)).collect()
    } else {
        let current_package = &cx.packages(cx.current_package().unwrap()).name;
        let multiple_packages = false;
        cx.workspace_members()
            .find(|&&id| cx.packages(id).name == *current_package)
            .filter(|&&id| !cx.exclude.iter().any(|e| *cx.packages(id).name == **e))
            .and_then(|&id| determine_kind(cx, id, multiple_packages).map(|p| vec![p]))
            .unwrap_or_default()
    })
}

fn versioned_cargo_exec_on_packages(
    cx: &Context,
    packages: &[PackageRuns<'_>],
    cargo_version: u32,
    progress: &mut Progress,
    keep_going: &mut KeepGoing,
    generate_lockfile: &mut bool,
    regenerate_lockfile_on_51_or_up: &mut bool,
) -> Result<()> {
    // Do not use `cargo +<toolchain>` due to a rustup bug: https://github.com/rust-lang/rustup/issues/3036
    let mut line = cmd!("rustup");
    line.leading_arg("run");

    let toolchain = format!("1.{cargo_version}");
    let print_output = true;
    rustup::install_toolchain(&toolchain, &cx.target, print_output, cx.log_group)?;
    if *generate_lockfile || *regenerate_lockfile_on_51_or_up && cargo_version >= 51 {
        let mut line = line.clone();
        line.leading_arg(&toolchain);
        line.leading_arg("cargo");
        line.arg("generate-lockfile");
        if let Some(pid) = cx.current_package() {
            let package = cx.packages(pid);
            if !cx.no_manifest_path {
                line.arg("--manifest-path");
                line.arg(
                    package
                        .manifest_path
                        .strip_prefix(&cx.current_dir)
                        .unwrap_or(&package.manifest_path),
                );
            }
        }
        line.run_with_output()?;
        *generate_lockfile = false;
        *regenerate_lockfile_on_51_or_up = false;
    }
    if !cx.locked && cargo_version < 51 {
        *regenerate_lockfile_on_51_or_up = true;
    }

    if cx.clean_per_version {
        cargo_clean(cx, None)?;
    }

    let mut line = line.clone();
    line.leading_arg(&toolchain);
    line.leading_arg("cargo");
    line.apply_context(cx);
    exec_on_packages(cx, packages, line, progress, keep_going, cargo_version)
}

fn default_cargo_exec_on_packages(
    cx: &Context,
    packages: &[PackageRuns<'_>],
    progress: &mut Progress,
    keep_going: &mut KeepGoing,
) -> Result<()> {
    let mut line = cx.cargo();
    line.apply_context(cx);
    exec_on_packages(cx, packages, line, progress, keep_going, cx.cargo_version)
}

fn exec_on_packages(
    cx: &Context,
    packages: &[PackageRuns<'_>],
    mut line: ProcessBuilder<'_>,
    progress: &mut Progress,
    keep_going: &mut KeepGoing,
    cargo_version: u32,
) -> Result<()> {
    if cx.locked {
        line.arg("--locked");
    }
    if cx.target.is_empty() || cargo_version >= 64 {
        // TODO: We should test that cargo's multi-target build does not break the resolver behavior required for a correct check.
        for target in &cx.target {
            line.arg("--target");
            line.arg(target);
        }
        packages
            .iter()
            .try_for_each(|pkg| exec_on_package(cx, pkg.id, &pkg.kind, &line, progress, keep_going))
    } else {
        cx.target.iter().try_for_each(|target| {
            let mut line = line.clone();
            line.arg("--target");
            line.arg(target);
            packages.iter().try_for_each(|pkg| {
                exec_on_package(cx, pkg.id, &pkg.kind, &line, progress, keep_going)
            })
        })
    }
}

fn exec_on_package(
    cx: &Context,
    id: PackageId,
    kind: &Kind<'_>,
    line: &ProcessBuilder<'_>,
    progress: &mut Progress,
    keep_going: &mut KeepGoing,
) -> Result<()> {
    let package = cx.packages(id);

    let mut line = line.clone();
    line.append_features_from_args(cx, id);

    if !cx.no_manifest_path {
        line.arg("--manifest-path");
        line.arg(
            package.manifest_path.strip_prefix(&cx.current_dir).unwrap_or(&package.manifest_path),
        );
    }

    match kind {
        Kind::Normal => {
            // only run with default features
            return exec_cargo(cx, id, &line, progress, keep_going);
        }
        Kind::Each { .. } | Kind::Powerset { .. } => {}
    }

    // Run with --all-features first: https://github.com/taiki-e/cargo-hack/issues/246
    // https://github.com/taiki-e/cargo-hack/issues/42
    // https://github.com/rust-lang/cargo/pull/8799
    // > --all-features will now enable features for inactive optional dependencies.
    let pkg_features = cx.pkg_features(id);
    let exclude_all_features = cx.exclude_all_features
        || match kind {
            Kind::Each { .. } => {
                pkg_features.optional_deps().is_empty() && pkg_features.normal().len() <= 1
            }
            Kind::Powerset { .. } => {
                pkg_features.optional_deps().is_empty()
                    || match &cx.optional_deps {
                        // Skip when all optional deps are already included in powerset
                        Some(opt_deps) => opt_deps.is_empty() && cx.depth.is_none(),
                        None => false,
                    }
            }
            Kind::Normal => unreachable!(),
        };
    if !exclude_all_features {
        let mut line = line.clone();
        // run with all features
        // https://github.com/taiki-e/cargo-hack/issues/42
        line.arg("--all-features");
        exec_cargo(cx, id, &line, progress, keep_going)?;
    }

    if !cx.no_default_features {
        line.arg("--no-default-features");
    }

    // if `metadata.packages[].features` has `default` feature, users can
    // specify `--features=default`, so it should be one of the combinations.
    // Otherwise, "run with default features" is basically the same as
    // "run with no default features".

    if !cx.exclude_no_default_features {
        // run with no default features if the package has other features
        exec_cargo(cx, id, &line, progress, keep_going)?;
    }

    match kind {
        Kind::Each { features } => {
            for &f in features {
                exec_cargo_with_features(cx, id, &line, progress, keep_going, &[f])?;
            }
        }
        Kind::Powerset { features } => {
            let features =
                if exclude_all_features && features.len() > 1 && cx.depth.unwrap_or(usize::MAX) > 1
                {
                    // If --all-features case is skipped, run with the biggest feature combination early (first or second): https://github.com/taiki-e/cargo-hack/issues/246
                    // TODO: The last combination is usually the biggest feature combination, but
                    //       in some cases this is not the case due to deduplication of the powerset.
                    //       See todo comment in powerset_deduplication test for example.
                    let last = features.last().unwrap();
                    exec_cargo_with_features(cx, id, &line, progress, keep_going, last)?;
                    &features[..features.len() - 1]
                } else {
                    features
                };
            for f in features {
                exec_cargo_with_features(cx, id, &line, progress, keep_going, f)?;
            }
        }
        Kind::Normal => unreachable!(),
    }

    Ok(())
}

fn exec_cargo_with_features(
    cx: &Context,
    id: PackageId,
    line: &ProcessBuilder<'_>,
    progress: &mut Progress,
    keep_going: &mut KeepGoing,
    features: &[&Feature],
) -> Result<()> {
    let mut line = line.clone();
    line.append_features(features);
    exec_cargo(cx, id, &line, progress, keep_going)
}

#[derive(Default)]
struct KeepGoing {
    count: u64,
    failed_commands: BTreeMap<String, Vec<String>>,
}

impl fmt::Display for KeepGoing {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "failed to run {} commands\n", self.count)?;
        writeln!(f, "failed commands:")?;
        for (pkg, commands) in &self.failed_commands {
            writeln!(f, "    {pkg}:")?;
            for cmd in commands {
                writeln!(f, "        {cmd}")?;
            }
        }
        Ok(())
    }
}

#[derive(Clone, Copy, PartialEq)]
enum LogGroup {
    None,
    GithubActions,
}

impl LogGroup {
    fn auto() -> Self {
        if env::var_os("GITHUB_ACTIONS").is_some() { Self::GithubActions } else { Self::None }
    }

    fn print(self, msg: &str) -> Option<LogGroupGuard> {
        match self {
            Self::GithubActions => {
                println!("::group::{msg}");
                Some(LogGroupGuard)
            }
            Self::None => {
                info!("{msg}");
                None
            }
        }
    }
}

struct LogGroupGuard;
impl Drop for LogGroupGuard {
    fn drop(&mut self) {
        println!("::endgroup::");
    }
}

impl FromStr for LogGroup {
    type Err = Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s {
            "none" => Ok(Self::None),
            "github-actions" => Ok(Self::GithubActions),
            other => bail!(
                "argument for --log-group must be none or github-actions, but found `{other}`"
            ),
        }
    }
}

pub(crate) struct Partition {
    index: usize,
    count: usize,
}

impl FromStr for Partition {
    type Err = Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        match s.split('/').map(str::parse::<usize>).collect::<Vec<_>>()[..] {
            [Ok(m), Ok(n)] if 0 < m && m <= n => Ok(Self { index: m - 1, count: n }),
            _ => bail!("bad or out-of-range partition: {s}"),
        }
    }
}

fn exec_cargo(
    cx: &Context,
    id: PackageId,
    line: &ProcessBuilder<'_>,
    progress: &mut Progress,
    keep_going: &mut KeepGoing,
) -> Result<()> {
    let res = exec_cargo_inner(cx, id, line, progress);
    if cx.keep_going {
        if let Err(e) = res {
            error!("{e:#}");
            keep_going.count = keep_going.count.saturating_add(1);
            let name = &*cx.packages(id).name;
            if !keep_going.failed_commands.contains_key(name) {
                keep_going.failed_commands.insert(name.to_owned(), vec![]);
            }
            keep_going.failed_commands.get_mut(name).unwrap().push(format!("{line:#}"));
        }
        Ok(())
    } else {
        res
    }
}

fn exec_cargo_inner(
    cx: &Context,
    id: PackageId,
    line: &ProcessBuilder<'_>,
    progress: &mut Progress,
) -> Result<()> {
    if progress.count != 0 && !cx.print_command_list && cx.log_group == LogGroup::None {
        eprintln!();
    }

    if let Some(partition) = &cx.partition {
        if !progress.in_partition(partition) {
            let _guard = log_and_update_progress(cx, id, line, progress, "skipping");
            return Ok(());
        }
    }

    if cx.clean_per_run {
        cargo_clean(cx, Some(id))?;
    }

    if cx.print_command_list {
        print_command(line.clone());
        return Ok(());
    }

    let _guard = log_and_update_progress(cx, id, line, progress, "running");

    line.run()
}

fn cargo_clean(cx: &Context, id: Option<PackageId>) -> Result<()> {
    let mut line = cx.cargo();
    line.arg("clean");
    if cx.locked {
        line.arg("--locked");
    }
    if let Some(id) = id {
        line.arg("--package");
        line.arg(&*cx.packages(id).name);
    }

    if cx.print_command_list {
        print_command(line);
        return Ok(());
    }

    if term::verbose() {
        // running `cargo clean [--package <package>]`
        info!("running {line}");
    }

    line.run()
}

fn print_command(mut line: ProcessBuilder<'_>) {
    let _guard = term::verbose::scoped(true);
    line.strip_program_path = true;
    let l = line.to_string();
    println!("{}", &l[1..l.len() - 1]);
}

fn log_and_update_progress(
    cx: &Context,
    id: PackageId,
    line: &ProcessBuilder<'_>,
    progress: &mut Progress,
    action: &str,
) -> Option<LogGroupGuard> {
    // running/skipping `<command>` (on <package>) (<count>/<total>)
    let mut msg = String::new();
    if term::verbose() {
        write!(msg, "{action} {line}").unwrap();
    } else {
        write!(msg, "{action} {line} on {}", cx.packages(id).name).unwrap();
    }
    progress.count += 1;
    write!(msg, " ({}/{})", progress.count, progress.total).unwrap();
    cx.log_group.print(&msg)
}