sc-lint 0.8.2

Top-level CLI composition root for the sc-lint tool family.
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
//! The one step scheduler shared by the built-in lint plan and the consumer
//! lint and test profiles (REQ-CLI-023 as amended, ADR-020 decision 10).
//!
//! Every `Lane::Parallel` step gets its own worker (no cap), every
//! `Lane::Cargo` step runs on one serialized lane worker in declaration order,
//! and `after_all` steps run last, one at a time, in declaration order. A
//! failure never stops other steps under [`FailurePolicy::KeepGoing`]; under
//! [`FailurePolicy::FailFast`] only not-yet-started cargo-lane steps and
//! `after_all` steps are skipped. Reports keep declaration order.

use std::any::Any;
use std::panic::AssertUnwindSafe;
use std::panic::catch_unwind;
use std::path::Path;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering;
use std::thread;
use std::thread::ScopedJoinHandle;

use serde::Serialize;
use serde_json::Value;
use serde_json::json;

use crate::CliError;
use crate::consts;
use crate::error::ErrorCode;
use crate::profile_plan::PlannedStep;
use crate::workflow::Lane;
use crate::workflow::StepPlan;
use crate::workflow::StepReport;
use crate::workflow::SystemAdapter;

/// Stable codes for consumer profile execution (ADR-020 decision 10).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ProfileCode {
    InvalidAfterAll,
    UnknownSelector,
    GroupCycle,
    StepsFailed,
}

impl ErrorCode for ProfileCode {
    fn as_str(&self) -> &'static str {
        match self {
            Self::InvalidAfterAll => "profile.invalid_after_all",
            Self::UnknownSelector => "test.unknown_selector",
            Self::GroupCycle => "test.group_cycle",
            Self::StepsFailed => "profile.steps_failed",
        }
    }
}

impl ProfileCode {
    /// The ADR-020 decision 10 recovery text for this code.
    pub(crate) const fn recovery(self) -> &'static str {
        match self {
            Self::InvalidAfterAll => "remove after_all from that selector",
            Self::UnknownSelector => "use a name from sc-lint test --list",
            Self::GroupCycle => "break the cycle in [tool.sc-lint.test]",
            Self::StepsFailed => "fix the failing steps and rerun",
        }
    }

    /// A `config` error carrying this code; `cause` names the selector or step
    /// and the file. Never used for [`ProfileCode::StepsFailed`].
    pub(crate) fn config_error(self, cause: String, config_path: &Path) -> CliError {
        CliError::config(cause.clone())
            .with_code(self.as_str())
            .with_cause(cause)
            .with_suggested_action(self.recovery())
            .with_detail("config_path", json!(config_path.display().to_string()))
    }
}

/// What the scheduler does after the first failed step.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FailurePolicy {
    /// Every step runs; the run fails after all of them finish.
    KeepGoing,
    /// Stop starting cargo-lane and `after_all` steps; running steps finish.
    FailFast,
}

/// One unit of work. `index` is the zero-based position in the caller's
/// selected list (used only in reports); `planned` is set for consumer lint
/// steps so the adapter's `run_planned_step` seam (which records
/// platform/target on the report) stays in use.
pub(crate) struct ScheduledStep<'a> {
    index: usize,
    plan: StepPlan,
    planned: Option<&'a PlannedStep>,
}

impl<'a> ScheduledStep<'a> {
    pub(crate) fn new(index: usize, plan: StepPlan) -> Self {
        Self {
            index,
            plan,
            planned: None,
        }
    }

    pub(crate) fn with_planned(mut self, planned: &'a PlannedStep) -> Self {
        self.planned = Some(planned);
        self
    }

    fn name(&self) -> &str {
        self.plan.name()
    }
}

/// The result of one scheduled step.
pub(crate) enum StepOutcome {
    Done(StepReport),
    Failed(CliError),
    Skipped,
}

/// One `error.details.failed_steps[]` entry.
#[derive(Debug, Serialize)]
struct FailedStep {
    index: usize,
    name: String,
    exit_code: Option<i32>,
    message: String,
}

/// One `error.details.skipped_steps[]` entry.
#[derive(Debug, Serialize)]
struct SkippedStep {
    index: usize,
    name: String,
    reason: &'static str,
}

/// Runs `steps` and returns their reports in declaration order, or one
/// `profile.steps_failed` error naming every failed (and fail-fast skipped)
/// step.
#[expect(
    clippy::result_large_err,
    reason = "Step execution stays within the top-level CliError contract."
)]
pub(crate) fn run_steps_scheduled(
    repo_root: &Path,
    adapter: &dyn SystemAdapter,
    steps: &[ScheduledStep<'_>],
    policy: FailurePolicy,
) -> Result<Vec<StepReport>, CliError> {
    let failed = AtomicBool::new(false);
    let mut outcomes = run_main_steps(repo_root, adapter, steps, policy, &failed);
    for (index, step) in steps.iter().enumerate() {
        if step.plan.after_all() {
            outcomes[index] = Some(run_unless_skipped(
                repo_root, adapter, step, policy, &failed,
            ));
        }
    }
    let outcomes = outcomes
        .into_iter()
        .map(|outcome| outcome.expect("the scheduler records an outcome for every step"))
        .collect();
    aggregate_outcomes(steps, outcomes)
}

/// The steps one worker runs and the outcomes it returns.
type Worker<'scope> = (
    Vec<usize>,
    ScopedJoinHandle<'scope, Vec<(usize, StepOutcome)>>,
);

/// Runs every non-`after_all` step: Parallel steps on their own workers, Cargo
/// steps on one lane worker. Returns once every worker has joined (the
/// barrier before the `after_all` tail); `after_all` slots stay `None`.
fn run_main_steps(
    repo_root: &Path,
    adapter: &dyn SystemAdapter,
    steps: &[ScheduledStep<'_>],
    policy: FailurePolicy,
    failed: &AtomicBool,
) -> Vec<Option<StepOutcome>> {
    let mut outcomes: Vec<Option<StepOutcome>> = steps.iter().map(|_| None).collect();
    thread::scope(|scope| {
        let mut workers = Vec::new();
        for index in main_indices(steps, Lane::Parallel) {
            let step = &steps[index];
            let spawned = thread::Builder::new()
                .name(format!("sc-lint-{}", step.name()))
                .spawn_scoped(scope, move || {
                    vec![(index, run_one(repo_root, adapter, step, failed))]
                });
            let started = started_worker(vec![index], spawned, steps, &mut outcomes, failed);
            workers.extend(started);
        }
        let lane = main_indices(steps, Lane::Cargo);
        let lane_steps = lane.clone();
        let spawned = thread::Builder::new()
            .name("sc-lint-cargo-lane".to_string())
            .spawn_scoped(scope, move || {
                lane_steps
                    .into_iter()
                    .map(|index| {
                        let outcome =
                            run_unless_skipped(repo_root, adapter, &steps[index], policy, failed);
                        (index, outcome)
                    })
                    .collect::<Vec<_>>()
            });
        workers.extend(started_worker(lane, spawned, steps, &mut outcomes, failed));
        join_workers(workers, steps, &mut outcomes, failed);
    });
    outcomes
}

/// The declaration-order indices of the non-`after_all` steps on `lane`.
fn main_indices(steps: &[ScheduledStep<'_>], lane: Lane) -> Vec<usize> {
    steps
        .iter()
        .enumerate()
        .filter(|(_, step)| !step.plan.after_all() && step.plan.lane() == lane)
        .map(|(index, _)| index)
        .collect()
}

/// The worker when it started; otherwise every step it would have run is
/// recorded as failed and `None` is returned.
fn started_worker<'scope>(
    indices: Vec<usize>,
    spawned: std::io::Result<ScopedJoinHandle<'scope, Vec<(usize, StepOutcome)>>>,
    steps: &[ScheduledStep<'_>],
    outcomes: &mut [Option<StepOutcome>],
    failed: &AtomicBool,
) -> Option<Worker<'scope>> {
    match spawned {
        Ok(worker) => Some((indices, worker)),
        Err(error) => {
            let cause = error.to_string();
            fail_all(&indices, steps, outcomes, failed, |name| {
                CliError::internal(format!("{name} worker failed to start")).with_source(&cause)
            });
            None
        }
    }
}

/// Joins every worker. `run_one` catches step panics, so a join error means
/// the worker itself failed; every step it owned is a failure.
fn join_workers(
    workers: Vec<Worker<'_>>,
    steps: &[ScheduledStep<'_>],
    outcomes: &mut [Option<StepOutcome>],
    failed: &AtomicBool,
) {
    for (indices, worker) in workers {
        match worker.join() {
            Ok(results) => {
                for (index, outcome) in results {
                    outcomes[index] = Some(outcome);
                }
            }
            Err(_) => fail_all(&indices, steps, outcomes, failed, |name| {
                CliError::internal(format!("{name} worker panicked"))
            }),
        }
    }
}

fn fail_all(
    indices: &[usize],
    steps: &[ScheduledStep<'_>],
    outcomes: &mut [Option<StepOutcome>],
    failed: &AtomicBool,
    error: impl Fn(&str) -> CliError,
) {
    for &index in indices {
        failed.store(true, Ordering::SeqCst);
        outcomes[index] = Some(StepOutcome::Failed(error(steps[index].name())));
    }
}

/// Under fail-fast a step that has not started after a failure is skipped.
fn run_unless_skipped(
    repo_root: &Path,
    adapter: &dyn SystemAdapter,
    step: &ScheduledStep<'_>,
    policy: FailurePolicy,
    failed: &AtomicBool,
) -> StepOutcome {
    if policy == FailurePolicy::FailFast && failed.load(Ordering::SeqCst) {
        return StepOutcome::Skipped;
    }
    run_one(repo_root, adapter, step, failed)
}

/// The only place a step executes. A panic becomes a failed outcome naming
/// the step; any failure sets `failed`.
#[expect(
    clippy::result_large_err,
    reason = "The adapter seam returns the shared top-level CliError contract by value."
)]
fn run_one(
    repo_root: &Path,
    adapter: &dyn SystemAdapter,
    step: &ScheduledStep<'_>,
    failed: &AtomicBool,
) -> StepOutcome {
    let result = catch_unwind(AssertUnwindSafe(|| match step.planned {
        Some(planned) => adapter.run_planned_step(repo_root, planned),
        None => adapter.run_step(repo_root, &step.plan),
    }));
    let outcome = match result {
        Ok(Ok(report)) => StepOutcome::Done(report),
        Ok(Err(error)) => StepOutcome::Failed(error),
        Err(payload) => StepOutcome::Failed(CliError::internal(format!(
            "{} panicked during execution: {}",
            step.name(),
            panic_message(payload.as_ref())
        ))),
    };
    if matches!(outcome, StepOutcome::Failed(_)) {
        failed.store(true, Ordering::SeqCst);
    }
    outcome
}

fn panic_message(payload: &(dyn Any + Send)) -> &str {
    payload
        .downcast_ref::<&str>()
        .copied()
        .or_else(|| payload.downcast_ref::<String>().map(String::as_str))
        .unwrap_or("non-string panic payload")
}

/// Returns every report in declaration order, or the `profile.steps_failed`
/// error listing every failed step (and every fail-fast skipped step).
#[expect(
    clippy::result_large_err,
    reason = "Step execution stays within the top-level CliError contract."
)]
pub(crate) fn aggregate_outcomes(
    steps: &[ScheduledStep<'_>],
    outcomes: Vec<StepOutcome>,
) -> Result<Vec<StepReport>, CliError> {
    let mut reports = Vec::with_capacity(outcomes.len());
    let mut failed = Vec::new();
    let mut skipped = Vec::new();
    for (step, outcome) in steps.iter().zip(outcomes) {
        match outcome {
            StepOutcome::Done(report) => reports.push(report),
            StepOutcome::Failed(error) => failed.push(FailedStep {
                index: step.index,
                name: step.name().to_string(),
                exit_code: step_exit_code(&error),
                message: failure_message(&error),
            }),
            StepOutcome::Skipped => skipped.push(SkippedStep {
                index: step.index,
                name: step.name().to_string(),
                reason: "fail-fast",
            }),
        }
    }
    if failed.is_empty() {
        debug_assert!(skipped.is_empty(), "a step is skipped only after a failure");
        return Ok(reports);
    }
    let listing = failed
        .iter()
        .map(|step| format!("[{}] {}", step.index, step.name))
        .collect::<Vec<_>>()
        .join(", ");
    let code = ProfileCode::StepsFailed;
    let mut error = CliError::backend_failure(format!(
        "{} of {} steps failed: {listing}",
        failed.len(),
        steps.len()
    ))
    .with_code(code.as_str())
    .with_suggested_action(code.recovery())
    .with_detail("failed_steps", json!(failed));
    if !skipped.is_empty() {
        error = error.with_detail("skipped_steps", json!(skipped));
    }
    Err(error)
}

/// The process exit code a step error recorded, `None` when the process did
/// not start or the step panicked.
fn step_exit_code(error: &CliError) -> Option<i32> {
    error
        .details
        .get(consts::FIELD_EXIT_CODE)
        .and_then(Value::as_i64)
        .and_then(|code| i32::try_from(code).ok())
}

/// The step error message followed by its cause, so step output is kept.
fn failure_message(error: &CliError) -> String {
    match error.cause.as_deref() {
        Some(cause) => format!("{}: {cause}", error.message),
        None => error.message.clone(),
    }
}