nextest_runner/
errors.rs

1// Copyright (c) The nextest Contributors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Errors produced by nextest.
5
6use crate::{
7    cargo_config::{TargetTriple, TargetTripleSource},
8    config::{
9        core::{ConfigExperimental, ToolName},
10        elements::{CustomTestGroup, TestGroup},
11        scripts::{ProfileScriptType, ScriptId, ScriptType},
12    },
13    helpers::{display_exited_with, dylib_path_envvar, plural},
14    indenter::{DisplayIndented, indented},
15    record::{
16        PortableRecordingFormatVersion, PortableRecordingVersionIncompatibility, RecordedRunInfo,
17        RunIdIndex, RunsJsonFormatVersion, StoreFormatVersion, StoreVersionIncompatibility,
18    },
19    redact::{Redactor, SizeDisplay},
20    reuse_build::{ArchiveFormat, ArchiveStep},
21    target_runner::PlatformRunnerSource,
22};
23use bytesize::ByteSize;
24use camino::{FromPathBufError, Utf8Path, Utf8PathBuf};
25use config::ConfigError;
26use eazip::CompressionMethod;
27use etcetera::HomeDirError;
28use itertools::{Either, Itertools};
29use nextest_filtering::errors::FiltersetParseErrors;
30use nextest_metadata::RustBinaryId;
31use quick_junit::ReportUuid;
32use serde::{Deserialize, Serialize};
33use smol_str::SmolStr;
34use std::{
35    borrow::Cow,
36    collections::BTreeSet,
37    env::JoinPathsError,
38    fmt::{self, Write as _},
39    path::PathBuf,
40    process::ExitStatus,
41    sync::Arc,
42};
43use target_spec_miette::IntoMietteDiagnostic;
44use thiserror::Error;
45
46/// An error that occurred while parsing the config.
47#[derive(Debug, Error)]
48#[error(
49    "failed to parse nextest config at `{config_file}`{}",
50    provided_by_tool(tool.as_ref())
51)]
52#[non_exhaustive]
53pub struct ConfigParseError {
54    config_file: Utf8PathBuf,
55    tool: Option<ToolName>,
56    #[source]
57    kind: ConfigParseErrorKind,
58}
59
60impl ConfigParseError {
61    pub(crate) fn new(
62        config_file: impl Into<Utf8PathBuf>,
63        tool: Option<&ToolName>,
64        kind: ConfigParseErrorKind,
65    ) -> Self {
66        Self {
67            config_file: config_file.into(),
68            tool: tool.cloned(),
69            kind,
70        }
71    }
72
73    /// Returns the config file for this error.
74    pub fn config_file(&self) -> &Utf8Path {
75        &self.config_file
76    }
77
78    /// Returns the tool name associated with this error.
79    pub fn tool(&self) -> Option<&ToolName> {
80        self.tool.as_ref()
81    }
82
83    /// Returns the kind of error this is.
84    pub fn kind(&self) -> &ConfigParseErrorKind {
85        &self.kind
86    }
87}
88
89/// Returns the string ` provided by tool <tool>`, if `tool` is `Some`.
90pub fn provided_by_tool(tool: Option<&ToolName>) -> String {
91    match tool {
92        Some(tool) => format!(" provided by tool `{tool}`"),
93        None => String::new(),
94    }
95}
96
97/// The kind of error that occurred while parsing a config.
98///
99/// Returned by [`ConfigParseError::kind`].
100#[derive(Debug, Error)]
101#[non_exhaustive]
102pub enum ConfigParseErrorKind {
103    /// An error occurred while building the config.
104    #[error(transparent)]
105    BuildError(Box<ConfigError>),
106    /// An error occurred while parsing the config into a table.
107    #[error(transparent)]
108    TomlParseError(Box<toml::de::Error>),
109    #[error(transparent)]
110    /// An error occurred while deserializing the config.
111    DeserializeError(Box<serde_path_to_error::Error<ConfigError>>),
112    /// An error occurred while reading the config file (version only).
113    #[error(transparent)]
114    VersionOnlyReadError(std::io::Error),
115    /// An error occurred while deserializing the config (version only).
116    #[error(transparent)]
117    VersionOnlyDeserializeError(Box<serde_path_to_error::Error<toml::de::Error>>),
118    /// Errors occurred while compiling configuration strings.
119    #[error("error parsing compiled data (destructure this variant for more details)")]
120    CompileErrors(Vec<ConfigCompileError>),
121    /// An invalid set of test groups was defined by the user.
122    #[error("invalid test groups defined: {}\n(test groups cannot start with '@tool:' unless specified by a tool)", .0.iter().join(", "))]
123    InvalidTestGroupsDefined(BTreeSet<CustomTestGroup>),
124    /// An invalid set of test groups was defined by a tool config file.
125    #[error(
126        "invalid test groups defined by tool: {}\n(test groups must start with '@tool:<tool-name>:')", .0.iter().join(", "))]
127    InvalidTestGroupsDefinedByTool(BTreeSet<CustomTestGroup>),
128    /// Some test groups were unknown.
129    #[error("unknown test groups specified by config (destructure this variant for more details)")]
130    UnknownTestGroups {
131        /// The list of errors that occurred.
132        errors: Vec<UnknownTestGroupError>,
133
134        /// Known groups up to this point.
135        known_groups: BTreeSet<TestGroup>,
136    },
137    /// Both `[script.*]` and `[scripts.*]` were defined.
138    #[error(
139        "both `[script.*]` and `[scripts.*]` defined\n\
140         (hint: [script.*] will be removed in the future: switch to [scripts.setup.*])"
141    )]
142    BothScriptAndScriptsDefined,
143    /// An invalid set of config scripts was defined by the user.
144    #[error("invalid config scripts defined: {}\n(config scripts cannot start with '@tool:' unless specified by a tool)", .0.iter().join(", "))]
145    InvalidConfigScriptsDefined(BTreeSet<ScriptId>),
146    /// An invalid set of config scripts was defined by a tool config file.
147    #[error(
148        "invalid config scripts defined by tool: {}\n(config scripts must start with '@tool:<tool-name>:')", .0.iter().join(", "))]
149    InvalidConfigScriptsDefinedByTool(BTreeSet<ScriptId>),
150    /// The same config script name was used across config script types.
151    #[error(
152        "config script names used more than once: {}\n\
153         (config script names must be unique across all script types)", .0.iter().join(", ")
154    )]
155    DuplicateConfigScriptNames(BTreeSet<ScriptId>),
156    /// Errors occurred while parsing `[[profile.<profile-name>.scripts]]`.
157    #[error(
158        "errors in profile-specific config scripts (destructure this variant for more details)"
159    )]
160    ProfileScriptErrors {
161        /// The errors that occurred.
162        errors: Box<ProfileScriptErrors>,
163
164        /// Known scripts up to this point.
165        known_scripts: BTreeSet<ScriptId>,
166    },
167    /// An unknown experimental feature or features were defined.
168    #[error("unknown experimental features defined (destructure this variant for more details)")]
169    UnknownExperimentalFeatures {
170        /// The set of unknown features.
171        unknown: BTreeSet<String>,
172
173        /// The set of known features.
174        known: BTreeSet<ConfigExperimental>,
175    },
176    /// A tool specified an experimental feature.
177    ///
178    /// Tools are not allowed to specify experimental features.
179    #[error(
180        "tool config file specifies experimental features `{}` \
181         -- only repository config files can do so",
182        .features.iter().join(", "),
183    )]
184    ExperimentalFeaturesInToolConfig {
185        /// The name of the experimental feature.
186        features: BTreeSet<String>,
187    },
188    /// Experimental features were used but not enabled.
189    #[error("experimental features used but not enabled: {}", .missing_features.iter().join(", "))]
190    ExperimentalFeaturesNotEnabled {
191        /// The features that were not enabled.
192        missing_features: BTreeSet<ConfigExperimental>,
193    },
194    /// An inheritance cycle was detected in the profile configuration.
195    #[error("inheritance error(s) detected: {}", .0.iter().join(", "))]
196    InheritanceErrors(Vec<InheritsError>),
197}
198
199/// An error that occurred while compiling overrides or scripts specified in
200/// configuration.
201#[derive(Debug)]
202#[non_exhaustive]
203pub struct ConfigCompileError {
204    /// The name of the profile under which the data was found.
205    pub profile_name: String,
206
207    /// The section within the profile where the error occurred.
208    pub section: ConfigCompileSection,
209
210    /// The kind of error that occurred.
211    pub kind: ConfigCompileErrorKind,
212}
213
214/// For a [`ConfigCompileError`], the section within the profile where the error
215/// occurred.
216#[derive(Debug)]
217pub enum ConfigCompileSection {
218    /// `profile.<profile-name>.default-filter`.
219    DefaultFilter,
220
221    /// `[[profile.<profile-name>.overrides]]` at the corresponding index.
222    Override(usize),
223
224    /// `[[profile.<profile-name>.scripts]]` at the corresponding index.
225    Script(usize),
226}
227
228/// The kind of error that occurred while parsing config overrides.
229#[derive(Debug)]
230#[non_exhaustive]
231pub enum ConfigCompileErrorKind {
232    /// Neither `platform` nor `filter` were specified.
233    ConstraintsNotSpecified {
234        /// Whether `default-filter` was specified.
235        ///
236        /// If default-filter is specified, then specifying `filter` is not
237        /// allowed -- so we show a different message in that case.
238        default_filter_specified: bool,
239    },
240
241    /// Both `filter` and `default-filter` were specified.
242    ///
243    /// It only makes sense to specify one of the two.
244    FilterAndDefaultFilterSpecified,
245
246    /// One or more errors occured while parsing expressions.
247    Parse {
248        /// A potential error that occurred while parsing the host platform expression.
249        host_parse_error: Option<target_spec::Error>,
250
251        /// A potential error that occurred while parsing the target platform expression.
252        target_parse_error: Option<target_spec::Error>,
253
254        /// Filterset or default filter parse errors.
255        filter_parse_errors: Vec<FiltersetParseErrors>,
256    },
257}
258
259impl ConfigCompileErrorKind {
260    /// Returns [`miette::Report`]s for each error recorded by self.
261    pub fn reports(&self) -> impl Iterator<Item = miette::Report> + '_ {
262        match self {
263            Self::ConstraintsNotSpecified {
264                default_filter_specified,
265            } => {
266                let message = if *default_filter_specified {
267                    "for override with `default-filter`, `platform` must also be specified"
268                } else {
269                    "at least one of `platform` and `filter` must be specified"
270                };
271                Either::Left(std::iter::once(miette::Report::msg(message)))
272            }
273            Self::FilterAndDefaultFilterSpecified => {
274                Either::Left(std::iter::once(miette::Report::msg(
275                    "at most one of `filter` and `default-filter` must be specified",
276                )))
277            }
278            Self::Parse {
279                host_parse_error,
280                target_parse_error,
281                filter_parse_errors,
282            } => {
283                let host_parse_report = host_parse_error
284                    .as_ref()
285                    .map(|error| miette::Report::new_boxed(error.clone().into_diagnostic()));
286                let target_parse_report = target_parse_error
287                    .as_ref()
288                    .map(|error| miette::Report::new_boxed(error.clone().into_diagnostic()));
289                let filter_parse_reports =
290                    filter_parse_errors.iter().flat_map(|filter_parse_errors| {
291                        filter_parse_errors.errors.iter().map(|single_error| {
292                            miette::Report::new(single_error.clone())
293                                .with_source_code(filter_parse_errors.input.to_owned())
294                        })
295                    });
296
297                Either::Right(
298                    host_parse_report
299                        .into_iter()
300                        .chain(target_parse_report)
301                        .chain(filter_parse_reports),
302                )
303            }
304        }
305    }
306}
307
308/// A test priority specified was out of range.
309#[derive(Clone, Debug, Error)]
310#[error("test priority ({priority}) out of range: must be between -100 and 100, both inclusive")]
311pub struct TestPriorityOutOfRange {
312    /// The priority that was out of range.
313    pub priority: i8,
314}
315
316/// An execution error occurred while attempting to start a test.
317#[derive(Clone, Debug, Error)]
318pub enum ChildStartError {
319    /// An error occurred while creating a temporary path for a setup script.
320    #[error("error creating temporary path for setup script")]
321    TempPath(#[source] Arc<std::io::Error>),
322
323    /// An error occurred while spawning the child process.
324    #[error("error spawning child process")]
325    Spawn(#[source] Arc<std::io::Error>),
326}
327
328/// An error that occurred while reading the output of a setup script.
329#[derive(Clone, Debug, Error)]
330pub enum SetupScriptOutputError {
331    /// An error occurred while opening the setup script environment file.
332    #[error("error opening environment file `{path}`")]
333    EnvFileOpen {
334        /// The path to the environment file.
335        path: Utf8PathBuf,
336
337        /// The underlying error.
338        #[source]
339        error: Arc<std::io::Error>,
340    },
341
342    /// An error occurred while reading the setup script environment file.
343    #[error("error reading environment file `{path}`")]
344    EnvFileRead {
345        /// The path to the environment file.
346        path: Utf8PathBuf,
347
348        /// The underlying error.
349        #[source]
350        error: Arc<std::io::Error>,
351    },
352
353    /// An error occurred while parsing the setup script environment file.
354    #[error("line `{line}` in environment file `{path}` not in KEY=VALUE format")]
355    EnvFileParse {
356        /// The path to the environment file.
357        path: Utf8PathBuf,
358        /// The line at issue.
359        line: String,
360    },
361
362    /// An environment variable key was reserved.
363    #[error("key `{key}` begins with `NEXTEST`, which is reserved for internal use")]
364    EnvFileReservedKey {
365        /// The environment variable name.
366        key: String,
367    },
368}
369
370/// A list of errors that implements `Error`.
371///
372/// In the future, we'll likely want to replace this with a `miette::Diagnostic`-based error, since
373/// that supports multiple causes via "related".
374#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
375pub struct ErrorList<T> {
376    // A description of what the errors are.
377    description: Cow<'static, str>,
378    // Invariant: this list is non-empty.
379    inner: Vec<T>,
380}
381
382impl<T: std::error::Error> ErrorList<T> {
383    pub(crate) fn new<U>(description: &'static str, errors: Vec<U>) -> Option<Self>
384    where
385        T: From<U>,
386    {
387        if errors.is_empty() {
388            None
389        } else {
390            Some(Self {
391                description: Cow::Borrowed(description),
392                inner: errors.into_iter().map(T::from).collect(),
393            })
394        }
395    }
396
397    /// Returns a short summary of the error list.
398    pub(crate) fn short_message(&self) -> String {
399        let string = self.to_string();
400        match string.lines().next() {
401            // Remove a trailing colon if it exists for a better UX.
402            Some(first_line) => first_line.trim_end_matches(':').to_string(),
403            None => String::new(),
404        }
405    }
406
407    /// Returns the description of what the errors are.
408    pub fn description(&self) -> &str {
409        &self.description
410    }
411
412    /// Iterates over the errors in this list.
413    pub fn iter(&self) -> impl Iterator<Item = &T> {
414        self.inner.iter()
415    }
416
417    /// Transforms the errors in this list using the given function.
418    pub fn map<U, F>(self, f: F) -> ErrorList<U>
419    where
420        U: std::error::Error,
421        F: FnMut(T) -> U,
422    {
423        ErrorList {
424            description: self.description,
425            inner: self.inner.into_iter().map(f).collect(),
426        }
427    }
428}
429
430impl<T: std::error::Error> IntoIterator for ErrorList<T> {
431    type Item = T;
432    type IntoIter = std::vec::IntoIter<T>;
433
434    fn into_iter(self) -> Self::IntoIter {
435        self.inner.into_iter()
436    }
437}
438
439impl<T: std::error::Error> fmt::Display for ErrorList<T> {
440    fn fmt(&self, mut f: &mut fmt::Formatter) -> fmt::Result {
441        // If a single error occurred, pretend that this is just that.
442        if self.inner.len() == 1 {
443            return write!(f, "{}", self.inner[0]);
444        }
445
446        // Otherwise, list all errors.
447        writeln!(
448            f,
449            "{} errors occurred {}:",
450            self.inner.len(),
451            self.description,
452        )?;
453        for error in &self.inner {
454            let mut indent = indented(f).with_str("  ").skip_initial();
455            writeln!(indent, "* {}", DisplayErrorChain::new(error))?;
456            f = indent.into_inner();
457        }
458        Ok(())
459    }
460}
461
462#[cfg(test)]
463impl<T: proptest::arbitrary::Arbitrary + std::fmt::Debug + 'static> proptest::arbitrary::Arbitrary
464    for ErrorList<T>
465{
466    type Parameters = ();
467    type Strategy = proptest::strategy::BoxedStrategy<Self>;
468
469    fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
470        use proptest::prelude::*;
471
472        // Generate 1-5 errors (non-empty).
473        proptest::collection::vec(any::<T>(), 1..=5)
474            .prop_map(|inner| ErrorList {
475                description: Cow::Borrowed("test errors"),
476                inner,
477            })
478            .boxed()
479    }
480}
481
482impl<T: std::error::Error> std::error::Error for ErrorList<T> {
483    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
484        if self.inner.len() == 1 {
485            self.inner[0].source()
486        } else {
487            // More than one error occurred, so we can't return a single error here. Instead, we
488            // return `None` and display the chain of causes in `fmt::Display`.
489            None
490        }
491    }
492}
493
494/// A wrapper type to display a chain of errors with internal indentation.
495///
496/// This is similar to the display-error-chain crate, but uses an indenter
497/// internally to ensure that subsequent lines are also nested.
498pub struct DisplayErrorChain<E> {
499    error: E,
500    initial_indent: &'static str,
501}
502
503impl<E: std::error::Error> DisplayErrorChain<E> {
504    /// Creates a new `DisplayErrorChain` with the given error.
505    pub fn new(error: E) -> Self {
506        Self {
507            error,
508            initial_indent: "",
509        }
510    }
511
512    /// Creates a new `DisplayErrorChain` with the given error and initial indentation.
513    pub fn new_with_initial_indent(initial_indent: &'static str, error: E) -> Self {
514        Self {
515            error,
516            initial_indent,
517        }
518    }
519}
520
521impl<E> fmt::Display for DisplayErrorChain<E>
522where
523    E: std::error::Error,
524{
525    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
526        let mut writer = indented(f).with_str(self.initial_indent);
527        write!(writer, "{}", self.error)?;
528
529        let Some(mut cause) = self.error.source() else {
530            return Ok(());
531        };
532
533        write!(writer, "\n  caused by:")?;
534
535        loop {
536            writeln!(writer)?;
537            // Wrap the existing writer to accumulate indentation.
538            let mut indent = indented(&mut writer).with_str("    ").skip_initial();
539            write!(indent, "  - {cause}")?;
540
541            let Some(next_cause) = cause.source() else {
542                break Ok(());
543            };
544
545            cause = next_cause;
546        }
547    }
548}
549
550/// An error was returned while managing a child process or reading its output.
551#[derive(Clone, Debug, Error)]
552pub enum ChildError {
553    /// An error occurred while reading from a child file descriptor.
554    #[error(transparent)]
555    Fd(#[from] ChildFdError),
556
557    /// An error occurred while reading the output of a setup script.
558    #[error(transparent)]
559    SetupScriptOutput(#[from] SetupScriptOutputError),
560}
561
562/// An error was returned while reading from child a file descriptor.
563#[derive(Clone, Debug, Error)]
564pub enum ChildFdError {
565    /// An error occurred while reading standard output.
566    #[error("error reading standard output")]
567    ReadStdout(#[source] Arc<std::io::Error>),
568
569    /// An error occurred while reading standard error.
570    #[error("error reading standard error")]
571    ReadStderr(#[source] Arc<std::io::Error>),
572
573    /// An error occurred while reading a combined stream.
574    #[error("error reading combined stream")]
575    ReadCombined(#[source] Arc<std::io::Error>),
576
577    /// An error occurred while waiting for the child process to exit.
578    #[error("error waiting for child process to exit")]
579    Wait(#[source] Arc<std::io::Error>),
580}
581
582/// An unknown test group was specified in the config.
583#[derive(Clone, Debug, Eq, PartialEq)]
584#[non_exhaustive]
585pub struct UnknownTestGroupError {
586    /// The name of the profile under which the unknown test group was found.
587    pub profile_name: String,
588
589    /// The name of the unknown test group.
590    pub name: TestGroup,
591}
592
593/// While parsing profile-specific config scripts, an unknown script was
594/// encountered.
595#[derive(Clone, Debug, Eq, PartialEq)]
596pub struct ProfileUnknownScriptError {
597    /// The name of the profile under which the errors occurred.
598    pub profile_name: String,
599
600    /// The name of the unknown script.
601    pub name: ScriptId,
602}
603
604/// While parsing profile-specific config scripts, a script of the wrong type
605/// was encountered.
606#[derive(Clone, Debug, Eq, PartialEq)]
607pub struct ProfileWrongConfigScriptTypeError {
608    /// The name of the profile under which the errors occurred.
609    pub profile_name: String,
610
611    /// The name of the config script.
612    pub name: ScriptId,
613
614    /// The script type that the user attempted to use the script as.
615    pub attempted: ProfileScriptType,
616
617    /// The script type that the script actually is.
618    pub actual: ScriptType,
619}
620
621/// While parsing profile-specific config scripts, a list-time-enabled script
622/// used a filter that can only be used at test run time.
623#[derive(Clone, Debug, Eq, PartialEq)]
624pub struct ProfileListScriptUsesRunFiltersError {
625    /// The name of the profile under which the errors occurred.
626    pub profile_name: String,
627
628    /// The name of the config script.
629    pub name: ScriptId,
630
631    /// The script type.
632    pub script_type: ProfileScriptType,
633
634    /// The filters that were used.
635    pub filters: BTreeSet<String>,
636}
637
638/// Errors that occurred while parsing `[[profile.*.scripts]]`.
639#[derive(Clone, Debug, Default)]
640pub struct ProfileScriptErrors {
641    /// The list of unknown script errors.
642    pub unknown_scripts: Vec<ProfileUnknownScriptError>,
643
644    /// The list of wrong script type errors.
645    pub wrong_script_types: Vec<ProfileWrongConfigScriptTypeError>,
646
647    /// The list of list-time-enabled scripts that used a run-time filter.
648    pub list_scripts_using_run_filters: Vec<ProfileListScriptUsesRunFiltersError>,
649}
650
651impl ProfileScriptErrors {
652    /// Returns true if there are no errors recorded.
653    pub fn is_empty(&self) -> bool {
654        self.unknown_scripts.is_empty()
655            && self.wrong_script_types.is_empty()
656            && self.list_scripts_using_run_filters.is_empty()
657    }
658}
659
660/// An error which indicates that a profile was requested but not known to nextest.
661#[derive(Clone, Debug, Error)]
662#[error("profile `{profile}` not found (known profiles: {})", .all_profiles.join(", "))]
663pub struct ProfileNotFound {
664    profile: String,
665    all_profiles: Vec<String>,
666}
667
668impl ProfileNotFound {
669    pub(crate) fn new(
670        profile: impl Into<String>,
671        all_profiles: impl IntoIterator<Item = impl Into<String>>,
672    ) -> Self {
673        let mut all_profiles: Vec<_> = all_profiles.into_iter().map(|s| s.into()).collect();
674        all_profiles.sort_unstable();
675        Self {
676            profile: profile.into(),
677            all_profiles,
678        }
679    }
680}
681
682/// An identifier is invalid.
683#[derive(Clone, Debug, Error, Eq, PartialEq)]
684pub enum InvalidIdentifier {
685    /// The identifier is empty.
686    #[error("identifier is empty")]
687    Empty,
688
689    /// The identifier is not in the correct Unicode format.
690    #[error("invalid identifier `{0}`")]
691    InvalidXid(SmolStr),
692
693    /// This tool identifier doesn't match the expected pattern.
694    #[error("tool identifier not of the form \"@tool:tool-name:identifier\": `{0}`")]
695    ToolIdentifierInvalidFormat(SmolStr),
696
697    /// One of the components of this tool identifier is empty.
698    #[error("tool identifier has empty component: `{0}`")]
699    ToolComponentEmpty(SmolStr),
700
701    /// The tool identifier is not in the correct Unicode format.
702    #[error("invalid tool identifier `{0}`")]
703    ToolIdentifierInvalidXid(SmolStr),
704}
705
706/// A tool name is invalid.
707#[derive(Clone, Debug, Error, Eq, PartialEq)]
708pub enum InvalidToolName {
709    /// The tool name is empty.
710    #[error("tool name is empty")]
711    Empty,
712
713    /// The tool name is not in the correct Unicode format.
714    #[error("invalid tool name `{0}`")]
715    InvalidXid(SmolStr),
716
717    /// The tool name starts with "@tool", which is reserved for tool identifiers.
718    #[error("tool name cannot start with \"@tool\": `{0}`")]
719    StartsWithToolPrefix(SmolStr),
720}
721
722/// The name of a test group is invalid (not a valid identifier).
723#[derive(Clone, Debug, Error)]
724#[error("invalid custom test group name: {0}")]
725pub struct InvalidCustomTestGroupName(pub InvalidIdentifier);
726
727/// The name of a configuration script is invalid (not a valid identifier).
728#[derive(Clone, Debug, Error)]
729#[error("invalid configuration script name: {0}")]
730pub struct InvalidConfigScriptName(pub InvalidIdentifier);
731
732/// Error returned while parsing a [`ToolConfigFile`](crate::config::core::ToolConfigFile) value.
733#[derive(Clone, Debug, Error, PartialEq, Eq)]
734pub enum ToolConfigFileParseError {
735    #[error(
736        "tool-config-file has invalid format: {input}\n(hint: tool configs must be in the format <tool-name>:<path>)"
737    )]
738    /// The input was not in the format "tool:path".
739    InvalidFormat {
740        /// The input that failed to parse.
741        input: String,
742    },
743
744    /// The tool name was invalid.
745    #[error("tool-config-file has invalid tool name: {input}")]
746    InvalidToolName {
747        /// The input that failed to parse.
748        input: String,
749
750        /// The error that occurred.
751        #[source]
752        error: InvalidToolName,
753    },
754
755    /// The config file path was empty.
756    #[error("tool-config-file has empty config file path: {input}")]
757    EmptyConfigFile {
758        /// The input that failed to parse.
759        input: String,
760    },
761
762    /// The config file was not an absolute path.
763    #[error("tool-config-file is not an absolute path: {config_file}")]
764    ConfigFileNotAbsolute {
765        /// The file name that wasn't absolute.
766        config_file: Utf8PathBuf,
767    },
768}
769
770/// Errors that can occur while loading user config.
771#[derive(Debug, Error)]
772#[non_exhaustive]
773pub enum UserConfigError {
774    /// The user config file specified via `--user-config-file` or
775    /// `NEXTEST_USER_CONFIG_FILE` does not exist.
776    #[error("user config file not found at {path}")]
777    FileNotFound {
778        /// The path that was specified.
779        path: Utf8PathBuf,
780    },
781
782    /// Failed to read the user config file.
783    #[error("failed to read user config at {path}")]
784    Read {
785        /// The path to the config file.
786        path: Utf8PathBuf,
787        /// The underlying I/O error.
788        #[source]
789        error: std::io::Error,
790    },
791
792    /// Failed to parse the user config file.
793    #[error("failed to parse user config at {path}")]
794    Parse {
795        /// The path to the config file.
796        path: Utf8PathBuf,
797        /// The underlying TOML parse error.
798        #[source]
799        error: toml::de::Error,
800    },
801
802    /// The user config path contains non-UTF-8 characters.
803    #[error("user config path contains non-UTF-8 characters")]
804    NonUtf8Path {
805        /// The underlying error from path conversion.
806        #[source]
807        error: FromPathBufError,
808    },
809
810    /// Failed to compile a platform spec in an override.
811    #[error(
812        "for user config at {path}, failed to compile platform spec in [[overrides]] at index {index}"
813    )]
814    OverridePlatformSpec {
815        /// The path to the config file.
816        path: Utf8PathBuf,
817        /// The index of the override in the array.
818        index: usize,
819        /// The underlying target-spec error.
820        #[source]
821        error: target_spec::Error,
822    },
823}
824
825/// Error returned while parsing a [`MaxFail`](crate::config::elements::MaxFail) input.
826#[derive(Clone, Debug, Error)]
827#[error("unrecognized value for max-fail: {reason}")]
828pub struct MaxFailParseError {
829    /// The reason parsing failed.
830    pub reason: String,
831}
832
833impl MaxFailParseError {
834    pub(crate) fn new(reason: impl Into<String>) -> Self {
835        Self {
836            reason: reason.into(),
837        }
838    }
839}
840
841/// Error returned while parsing a [`StressCount`](crate::runner::StressCount) input.
842#[derive(Clone, Debug, Error)]
843#[error(
844    "unrecognized value for stress-count: {input}\n\
845     (hint: expected either a positive integer or \"infinite\")"
846)]
847pub struct StressCountParseError {
848    /// The input that failed to parse.
849    pub input: String,
850}
851
852impl StressCountParseError {
853    pub(crate) fn new(input: impl Into<String>) -> Self {
854        Self {
855            input: input.into(),
856        }
857    }
858}
859
860/// An error that occurred while parsing a debugger command.
861#[derive(Clone, Debug, Error)]
862#[non_exhaustive]
863pub enum DebuggerCommandParseError {
864    /// The command string could not be parsed as shell words.
865    #[error(transparent)]
866    ShellWordsParse(shell_words::ParseError),
867
868    /// The command was empty.
869    #[error("debugger command cannot be empty")]
870    EmptyCommand,
871}
872
873/// An error that occurred while parsing a tracer command.
874#[derive(Clone, Debug, Error)]
875#[non_exhaustive]
876pub enum TracerCommandParseError {
877    /// The command string could not be parsed as shell words.
878    #[error(transparent)]
879    ShellWordsParse(shell_words::ParseError),
880
881    /// The command was empty.
882    #[error("tracer command cannot be empty")]
883    EmptyCommand,
884}
885
886/// Error returned while parsing a [`TestThreads`](crate::config::elements::TestThreads) value.
887#[derive(Clone, Debug, Error)]
888#[error(
889    "unrecognized value for test-threads: {input}\n(hint: expected either an integer or \"num-cpus\")"
890)]
891pub struct TestThreadsParseError {
892    /// The input that failed to parse.
893    pub input: String,
894}
895
896impl TestThreadsParseError {
897    pub(crate) fn new(input: impl Into<String>) -> Self {
898        Self {
899            input: input.into(),
900        }
901    }
902}
903
904/// An error that occurs while parsing a
905/// [`PartitionerBuilder`](crate::partition::PartitionerBuilder) input.
906#[derive(Clone, Debug, Error)]
907pub struct PartitionerBuilderParseError {
908    expected_format: Option<&'static str>,
909    message: Cow<'static, str>,
910}
911
912impl PartitionerBuilderParseError {
913    pub(crate) fn new(
914        expected_format: Option<&'static str>,
915        message: impl Into<Cow<'static, str>>,
916    ) -> Self {
917        Self {
918            expected_format,
919            message: message.into(),
920        }
921    }
922}
923
924impl fmt::Display for PartitionerBuilderParseError {
925    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
926        match self.expected_format {
927            Some(format) => {
928                write!(
929                    f,
930                    "partition must be in the format \"{}\":\n{}",
931                    format, self.message
932                )
933            }
934            None => write!(f, "{}", self.message),
935        }
936    }
937}
938
939/// An error that occurs while building a
940/// [`TestFilter`](crate::test_filter::TestFilter).
941#[derive(Clone, Debug, Error)]
942pub enum TestFilterBuildError {
943    /// An error that occurred while constructing test filters.
944    #[error("error constructing test filters")]
945    Construct {
946        /// The underlying error.
947        #[from]
948        error: aho_corasick::BuildError,
949    },
950}
951
952/// An error occurred in [`PathMapper::new`](crate::reuse_build::PathMapper::new).
953#[derive(Debug, Error)]
954pub enum PathMapperConstructError {
955    /// An error occurred while canonicalizing a directory.
956    #[error("{kind} `{input}` failed to canonicalize")]
957    Canonicalization {
958        /// The directory that failed to be canonicalized.
959        kind: PathMapperConstructKind,
960
961        /// The input provided.
962        input: Utf8PathBuf,
963
964        /// The error that occurred.
965        #[source]
966        err: std::io::Error,
967    },
968    /// The canonicalized path isn't valid UTF-8.
969    #[error("{kind} `{input}` canonicalized to a non-UTF-8 path")]
970    NonUtf8Path {
971        /// The directory that failed to be canonicalized.
972        kind: PathMapperConstructKind,
973
974        /// The input provided.
975        input: Utf8PathBuf,
976
977        /// The underlying error.
978        #[source]
979        err: FromPathBufError,
980    },
981    /// A provided input is not a directory.
982    #[error("{kind} `{canonicalized_path}` is not a directory")]
983    NotADirectory {
984        /// The directory that failed to be canonicalized.
985        kind: PathMapperConstructKind,
986
987        /// The input provided.
988        input: Utf8PathBuf,
989
990        /// The canonicalized path that wasn't a directory.
991        canonicalized_path: Utf8PathBuf,
992    },
993}
994
995impl PathMapperConstructError {
996    /// The kind of directory.
997    pub fn kind(&self) -> PathMapperConstructKind {
998        match self {
999            Self::Canonicalization { kind, .. }
1000            | Self::NonUtf8Path { kind, .. }
1001            | Self::NotADirectory { kind, .. } => *kind,
1002        }
1003    }
1004
1005    /// The input path that failed.
1006    pub fn input(&self) -> &Utf8Path {
1007        match self {
1008            Self::Canonicalization { input, .. }
1009            | Self::NonUtf8Path { input, .. }
1010            | Self::NotADirectory { input, .. } => input,
1011        }
1012    }
1013}
1014
1015/// The kind of directory that failed to be read in
1016/// [`PathMapper::new`](crate::reuse_build::PathMapper::new).
1017///
1018/// Returned as part of [`PathMapperConstructError`].
1019#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1020pub enum PathMapperConstructKind {
1021    /// The workspace root.
1022    WorkspaceRoot,
1023
1024    /// The target directory.
1025    TargetDir,
1026}
1027
1028impl fmt::Display for PathMapperConstructKind {
1029    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1030        match self {
1031            Self::WorkspaceRoot => write!(f, "remapped workspace root"),
1032            Self::TargetDir => write!(f, "remapped target directory"),
1033        }
1034    }
1035}
1036
1037/// An error that occurs while parsing Rust build metadata from a summary.
1038#[derive(Debug, Error)]
1039pub enum RustBuildMetaParseError {
1040    /// An error occurred while deserializing the platform.
1041    #[error("error deserializing platform from build metadata")]
1042    PlatformDeserializeError(#[from] target_spec::Error),
1043
1044    /// The host platform could not be determined.
1045    #[error("the host platform could not be determined")]
1046    DetectBuildTargetError(#[source] target_spec::Error),
1047
1048    /// The build metadata includes features unsupported.
1049    #[error("unsupported features in the build metadata: {message}")]
1050    Unsupported {
1051        /// The detailed error message.
1052        message: String,
1053    },
1054}
1055
1056/// Error returned when a user-supplied format version fails to be parsed to a
1057/// valid and supported version.
1058#[derive(Clone, Debug, thiserror::Error)]
1059#[error("invalid format version: {input}")]
1060pub struct FormatVersionError {
1061    /// The input that failed to parse.
1062    pub input: String,
1063    /// The underlying error.
1064    #[source]
1065    pub error: FormatVersionErrorInner,
1066}
1067
1068/// The different errors that can occur when parsing and validating a format version.
1069#[derive(Clone, Debug, thiserror::Error)]
1070pub enum FormatVersionErrorInner {
1071    /// The input did not have a valid syntax.
1072    #[error("expected format version in form of `{expected}`")]
1073    InvalidFormat {
1074        /// The expected pseudo format.
1075        expected: &'static str,
1076    },
1077    /// A decimal integer was expected but could not be parsed.
1078    #[error("version component `{which}` could not be parsed as an integer")]
1079    InvalidInteger {
1080        /// Which component was invalid.
1081        which: &'static str,
1082        /// The parse failure.
1083        #[source]
1084        err: std::num::ParseIntError,
1085    },
1086    /// The version component was not within the expected range.
1087    #[error("version component `{which}` value {value} is out of range {range:?}")]
1088    InvalidValue {
1089        /// The component which was out of range.
1090        which: &'static str,
1091        /// The value that was parsed.
1092        value: u8,
1093        /// The range of valid values for the component.
1094        range: std::ops::Range<u8>,
1095    },
1096}
1097
1098/// An error that occurs in [`BinaryList::from_messages`](crate::list::BinaryList::from_messages),
1099/// [`BinaryListBuilder`](crate::list::BinaryListBuilder), or
1100/// [`RustTestArtifact::from_binary_list`](crate::list::RustTestArtifact::from_binary_list).
1101#[derive(Debug, Error)]
1102#[non_exhaustive]
1103pub enum FromMessagesError {
1104    /// An error occurred while reading Cargo's JSON messages.
1105    #[error("error reading Cargo JSON messages")]
1106    ReadMessages(#[source] std::io::Error),
1107
1108    /// An error occurred while querying the package graph.
1109    #[error("error querying package graph")]
1110    PackageGraph(#[source] guppy::Error),
1111
1112    /// A target in the package graph was missing `kind` information.
1113    #[error("missing kind for target {binary_name} in package {package_name}")]
1114    MissingTargetKind {
1115        /// The name of the malformed package.
1116        package_name: String,
1117        /// The name of the malformed target within the package.
1118        binary_name: String,
1119    },
1120}
1121
1122/// An error that occurs while parsing test list output.
1123#[derive(Debug, Error)]
1124#[non_exhaustive]
1125pub enum CreateTestListError {
1126    /// The proposed cwd for a process is not a directory.
1127    #[error(
1128        "for `{binary_id}`, current directory `{cwd}` is not a directory\n\
1129         (hint: ensure project source is available at this location)"
1130    )]
1131    CwdIsNotDir {
1132        /// The binary ID for which the current directory wasn't found.
1133        binary_id: RustBinaryId,
1134
1135        /// The current directory that wasn't found.
1136        cwd: Utf8PathBuf,
1137    },
1138
1139    /// Running a command to gather the list of tests failed to execute.
1140    #[error(
1141        "for `{binary_id}`, running command `{}` failed to execute",
1142        shell_words::join(command)
1143    )]
1144    CommandExecFail {
1145        /// The binary ID for which gathering the list of tests failed.
1146        binary_id: RustBinaryId,
1147
1148        /// The command that was run.
1149        command: Vec<String>,
1150
1151        /// The underlying error.
1152        #[source]
1153        error: std::io::Error,
1154    },
1155
1156    /// Running a command to gather the list of tests failed failed with a non-zero exit code.
1157    #[error(
1158        "for `{binary_id}`, command `{}` {}\n--- stdout:\n{}\n--- stderr:\n{}\n---",
1159        shell_words::join(command),
1160        display_exited_with(*exit_status),
1161        String::from_utf8_lossy(stdout),
1162        String::from_utf8_lossy(stderr),
1163    )]
1164    CommandFail {
1165        /// The binary ID for which gathering the list of tests failed.
1166        binary_id: RustBinaryId,
1167
1168        /// The command that was run.
1169        command: Vec<String>,
1170
1171        /// The exit status with which the command failed.
1172        exit_status: ExitStatus,
1173
1174        /// Standard output for the command.
1175        stdout: Vec<u8>,
1176
1177        /// Standard error for the command.
1178        stderr: Vec<u8>,
1179    },
1180
1181    /// Running a command to gather the list of tests produced a non-UTF-8 standard output.
1182    #[error(
1183        "for `{binary_id}`, command `{}` produced non-UTF-8 output:\n--- stdout:\n{}\n--- stderr:\n{}\n---",
1184        shell_words::join(command),
1185        String::from_utf8_lossy(stdout),
1186        String::from_utf8_lossy(stderr)
1187    )]
1188    CommandNonUtf8 {
1189        /// The binary ID for which gathering the list of tests failed.
1190        binary_id: RustBinaryId,
1191
1192        /// The command that was run.
1193        command: Vec<String>,
1194
1195        /// Standard output for the command.
1196        stdout: Vec<u8>,
1197
1198        /// Standard error for the command.
1199        stderr: Vec<u8>,
1200    },
1201
1202    /// An error occurred while parsing a line in the test output.
1203    #[error("for `{binary_id}`, {message}\nfull output:\n{full_output}")]
1204    ParseLine {
1205        /// The binary ID for which parsing the list of tests failed.
1206        binary_id: RustBinaryId,
1207
1208        /// A descriptive message.
1209        message: Cow<'static, str>,
1210
1211        /// The full output.
1212        full_output: String,
1213    },
1214
1215    /// An error occurred while joining paths for dynamic libraries.
1216    #[error(
1217        "error joining dynamic library paths for {}: [{}]",
1218        dylib_path_envvar(),
1219        itertools::join(.new_paths, ", ")
1220    )]
1221    DylibJoinPaths {
1222        /// New paths attempted to be added to the dynamic library environment variable.
1223        new_paths: Vec<Utf8PathBuf>,
1224
1225        /// The underlying error.
1226        #[source]
1227        error: JoinPathsError,
1228    },
1229
1230    /// Creating a Tokio runtime failed.
1231    #[error("error creating Tokio runtime")]
1232    TokioRuntimeCreate(#[source] std::io::Error),
1233}
1234
1235impl CreateTestListError {
1236    pub(crate) fn parse_line(
1237        binary_id: RustBinaryId,
1238        message: impl Into<Cow<'static, str>>,
1239        full_output: impl Into<String>,
1240    ) -> Self {
1241        Self::ParseLine {
1242            binary_id,
1243            message: message.into(),
1244            full_output: full_output.into(),
1245        }
1246    }
1247
1248    pub(crate) fn dylib_join_paths(new_paths: Vec<Utf8PathBuf>, error: JoinPathsError) -> Self {
1249        Self::DylibJoinPaths { new_paths, error }
1250    }
1251}
1252
1253/// An error that occurs while writing list output.
1254#[derive(Debug, Error)]
1255#[non_exhaustive]
1256pub enum WriteTestListError {
1257    /// An error occurred while writing the list to the provided output.
1258    #[error("error writing to output")]
1259    Io(#[source] std::io::Error),
1260
1261    /// An error occurred while serializing JSON, or while writing it to the provided output.
1262    #[error("error serializing to JSON")]
1263    Json(#[source] serde_json::Error),
1264}
1265
1266/// An error occurred while configuring handles.
1267///
1268/// Only relevant on Windows.
1269#[derive(Debug, Error)]
1270pub enum ConfigureHandleInheritanceError {
1271    /// An error occurred. This can only happen on Windows.
1272    #[cfg(windows)]
1273    #[error("error configuring handle inheritance")]
1274    WindowsError(#[from] std::io::Error),
1275}
1276
1277/// An error that occurs while building the test runner.
1278#[derive(Debug, Error)]
1279#[non_exhaustive]
1280pub enum TestRunnerBuildError {
1281    /// An error occurred while creating the Tokio runtime.
1282    #[error("error creating Tokio runtime")]
1283    TokioRuntimeCreate(#[source] std::io::Error),
1284
1285    /// An error occurred while setting up signals.
1286    #[error("error setting up signals")]
1287    SignalHandlerSetupError(#[from] SignalHandlerSetupError),
1288}
1289
1290/// Errors that occurred while managing test runner Tokio tasks.
1291#[derive(Debug, Error)]
1292pub struct TestRunnerExecuteErrors<E> {
1293    /// An error that occurred while reporting results to the reporter callback.
1294    pub report_error: Option<E>,
1295
1296    /// Join errors (typically panics) that occurred while running the test
1297    /// runner.
1298    pub join_errors: Vec<tokio::task::JoinError>,
1299}
1300
1301impl<E: std::error::Error> fmt::Display for TestRunnerExecuteErrors<E> {
1302    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1303        if let Some(report_error) = &self.report_error {
1304            write!(f, "error reporting results: {report_error}")?;
1305        }
1306
1307        if !self.join_errors.is_empty() {
1308            if self.report_error.is_some() {
1309                write!(f, "; ")?;
1310            }
1311
1312            write!(f, "errors joining tasks: ")?;
1313
1314            for (i, join_error) in self.join_errors.iter().enumerate() {
1315                if i > 0 {
1316                    write!(f, ", ")?;
1317                }
1318
1319                write!(f, "{join_error}")?;
1320            }
1321        }
1322
1323        Ok(())
1324    }
1325}
1326
1327/// Represents an unknown archive format.
1328///
1329/// Returned by [`ArchiveFormat::autodetect`].
1330#[derive(Debug, Error)]
1331#[error(
1332    "could not detect archive format from file name `{file_name}` (supported extensions: {})",
1333    supported_extensions()
1334)]
1335pub struct UnknownArchiveFormat {
1336    /// The name of the archive file without any leading components.
1337    pub file_name: String,
1338}
1339
1340fn supported_extensions() -> String {
1341    ArchiveFormat::SUPPORTED_FORMATS
1342        .iter()
1343        .map(|(extension, _)| *extension)
1344        .join(", ")
1345}
1346
1347/// An error that occurs while archiving data.
1348#[derive(Debug, Error)]
1349#[non_exhaustive]
1350pub enum ArchiveCreateError {
1351    /// An error occurred while creating the binary list to be written.
1352    #[error("error creating binary list")]
1353    CreateBinaryList(#[source] WriteTestListError),
1354
1355    /// An extra path was missing.
1356    #[error("extra path `{}` not found", .redactor.redact_path(path))]
1357    MissingExtraPath {
1358        /// The path that was missing.
1359        path: Utf8PathBuf,
1360
1361        /// A redactor for the path.
1362        ///
1363        /// (This should eventually move to being a field for a wrapper struct, but it's okay for
1364        /// now.)
1365        redactor: Redactor,
1366    },
1367
1368    /// An error occurred while reading data from a file on disk.
1369    #[error("while archiving {step}, error writing {} `{path}` to archive", kind_str(*.is_dir))]
1370    InputFileRead {
1371        /// The step that the archive errored at.
1372        step: ArchiveStep,
1373
1374        /// The name of the file that could not be read.
1375        path: Utf8PathBuf,
1376
1377        /// Whether this is a directory. `None` means the status was unknown.
1378        is_dir: Option<bool>,
1379
1380        /// The error that occurred.
1381        #[source]
1382        error: std::io::Error,
1383    },
1384
1385    /// An error occurred while reading entries from a directory on disk.
1386    #[error("error reading directory entry from `{path}")]
1387    DirEntryRead {
1388        /// The name of the directory from which entries couldn't be read.
1389        path: Utf8PathBuf,
1390
1391        /// The error that occurred.
1392        #[source]
1393        error: std::io::Error,
1394    },
1395
1396    /// An error occurred while writing data to the output file.
1397    #[error("error writing to archive")]
1398    OutputArchiveIo(#[source] std::io::Error),
1399
1400    /// An error occurred in the reporter.
1401    #[error("error reporting archive status")]
1402    ReporterIo(#[source] std::io::Error),
1403}
1404
1405fn kind_str(is_dir: Option<bool>) -> &'static str {
1406    match is_dir {
1407        Some(true) => "directory",
1408        Some(false) => "file",
1409        None => "path",
1410    }
1411}
1412
1413/// An error occurred while materializing a metadata path.
1414#[derive(Debug, Error)]
1415pub enum MetadataMaterializeError {
1416    /// An I/O error occurred while reading the metadata file.
1417    #[error("I/O error reading metadata file `{path}`")]
1418    Read {
1419        /// The path that was being read.
1420        path: Utf8PathBuf,
1421
1422        /// The error that occurred.
1423        #[source]
1424        error: std::io::Error,
1425    },
1426
1427    /// A JSON deserialization error occurred while reading the metadata file.
1428    #[error("error deserializing metadata file `{path}`")]
1429    Deserialize {
1430        /// The path that was being read.
1431        path: Utf8PathBuf,
1432
1433        /// The error that occurred.
1434        #[source]
1435        error: serde_json::Error,
1436    },
1437
1438    /// An error occurred while parsing Rust build metadata.
1439    #[error("error parsing Rust build metadata from `{path}`")]
1440    RustBuildMeta {
1441        /// The path that was deserialized.
1442        path: Utf8PathBuf,
1443
1444        /// The error that occurred.
1445        #[source]
1446        error: RustBuildMetaParseError,
1447    },
1448
1449    /// An error occurred converting data into a `PackageGraph`.
1450    #[error("error building package graph from `{path}`")]
1451    PackageGraphConstruct {
1452        /// The path that was deserialized.
1453        path: Utf8PathBuf,
1454
1455        /// The error that occurred.
1456        #[source]
1457        error: guppy::Error,
1458    },
1459}
1460
1461/// An error occurred while reading a file.
1462///
1463/// Returned as part of both [`ArchiveCreateError`] and [`ArchiveExtractError`].
1464#[derive(Debug, Error)]
1465#[non_exhaustive]
1466pub enum ArchiveReadError {
1467    /// An I/O error occurred while reading the archive.
1468    #[error("I/O error reading archive")]
1469    Io(#[source] std::io::Error),
1470
1471    /// A path wasn't valid UTF-8.
1472    #[error("path in archive `{}` wasn't valid UTF-8", String::from_utf8_lossy(.0))]
1473    NonUtf8Path(Vec<u8>),
1474
1475    /// A file path within the archive didn't begin with "target/".
1476    #[error("path in archive `{0}` doesn't start with `target/`")]
1477    NoTargetPrefix(Utf8PathBuf),
1478
1479    /// A file path within the archive had an invalid component within it.
1480    #[error("path in archive `{path}` contains an invalid component `{component}`")]
1481    InvalidComponent {
1482        /// The path that had an invalid component.
1483        path: Utf8PathBuf,
1484
1485        /// The invalid component.
1486        component: String,
1487    },
1488
1489    /// An error occurred while reading a checksum.
1490    #[error("corrupted archive: checksum read error for path `{path}`")]
1491    ChecksumRead {
1492        /// The path for which there was a checksum read error.
1493        path: Utf8PathBuf,
1494
1495        /// The error that occurred.
1496        #[source]
1497        error: std::io::Error,
1498    },
1499
1500    /// An entry had an invalid checksum.
1501    #[error("corrupted archive: invalid checksum for path `{path}`")]
1502    InvalidChecksum {
1503        /// The path that had an invalid checksum.
1504        path: Utf8PathBuf,
1505
1506        /// The expected checksum.
1507        expected: u32,
1508
1509        /// The actual checksum.
1510        actual: u32,
1511    },
1512
1513    /// A metadata file wasn't found.
1514    #[error("metadata file `{0}` not found in archive")]
1515    MetadataFileNotFound(&'static Utf8Path),
1516
1517    /// An error occurred while deserializing a metadata file.
1518    #[error("error deserializing metadata file `{path}` in archive")]
1519    MetadataDeserializeError {
1520        /// The name of the metadata file.
1521        path: &'static Utf8Path,
1522
1523        /// The deserialize error.
1524        #[source]
1525        error: serde_json::Error,
1526    },
1527
1528    /// An error occurred while building a `PackageGraph`.
1529    #[error("error building package graph from `{path}` in archive")]
1530    PackageGraphConstructError {
1531        /// The name of the metadata file.
1532        path: &'static Utf8Path,
1533
1534        /// The error.
1535        #[source]
1536        error: guppy::Error,
1537    },
1538}
1539
1540/// An error occurred while extracting a file.
1541///
1542/// Returned by [`extract_archive`](crate::reuse_build::ReuseBuildInfo::extract_archive).
1543#[derive(Debug, Error)]
1544#[non_exhaustive]
1545pub enum ArchiveExtractError {
1546    /// An error occurred while creating a temporary directory.
1547    #[error("error creating temporary directory")]
1548    TempDirCreate(#[source] std::io::Error),
1549
1550    /// An error occurred while canonicalizing the destination directory.
1551    #[error("error canonicalizing destination directory `{dir}`")]
1552    DestDirCanonicalization {
1553        /// The directory that failed to canonicalize.
1554        dir: Utf8PathBuf,
1555
1556        /// The error that occurred.
1557        #[source]
1558        error: std::io::Error,
1559    },
1560
1561    /// The destination already exists and `--overwrite` was not passed in.
1562    #[error("destination `{0}` already exists")]
1563    DestinationExists(Utf8PathBuf),
1564
1565    /// An error occurred while reading the archive.
1566    #[error("error reading archive")]
1567    Read(#[source] ArchiveReadError),
1568
1569    /// An error occurred while deserializing Rust build metadata.
1570    #[error("error deserializing Rust build metadata")]
1571    RustBuildMeta(#[from] RustBuildMetaParseError),
1572
1573    /// An error occurred while writing out a file to the destination directory.
1574    #[error("error writing file `{path}` to disk")]
1575    WriteFile {
1576        /// The path that we couldn't write out.
1577        path: Utf8PathBuf,
1578
1579        /// The error that occurred.
1580        #[source]
1581        error: std::io::Error,
1582    },
1583
1584    /// An error occurred while reporting the extraction status.
1585    #[error("error reporting extract status")]
1586    ReporterIo(std::io::Error),
1587}
1588
1589/// An error that occurs while writing an event.
1590#[derive(Debug, Error)]
1591#[non_exhaustive]
1592pub enum WriteEventError {
1593    /// An error occurred while writing the event to the provided output.
1594    #[error("error writing to output")]
1595    Io(#[source] std::io::Error),
1596
1597    /// An error occurred while operating on the file system.
1598    #[error("error operating on path {file}")]
1599    Fs {
1600        /// The file being operated on.
1601        file: Utf8PathBuf,
1602
1603        /// The underlying IO error.
1604        #[source]
1605        error: std::io::Error,
1606    },
1607
1608    /// An error occurred while producing JUnit XML.
1609    #[error("error writing JUnit output to {file}")]
1610    Junit {
1611        /// The output file.
1612        file: Utf8PathBuf,
1613
1614        /// The underlying error.
1615        #[source]
1616        error: quick_junit::SerializeError,
1617    },
1618}
1619
1620/// An error occurred while constructing a [`CargoConfigs`](crate::cargo_config::CargoConfigs)
1621/// instance.
1622#[derive(Debug, Error)]
1623#[non_exhaustive]
1624pub enum CargoConfigError {
1625    /// Failed to retrieve the current directory.
1626    #[error("failed to retrieve current directory")]
1627    GetCurrentDir(#[source] std::io::Error),
1628
1629    /// The current directory was invalid UTF-8.
1630    #[error("current directory is invalid UTF-8")]
1631    CurrentDirInvalidUtf8(#[source] FromPathBufError),
1632
1633    /// Parsing a CLI config option failed.
1634    #[error("failed to parse --config argument `{config_str}` as TOML")]
1635    CliConfigParseError {
1636        /// The CLI config option.
1637        config_str: String,
1638
1639        /// The error that occurred trying to parse the config.
1640        #[source]
1641        error: toml_edit::TomlError,
1642    },
1643
1644    /// Deserializing a CLI config option into domain types failed.
1645    #[error("failed to deserialize --config argument `{config_str}` as TOML")]
1646    CliConfigDeError {
1647        /// The CLI config option.
1648        config_str: String,
1649
1650        /// The error that occurred trying to deserialize the config.
1651        #[source]
1652        error: toml_edit::de::Error,
1653    },
1654
1655    /// A CLI config option is not in the dotted key format.
1656    #[error(
1657        "invalid format for --config argument `{config_str}` (should be a dotted key expression)"
1658    )]
1659    InvalidCliConfig {
1660        /// The CLI config option.
1661        config_str: String,
1662
1663        /// The reason why this Cargo CLI config is invalid.
1664        #[source]
1665        reason: InvalidCargoCliConfigReason,
1666    },
1667
1668    /// A non-UTF-8 path was encountered.
1669    #[error("non-UTF-8 path encountered")]
1670    NonUtf8Path(#[source] FromPathBufError),
1671
1672    /// Failed to retrieve the Cargo home directory.
1673    #[error("failed to retrieve the Cargo home directory")]
1674    GetCargoHome(#[source] std::io::Error),
1675
1676    /// Failed to canonicalize a path
1677    #[error("failed to canonicalize path `{path}")]
1678    FailedPathCanonicalization {
1679        /// The path that failed to canonicalize
1680        path: Utf8PathBuf,
1681
1682        /// The error the occurred during canonicalization
1683        #[source]
1684        error: std::io::Error,
1685    },
1686
1687    /// Failed to read config file
1688    #[error("failed to read config at `{path}`")]
1689    ConfigReadError {
1690        /// The path of the config file
1691        path: Utf8PathBuf,
1692
1693        /// The error that occurred trying to read the config file
1694        #[source]
1695        error: std::io::Error,
1696    },
1697
1698    /// Failed to deserialize config file
1699    #[error(transparent)]
1700    ConfigParseError(#[from] Box<CargoConfigParseError>),
1701}
1702
1703/// Failed to deserialize config file
1704///
1705/// We introduce this extra indirection, because of the `clippy::result_large_err` rule on Windows.
1706#[derive(Debug, Error)]
1707#[error("failed to parse config at `{path}`")]
1708pub struct CargoConfigParseError {
1709    /// The path of the config file
1710    pub path: Utf8PathBuf,
1711
1712    /// The error that occurred trying to deserialize the config file
1713    #[source]
1714    pub error: toml::de::Error,
1715}
1716
1717/// The reason an invalid CLI config failed.
1718///
1719/// Part of [`CargoConfigError::InvalidCliConfig`].
1720#[derive(Copy, Clone, Debug, Error, Eq, PartialEq)]
1721#[non_exhaustive]
1722pub enum InvalidCargoCliConfigReason {
1723    /// The argument is not a TOML dotted key expression.
1724    #[error("was not a TOML dotted key expression (such as `build.jobs = 2`)")]
1725    NotDottedKv,
1726
1727    /// The argument includes non-whitespace decoration.
1728    #[error("includes non-whitespace decoration")]
1729    IncludesNonWhitespaceDecoration,
1730
1731    /// The argument sets a value to an inline table.
1732    #[error("sets a value to an inline table, which is not accepted")]
1733    SetsValueToInlineTable,
1734
1735    /// The argument sets a value to an array of tables.
1736    #[error("sets a value to an array of tables, which is not accepted")]
1737    SetsValueToArrayOfTables,
1738
1739    /// The argument doesn't provide a value.
1740    #[error("doesn't provide a value")]
1741    DoesntProvideValue,
1742}
1743
1744/// The host platform couldn't be detected.
1745#[derive(Debug, Error)]
1746pub enum HostPlatformDetectError {
1747    /// Spawning `rustc -vV` failed, and detecting the build target failed as
1748    /// well.
1749    #[error(
1750        "error spawning `rustc -vV`, and detecting the build \
1751         target failed as well\n\
1752         - rustc spawn error: {}\n\
1753         - build target error: {}\n",
1754        DisplayErrorChain::new_with_initial_indent("  ", error),
1755        DisplayErrorChain::new_with_initial_indent("  ", build_target_error)
1756    )]
1757    RustcVvSpawnError {
1758        /// The error.
1759        error: std::io::Error,
1760
1761        /// The error that occurred while detecting the build target.
1762        build_target_error: Box<target_spec::Error>,
1763    },
1764
1765    /// `rustc -vV` exited with a non-zero code, and detecting the build target
1766    /// failed as well.
1767    #[error(
1768        "`rustc -vV` failed with {}, and detecting the \
1769         build target failed as well\n\
1770         - `rustc -vV` stdout:\n{}\n\
1771         - `rustc -vV` stderr:\n{}\n\
1772         - build target error:\n{}\n",
1773        status,
1774        DisplayIndented { item: String::from_utf8_lossy(stdout), indent: "  " },
1775        DisplayIndented { item: String::from_utf8_lossy(stderr), indent: "  " },
1776        DisplayErrorChain::new_with_initial_indent("  ", build_target_error)
1777    )]
1778    RustcVvFailed {
1779        /// The status.
1780        status: ExitStatus,
1781
1782        /// The standard output from `rustc -vV`.
1783        stdout: Vec<u8>,
1784
1785        /// The standard error from `rustc -vV`.
1786        stderr: Vec<u8>,
1787
1788        /// The error that occurred while detecting the build target.
1789        build_target_error: Box<target_spec::Error>,
1790    },
1791
1792    /// Parsing the host platform failed, and detecting the build target failed
1793    /// as well.
1794    #[error(
1795        "parsing `rustc -vV` output failed, and detecting the build target \
1796         failed as well\n\
1797         - host platform error:\n{}\n\
1798         - build target error:\n{}\n",
1799        DisplayErrorChain::new_with_initial_indent("  ", host_platform_error),
1800        DisplayErrorChain::new_with_initial_indent("  ", build_target_error)
1801    )]
1802    HostPlatformParseError {
1803        /// The error that occurred while parsing the host platform.
1804        host_platform_error: Box<target_spec::Error>,
1805
1806        /// The error that occurred while detecting the build target.
1807        build_target_error: Box<target_spec::Error>,
1808    },
1809
1810    /// Test-only code: `rustc -vV` was not queried, and detecting the build
1811    /// target failed as well.
1812    #[error("test-only code, so `rustc -vV` was not called; failed to detect build target")]
1813    BuildTargetError {
1814        /// The error that occurred while detecting the build target.
1815        #[source]
1816        build_target_error: Box<target_spec::Error>,
1817    },
1818}
1819
1820/// An error occurred while determining the cross-compiling target triple.
1821#[derive(Debug, Error)]
1822pub enum TargetTripleError {
1823    /// The environment variable contained non-utf8 content
1824    #[error(
1825        "environment variable '{}' contained non-UTF-8 data",
1826        TargetTriple::CARGO_BUILD_TARGET_ENV
1827    )]
1828    InvalidEnvironmentVar,
1829
1830    /// An error occurred while deserializing the platform.
1831    #[error("error deserializing target triple from {source}")]
1832    TargetSpecError {
1833        /// The source from which the triple couldn't be parsed.
1834        source: TargetTripleSource,
1835
1836        /// The error that occurred parsing the triple.
1837        #[source]
1838        error: target_spec::Error,
1839    },
1840
1841    /// For a custom platform, reading the target path failed.
1842    #[error("target path `{path}` is not a valid file")]
1843    TargetPathReadError {
1844        /// The source from which the triple couldn't be parsed.
1845        source: TargetTripleSource,
1846
1847        /// The path that we tried to read.
1848        path: Utf8PathBuf,
1849
1850        /// The error that occurred parsing the triple.
1851        #[source]
1852        error: std::io::Error,
1853    },
1854
1855    /// Failed to create a temporary directory for a custom platform.
1856    #[error(
1857        "for custom platform obtained from {source}, \
1858         failed to create temporary directory for custom platform"
1859    )]
1860    CustomPlatformTempDirError {
1861        /// The source of the target triple.
1862        source: TargetTripleSource,
1863
1864        /// The error that occurred during the create.
1865        #[source]
1866        error: std::io::Error,
1867    },
1868
1869    /// Failed to write a custom platform to disk.
1870    #[error(
1871        "for custom platform obtained from {source}, \
1872         failed to write JSON to temporary path `{path}`"
1873    )]
1874    CustomPlatformWriteError {
1875        /// The source of the target triple.
1876        source: TargetTripleSource,
1877
1878        /// The path that we tried to write to.
1879        path: Utf8PathBuf,
1880
1881        /// The error that occurred during the write.
1882        #[source]
1883        error: std::io::Error,
1884    },
1885
1886    /// Failed to close a temporary directory for an extracted custom platform.
1887    #[error(
1888        "for custom platform obtained from {source}, \
1889         failed to close temporary directory `{dir_path}`"
1890    )]
1891    CustomPlatformCloseError {
1892        /// The source of the target triple.
1893        source: TargetTripleSource,
1894
1895        /// The directory that we tried to delete.
1896        dir_path: Utf8PathBuf,
1897
1898        /// The error that occurred during the close.
1899        #[source]
1900        error: std::io::Error,
1901    },
1902}
1903
1904impl TargetTripleError {
1905    /// Returns a [`miette::Report`] for the source, if available.
1906    ///
1907    /// This should be preferred over [`std::error::Error::source`] if
1908    /// available.
1909    pub fn source_report(&self) -> Option<miette::Report> {
1910        match self {
1911            Self::TargetSpecError { error, .. } => {
1912                Some(miette::Report::new_boxed(error.clone().into_diagnostic()))
1913            }
1914            // The remaining types are covered via the error source path.
1915            TargetTripleError::InvalidEnvironmentVar
1916            | TargetTripleError::TargetPathReadError { .. }
1917            | TargetTripleError::CustomPlatformTempDirError { .. }
1918            | TargetTripleError::CustomPlatformWriteError { .. }
1919            | TargetTripleError::CustomPlatformCloseError { .. } => None,
1920        }
1921    }
1922}
1923
1924/// An error occurred determining the target runner
1925#[derive(Debug, Error)]
1926pub enum TargetRunnerError {
1927    /// An environment variable contained non-utf8 content
1928    #[error("environment variable '{0}' contained non-UTF-8 data")]
1929    InvalidEnvironmentVar(String),
1930
1931    /// An environment variable or config key was found that matches the target
1932    /// triple, but it didn't actually contain a binary
1933    #[error("runner '{key}' = '{value}' did not contain a runner binary")]
1934    BinaryNotSpecified {
1935        /// The source under consideration.
1936        key: PlatformRunnerSource,
1937
1938        /// The value that was read from the key
1939        value: String,
1940    },
1941}
1942
1943/// An error that occurred while setting up the signal handler.
1944#[derive(Debug, Error)]
1945#[error("error setting up signal handler")]
1946pub struct SignalHandlerSetupError(#[from] std::io::Error);
1947
1948/// An error occurred while showing test groups.
1949#[derive(Debug, Error)]
1950pub enum ShowTestGroupsError {
1951    /// Unknown test groups were specified.
1952    #[error(
1953        "unknown test groups specified: {}\n(known groups: {})",
1954        unknown_groups.iter().join(", "),
1955        known_groups.iter().join(", "),
1956    )]
1957    UnknownGroups {
1958        /// The unknown test groups.
1959        unknown_groups: BTreeSet<TestGroup>,
1960
1961        /// All known test groups.
1962        known_groups: BTreeSet<TestGroup>,
1963    },
1964}
1965
1966/// An error occurred while processing profile's inherits setting
1967#[derive(Debug, Error, PartialEq, Eq, Hash)]
1968pub enum InheritsError {
1969    /// The default profile should not be able to inherit from other profiles
1970    #[error("the {} profile should not inherit from other profiles", .0)]
1971    DefaultProfileInheritance(String),
1972    /// An unknown/unfound profile was detected to inherit from in profile configuration
1973    #[error("profile {} inherits from an unknown profile {}", .0, .1)]
1974    UnknownInheritance(String, String),
1975    /// A self referential inheritance is detected from this profile
1976    #[error("a self referential inheritance is detected from profile: {}", .0)]
1977    SelfReferentialInheritance(String),
1978    /// An inheritance cycle was detected in the profile configuration.
1979    #[error("inheritance cycle detected in profile configuration from: {}", .0.iter().map(|scc| {
1980        format!("[{}]", scc.iter().join(", "))
1981    }).join(", "))]
1982    InheritanceCycle(Vec<Vec<String>>),
1983}
1984
1985// ---
1986// Record and replay errors
1987// ---
1988
1989/// An error that occurred while managing the run store.
1990#[derive(Debug, Error)]
1991pub enum RunStoreError {
1992    /// An error occurred while creating the run directory.
1993    #[error("error creating run directory `{run_dir}`")]
1994    RunDirCreate {
1995        /// The run directory that could not be created.
1996        run_dir: Utf8PathBuf,
1997
1998        /// The underlying error.
1999        #[source]
2000        error: std::io::Error,
2001    },
2002
2003    /// An error occurred while acquiring a file lock.
2004    #[error("error acquiring lock on `{path}`")]
2005    FileLock {
2006        /// The path to the lock file.
2007        path: Utf8PathBuf,
2008
2009        /// The underlying error.
2010        #[source]
2011        error: std::io::Error,
2012    },
2013
2014    /// Timed out waiting to acquire a file lock.
2015    #[error(
2016        "timed out acquiring lock on `{path}` after {timeout_secs}s (is the state directory \
2017         on a networked filesystem?)"
2018    )]
2019    FileLockTimeout {
2020        /// The path to the lock file.
2021        path: Utf8PathBuf,
2022
2023        /// The timeout duration in seconds.
2024        timeout_secs: u64,
2025    },
2026
2027    /// An error occurred while reading the run list.
2028    #[error("error reading run list from `{path}`")]
2029    RunListRead {
2030        /// The path to the run list file.
2031        path: Utf8PathBuf,
2032
2033        /// The underlying error.
2034        #[source]
2035        error: std::io::Error,
2036    },
2037
2038    /// An error occurred while deserializing the run list.
2039    #[error("error deserializing run list from `{path}`")]
2040    RunListDeserialize {
2041        /// The path to the run list file.
2042        path: Utf8PathBuf,
2043
2044        /// The underlying error.
2045        #[source]
2046        error: serde_json::Error,
2047    },
2048
2049    /// An error occurred while serializing the run list.
2050    #[error("error serializing run list to `{path}`")]
2051    RunListSerialize {
2052        /// The path to the run list file.
2053        path: Utf8PathBuf,
2054
2055        /// The underlying error.
2056        #[source]
2057        error: serde_json::Error,
2058    },
2059
2060    /// An error occurred while serializing rerun info.
2061    #[error("error serializing rerun info")]
2062    RerunInfoSerialize {
2063        /// The underlying error.
2064        #[source]
2065        error: serde_json::Error,
2066    },
2067
2068    /// An error occurred while serializing the test list.
2069    #[error("error serializing test list")]
2070    TestListSerialize {
2071        /// The underlying error.
2072        #[source]
2073        error: serde_json::Error,
2074    },
2075
2076    /// An error occurred while serializing the record options.
2077    #[error("error serializing record options")]
2078    RecordOptionsSerialize {
2079        /// The underlying error.
2080        #[source]
2081        error: serde_json::Error,
2082    },
2083
2084    /// An error occurred while serializing a test event.
2085    #[error("error serializing test event")]
2086    TestEventSerialize {
2087        /// The underlying error.
2088        #[source]
2089        error: serde_json::Error,
2090    },
2091
2092    /// An error occurred while writing the run list.
2093    #[error("error writing run list to `{path}`")]
2094    RunListWrite {
2095        /// The path to the run list file.
2096        path: Utf8PathBuf,
2097
2098        /// The underlying error.
2099        #[source]
2100        error: atomicwrites::Error<std::io::Error>,
2101    },
2102
2103    /// An error occurred while writing to the store.
2104    #[error("error writing to store at `{store_path}`")]
2105    StoreWrite {
2106        /// The path to the store file.
2107        store_path: Utf8PathBuf,
2108
2109        /// The underlying error.
2110        #[source]
2111        error: StoreWriterError,
2112    },
2113
2114    /// An error occurred while creating the run log.
2115    #[error("error creating run log at `{path}`")]
2116    RunLogCreate {
2117        /// The path to the run log file.
2118        path: Utf8PathBuf,
2119
2120        /// The underlying error.
2121        #[source]
2122        error: std::io::Error,
2123    },
2124
2125    /// An error occurred while writing to the run log.
2126    #[error("error writing to run log at `{path}`")]
2127    RunLogWrite {
2128        /// The path to the run log file.
2129        path: Utf8PathBuf,
2130
2131        /// The underlying error.
2132        #[source]
2133        error: std::io::Error,
2134    },
2135
2136    /// An error occurred while flushing the run log.
2137    #[error("error flushing run log at `{path}`")]
2138    RunLogFlush {
2139        /// The path to the run log file.
2140        path: Utf8PathBuf,
2141
2142        /// The underlying error.
2143        #[source]
2144        error: std::io::Error,
2145    },
2146
2147    /// Cannot write to runs.json.zst because it has a newer format version.
2148    #[error(
2149        "cannot write to record store: runs.json.zst format version {file_version} is newer than \
2150         supported version {max_supported_version}"
2151    )]
2152    FormatVersionTooNew {
2153        /// The format version in the file.
2154        file_version: RunsJsonFormatVersion,
2155        /// The maximum version this nextest can write.
2156        max_supported_version: RunsJsonFormatVersion,
2157    },
2158}
2159
2160/// An error that occurred while writing to a zip store.
2161#[derive(Debug, Error)]
2162#[non_exhaustive]
2163pub enum StoreWriterError {
2164    /// An error occurred while creating the store file.
2165    #[error("error creating store")]
2166    Create {
2167        /// The underlying error.
2168        #[source]
2169        error: std::io::Error,
2170    },
2171
2172    /// An error occurred while writing to a file in the store.
2173    #[error("error writing to path `{path}` in store")]
2174    Write {
2175        /// The path within the store.
2176        path: Utf8PathBuf,
2177
2178        /// The underlying error.
2179        #[source]
2180        error: std::io::Error,
2181    },
2182
2183    /// An error occurred while compressing data with a zstd dictionary.
2184    #[error("error compressing data")]
2185    Compress {
2186        /// The underlying error.
2187        #[source]
2188        error: std::io::Error,
2189    },
2190
2191    /// An error occurred while finalizing the store.
2192    #[error("error finalizing store")]
2193    Finish {
2194        /// The underlying error.
2195        #[source]
2196        error: std::io::Error,
2197    },
2198
2199    /// An error occurred while flushing the store.
2200    #[error("error flushing store")]
2201    Flush {
2202        /// The underlying error.
2203        #[source]
2204        error: std::io::Error,
2205    },
2206}
2207
2208/// An error that occurred in the record reporter.
2209#[derive(Debug, Error)]
2210pub enum RecordReporterError {
2211    /// An error occurred while writing to the run store.
2212    #[error(transparent)]
2213    RunStore(RunStoreError),
2214
2215    /// The writer thread panicked.
2216    #[error("record writer thread panicked: {message}")]
2217    WriterPanic {
2218        /// A message extracted from the panic payload, if available.
2219        message: String,
2220    },
2221}
2222
2223/// An error determining the state directory for recordings.
2224#[derive(Debug, Error)]
2225pub enum StateDirError {
2226    /// The platform base strategy could not be determined.
2227    ///
2228    /// This typically means the platform doesn't support standard directory layouts.
2229    #[error("could not determine platform base directory strategy")]
2230    BaseDirStrategy(#[source] HomeDirError),
2231
2232    /// The platform state directory path is not valid UTF-8.
2233    #[error("platform state directory is not valid UTF-8: {path:?}")]
2234    StateDirNotUtf8 {
2235        /// The path that was not valid UTF-8.
2236        path: PathBuf,
2237    },
2238
2239    /// The workspace path could not be canonicalized.
2240    #[error("could not canonicalize workspace path `{workspace_root}`")]
2241    Canonicalize {
2242        /// The workspace root that could not be canonicalized.
2243        workspace_root: Utf8PathBuf,
2244        /// The underlying I/O error.
2245        #[source]
2246        error: std::io::Error,
2247    },
2248}
2249
2250/// An error during recording session setup.
2251#[derive(Debug, Error)]
2252pub enum RecordSetupError {
2253    /// The platform state directory could not be determined.
2254    #[error("could not determine platform state directory for recording")]
2255    StateDirNotFound(#[source] StateDirError),
2256
2257    /// Failed to create the run store.
2258    #[error("failed to create run store")]
2259    StoreCreate(#[source] RunStoreError),
2260
2261    /// Failed to acquire exclusive lock on the run store.
2262    #[error("failed to lock run store")]
2263    StoreLock(#[source] RunStoreError),
2264
2265    /// Failed to create the run recorder.
2266    #[error("failed to create run recorder")]
2267    RecorderCreate(#[source] RunStoreError),
2268}
2269
2270impl RecordSetupError {
2271    /// If this error indicates that recording should be disabled (but the test
2272    /// run should continue), returns the underlying error.
2273    ///
2274    /// This is the case when the runs.json.zst file has a newer format version than
2275    /// this nextest supports. Recording is disabled to avoid data loss, but the
2276    /// test run can proceed normally.
2277    pub fn disabled_error(&self) -> Option<&(dyn std::error::Error + 'static)> {
2278        match self {
2279            RecordSetupError::RecorderCreate(err @ RunStoreError::FormatVersionTooNew { .. }) => {
2280                Some(err)
2281            }
2282            _ => None,
2283        }
2284    }
2285}
2286
2287/// An error that occurred while pruning recorded runs.
2288#[derive(Debug, Error)]
2289pub enum RecordPruneError {
2290    /// An error occurred while deleting a run directory.
2291    #[error("error deleting run `{run_id}` at `{path}`")]
2292    DeleteRun {
2293        /// The run ID that could not be deleted.
2294        run_id: ReportUuid,
2295
2296        /// The path to the run directory.
2297        path: Utf8PathBuf,
2298
2299        /// The underlying error.
2300        #[source]
2301        error: std::io::Error,
2302    },
2303
2304    /// An error occurred while calculating the size of a path.
2305    #[error("error calculating size of `{path}`")]
2306    CalculateSize {
2307        /// The path whose size could not be calculated.
2308        path: Utf8PathBuf,
2309
2310        /// The underlying error.
2311        #[source]
2312        error: std::io::Error,
2313    },
2314
2315    /// An error occurred while deleting an orphaned directory.
2316    #[error("error deleting orphaned directory `{path}`")]
2317    DeleteOrphan {
2318        /// The path to the orphaned directory.
2319        path: Utf8PathBuf,
2320
2321        /// The underlying error.
2322        #[source]
2323        error: std::io::Error,
2324    },
2325
2326    /// An error occurred while reading the runs directory.
2327    #[error("error reading runs directory `{path}`")]
2328    ReadRunsDir {
2329        /// The path to the runs directory.
2330        path: Utf8PathBuf,
2331
2332        /// The underlying error.
2333        #[source]
2334        error: std::io::Error,
2335    },
2336
2337    /// An error occurred while reading a directory entry.
2338    #[error("error reading directory entry in `{dir}`")]
2339    ReadDirEntry {
2340        /// The path to the directory being read.
2341        dir: Utf8PathBuf,
2342
2343        /// The underlying error.
2344        #[source]
2345        error: std::io::Error,
2346    },
2347
2348    /// An error occurred while reading file type.
2349    #[error("error reading file type for `{path}`")]
2350    ReadFileType {
2351        /// The path whose file type could not be read.
2352        path: Utf8PathBuf,
2353
2354        /// The underlying error.
2355        #[source]
2356        error: std::io::Error,
2357    },
2358}
2359
2360/// Error returned when parsing an invalid run ID selector.
2361///
2362/// A valid selector is either "latest" or a string containing only hex digits
2363/// and dashes (for UUID format).
2364#[derive(Clone, Debug, PartialEq, Eq, Error)]
2365#[error("invalid run ID selector `{input}`: expected `latest` or hex digits")]
2366pub struct InvalidRunIdSelector {
2367    /// The invalid input string.
2368    pub input: String,
2369}
2370
2371/// Error returned when parsing a [`RunIdOrRecordingSelector`](crate::record::RunIdOrRecordingSelector) fails.
2372///
2373/// A valid selector is either "latest", a string containing only hex digits
2374/// and dashes (for UUID format), or a file path (ending in `.zip` or
2375/// containing path separators).
2376#[derive(Clone, Debug, PartialEq, Eq, Error)]
2377#[error(
2378    "invalid run ID selector `{input}`: expected `latest`, hex digits, \
2379     or a file path (ending in `.zip` or containing path separators)"
2380)]
2381pub struct InvalidRunIdOrRecordingSelector {
2382    /// The invalid input string.
2383    pub input: String,
2384}
2385
2386/// An error resolving a run ID prefix.
2387#[derive(Debug, Error)]
2388pub enum RunIdResolutionError {
2389    /// No run found matching the prefix.
2390    #[error("no recorded run found matching `{prefix}`")]
2391    NotFound {
2392        /// The prefix that was searched for.
2393        prefix: String,
2394    },
2395
2396    /// Multiple runs match the prefix.
2397    #[error("prefix `{prefix}` is ambiguous, matches {count} runs")]
2398    Ambiguous {
2399        /// The prefix that was searched for.
2400        prefix: String,
2401
2402        /// The number of matching runs.
2403        count: usize,
2404
2405        /// The candidates that matched (up to a limit).
2406        candidates: Vec<RecordedRunInfo>,
2407
2408        /// The run ID index for computing shortest unique prefixes.
2409        run_id_index: RunIdIndex,
2410    },
2411
2412    /// The prefix contains invalid characters.
2413    #[error("prefix `{prefix}` contains invalid characters (expected hexadecimal)")]
2414    InvalidPrefix {
2415        /// The invalid prefix.
2416        prefix: String,
2417    },
2418
2419    /// No recorded runs exist.
2420    #[error("no recorded runs exist")]
2421    NoRuns,
2422}
2423
2424/// An error that occurred while reading a recorded run.
2425#[derive(Debug, Error)]
2426pub enum RecordReadError {
2427    /// The run was not found.
2428    #[error("run not found at `{path}`")]
2429    RunNotFound {
2430        /// The path where the run was expected.
2431        path: Utf8PathBuf,
2432    },
2433
2434    /// Failed to open the archive.
2435    #[error("error opening archive at `{path}`")]
2436    OpenArchive {
2437        /// The path to the archive.
2438        path: Utf8PathBuf,
2439
2440        /// The underlying error.
2441        #[source]
2442        error: std::io::Error,
2443    },
2444
2445    /// Failed to parse the archive (corrupt or truncated).
2446    #[error("error parsing archive at `{path}`")]
2447    ParseArchive {
2448        /// The path to the archive.
2449        path: Utf8PathBuf,
2450
2451        /// The underlying error.
2452        #[source]
2453        error: std::io::Error,
2454    },
2455
2456    /// Failed to read an archive file.
2457    #[error("error reading `{file_name}` from archive")]
2458    ReadArchiveFile {
2459        /// The file name within the archive.
2460        file_name: String,
2461
2462        /// The underlying error.
2463        #[source]
2464        error: std::io::Error,
2465    },
2466
2467    /// Failed to open the run log.
2468    #[error("error opening run log at `{path}`")]
2469    OpenRunLog {
2470        /// The path to the run log.
2471        path: Utf8PathBuf,
2472
2473        /// The underlying error.
2474        #[source]
2475        error: std::io::Error,
2476    },
2477
2478    /// Failed to read a line from the run log.
2479    #[error("error reading line {line_number} from run log")]
2480    ReadRunLog {
2481        /// The line number that failed.
2482        line_number: usize,
2483
2484        /// The underlying error.
2485        #[source]
2486        error: std::io::Error,
2487    },
2488
2489    /// Failed to parse an event from the run log.
2490    #[error("error parsing event at line {line_number}")]
2491    ParseEvent {
2492        /// The line number that failed.
2493        line_number: usize,
2494
2495        /// The underlying error.
2496        #[source]
2497        error: serde_json::Error,
2498    },
2499
2500    /// Required file not found in archive.
2501    #[error("required file `{file_name}` not found in archive")]
2502    FileNotFound {
2503        /// The file name that was not found.
2504        file_name: String,
2505    },
2506
2507    /// Failed to decompress archive data.
2508    #[error("error decompressing data from `{file_name}`")]
2509    Decompress {
2510        /// The file name being decompressed.
2511        file_name: String,
2512
2513        /// The underlying error.
2514        #[source]
2515        error: std::io::Error,
2516    },
2517
2518    /// Unknown output file type in archive.
2519    ///
2520    /// This indicates the archive was created by a newer version of nextest
2521    /// with additional output types that this version doesn't recognize.
2522    #[error(
2523        "unknown output file type `{file_name}` in archive \
2524         (archive may have been created by a newer version of nextest)"
2525    )]
2526    UnknownOutputType {
2527        /// The file name with the unknown type.
2528        file_name: String,
2529    },
2530
2531    /// Archive file exceeds maximum allowed size.
2532    #[error(
2533        "file `{file_name}` in archive exceeds maximum size ({size} bytes, limit is {limit} bytes)"
2534    )]
2535    FileTooLarge {
2536        /// The file name in the archive.
2537        file_name: String,
2538
2539        /// The size of the file.
2540        size: u64,
2541
2542        /// The maximum allowed size.
2543        limit: u64,
2544    },
2545
2546    /// Archive file size doesn't match the header.
2547    ///
2548    /// This indicates a corrupt or tampered archive since nextest controls
2549    /// archive creation.
2550    #[error(
2551        "file `{file_name}` size mismatch: header claims {claimed_size} bytes, \
2552         but read {actual_size} bytes (archive may be corrupt or tampered)"
2553    )]
2554    SizeMismatch {
2555        /// The file name in the archive.
2556        file_name: String,
2557
2558        /// The size claimed in the ZIP header.
2559        claimed_size: u64,
2560
2561        /// The actual size read.
2562        actual_size: u64,
2563    },
2564
2565    /// Failed to deserialize metadata.
2566    #[error("error deserializing `{file_name}`")]
2567    DeserializeMetadata {
2568        /// The file name being deserialized.
2569        file_name: String,
2570
2571        /// The underlying error.
2572        #[source]
2573        error: serde_json::Error,
2574    },
2575
2576    /// Failed to extract a file from the store.
2577    #[error("failed to extract `{store_path}` to `{output_path}`")]
2578    ExtractFile {
2579        /// The path within the store.
2580        store_path: String,
2581
2582        /// The output path where extraction was attempted.
2583        output_path: Utf8PathBuf,
2584
2585        /// The underlying I/O error.
2586        #[source]
2587        error: std::io::Error,
2588    },
2589
2590    /// An error occurred while reading a portable recording.
2591    #[error("error reading portable recording")]
2592    PortableRecording(#[source] PortableRecordingReadError),
2593}
2594
2595/// An error that occurred while creating a portable recording.
2596#[derive(Debug, Error)]
2597#[non_exhaustive]
2598pub enum PortableRecordingError {
2599    /// The run directory does not exist.
2600    #[error("run directory does not exist: {path}")]
2601    RunDirNotFound {
2602        /// The path that was expected to exist.
2603        path: Utf8PathBuf,
2604    },
2605
2606    /// A required file is missing from the run directory.
2607    #[error("required file missing from run directory `{run_dir}`: `{file_name}`")]
2608    RequiredFileMissing {
2609        /// The run directory that was being validated.
2610        run_dir: Utf8PathBuf,
2611        /// The name of the missing file.
2612        file_name: &'static str,
2613    },
2614
2615    /// Failed to serialize the manifest.
2616    #[error("failed to serialize manifest")]
2617    SerializeManifest(#[source] serde_json::Error),
2618
2619    /// Failed to start a file in the zip archive.
2620    #[error("failed to start file {file_name} in archive")]
2621    ZipStartFile {
2622        /// The file that failed to start.
2623        file_name: &'static str,
2624        /// The underlying I/O error.
2625        #[source]
2626        source: std::io::Error,
2627    },
2628
2629    /// Failed to write to the zip archive.
2630    #[error("failed to write {file_name} to archive")]
2631    ZipWrite {
2632        /// The file being written.
2633        file_name: &'static str,
2634        /// The underlying I/O error.
2635        #[source]
2636        source: std::io::Error,
2637    },
2638
2639    /// Failed to read a source file.
2640    #[error("failed to read {file_name}")]
2641    ReadFile {
2642        /// The file being read.
2643        file_name: &'static str,
2644        /// The underlying I/O error.
2645        #[source]
2646        source: std::io::Error,
2647    },
2648
2649    /// Failed to finalize the zip archive.
2650    #[error("failed to finalize archive")]
2651    ZipFinalize(#[source] std::io::Error),
2652
2653    /// Failed to write the archive atomically.
2654    #[error("failed to write archive atomically to {path}")]
2655    AtomicWrite {
2656        /// The destination path.
2657        path: Utf8PathBuf,
2658        /// The underlying error.
2659        #[source]
2660        source: std::io::Error,
2661    },
2662}
2663
2664/// An error that occurred while reading a portable recording.
2665#[derive(Debug, Error)]
2666#[non_exhaustive]
2667pub enum PortableRecordingReadError {
2668    /// Failed to open the archive file.
2669    #[error("failed to open archive at `{path}`")]
2670    OpenArchive {
2671        /// The path to the archive.
2672        path: Utf8PathBuf,
2673        /// The underlying I/O error.
2674        #[source]
2675        error: std::io::Error,
2676    },
2677
2678    /// Failed to read from the archive.
2679    #[error("failed to read archive at `{path}`")]
2680    ReadArchive {
2681        /// The path to the archive.
2682        path: Utf8PathBuf,
2683        /// The underlying I/O error.
2684        #[source]
2685        error: std::io::Error,
2686    },
2687
2688    /// A required file is missing from the archive.
2689    #[error("required file `{file_name}` missing from archive at `{path}`")]
2690    MissingFile {
2691        /// The path to the archive.
2692        path: Utf8PathBuf,
2693        /// The name of the missing file.
2694        file_name: Cow<'static, str>,
2695    },
2696
2697    /// Failed to parse the manifest.
2698    #[error("failed to parse manifest from archive at `{path}`")]
2699    ParseManifest {
2700        /// The path to the archive.
2701        path: Utf8PathBuf,
2702        /// The underlying JSON error.
2703        #[source]
2704        error: serde_json::Error,
2705    },
2706
2707    /// The portable recording format version is not supported.
2708    #[error(
2709        "portable recording format version {found} in `{path}` is incompatible: {incompatibility} \
2710         (this nextest supports version {supported})"
2711    )]
2712    UnsupportedFormatVersion {
2713        /// The path to the archive.
2714        path: Utf8PathBuf,
2715        /// The format version found in the archive.
2716        found: PortableRecordingFormatVersion,
2717        /// The supported format version.
2718        supported: PortableRecordingFormatVersion,
2719        /// The specific incompatibility.
2720        incompatibility: PortableRecordingVersionIncompatibility,
2721    },
2722
2723    /// The store format version is not supported.
2724    #[error(
2725        "store format version {found} in `{path}` is incompatible: {incompatibility} \
2726         (this nextest supports version {supported})"
2727    )]
2728    UnsupportedStoreFormatVersion {
2729        /// The path to the archive.
2730        path: Utf8PathBuf,
2731        /// The store format version found in the archive.
2732        found: StoreFormatVersion,
2733        /// The supported store format version.
2734        supported: StoreFormatVersion,
2735        /// The specific incompatibility.
2736        incompatibility: StoreVersionIncompatibility,
2737    },
2738
2739    /// A file in the archive exceeds the size limit.
2740    #[error(
2741        "file `{file_name}` in archive `{path}` is too large \
2742         ({size} bytes, limit is {limit} bytes)"
2743    )]
2744    FileTooLarge {
2745        /// The path to the archive.
2746        path: Utf8PathBuf,
2747        /// The name of the file.
2748        file_name: Cow<'static, str>,
2749        /// The size of the file.
2750        size: u64,
2751        /// The size limit.
2752        limit: u64,
2753    },
2754
2755    /// Failed to extract a file from the archive.
2756    #[error("failed to extract `{file_name}` from archive `{archive_path}` to `{output_path}`")]
2757    ExtractFile {
2758        /// The path to the archive.
2759        archive_path: Utf8PathBuf,
2760        /// The name of the file being extracted.
2761        file_name: &'static str,
2762        /// The path where extraction was attempted.
2763        output_path: Utf8PathBuf,
2764        /// The underlying I/O error.
2765        #[source]
2766        error: std::io::Error,
2767    },
2768
2769    /// The inner archive is compressed.
2770    ///
2771    /// With portable recordings, the inner archive must be stored uncompressed.
2772    #[error(
2773        "for portable recording `{archive_path}`, the inner archive is stored \
2774         with {:?} compression -- it must be stored uncompressed",
2775        compression
2776    )]
2777    CompressedInnerArchive {
2778        /// The path to the archive.
2779        archive_path: Utf8PathBuf,
2780        /// The compression method used.
2781        compression: CompressionMethod,
2782    },
2783
2784    /// The archive has no manifest and is not a valid wrapper archive.
2785    ///
2786    /// A wrapper archive must contain exactly one `.zip` file.
2787    #[error(
2788        "archive at `{path}` has no manifest and is not a wrapper archive \
2789         (contains {file_count} {}, {zip_count} of which {} in .zip)",
2790        plural::files_str(*file_count),
2791        plural::end_str(*zip_count)
2792    )]
2793    NotAWrapperArchive {
2794        /// The path to the archive.
2795        path: Utf8PathBuf,
2796        /// The total number of files in the archive.
2797        file_count: usize,
2798        /// The number of files ending in `.zip`.
2799        zip_count: usize,
2800    },
2801
2802    /// An unexpected I/O error occurred while probing whether the input is
2803    /// seekable.
2804    ///
2805    /// This is distinct from the expected `ESPIPE`/`ERROR_INVALID_FUNCTION`
2806    /// errors that indicate a pipe or FIFO. Unexpected errors (e.g. `EBADF`,
2807    /// `EIO`) are propagated here rather than falling into the spool path.
2808    #[error("unexpected I/O error while probing seekability of `{path}`")]
2809    SeekProbe {
2810        /// The path to the recording.
2811        path: Utf8PathBuf,
2812        /// The underlying I/O error.
2813        #[source]
2814        error: std::io::Error,
2815    },
2816
2817    /// An I/O error occurred while spooling a non-seekable input to a
2818    /// temporary file.
2819    ///
2820    /// This covers temp file creation, data copying, and seeking back to
2821    /// the start.
2822    #[error("failed to spool non-seekable input `{path}` to a temporary file")]
2823    SpoolTempFile {
2824        /// The path to the recording (e.g. `/proc/self/fd/11`).
2825        path: Utf8PathBuf,
2826        /// The underlying I/O error.
2827        #[source]
2828        error: std::io::Error,
2829    },
2830
2831    /// The input being spooled to a temporary file exceeded the size limit.
2832    #[error(
2833        "recording at `{path}` exceeds the spool size limit \
2834         ({}); use a file path instead of process substitution",
2835        SizeDisplay(.limit.0)
2836    )]
2837    SpoolTooLarge {
2838        /// The path to the recording.
2839        path: Utf8PathBuf,
2840        /// The size limit.
2841        limit: ByteSize,
2842    },
2843}
2844
2845/// An error that occurred while reconstructing a TestList from a summary.
2846///
2847/// Returned by [`TestList::from_summary`](crate::list::TestList::from_summary).
2848#[derive(Debug, Error)]
2849pub enum TestListFromSummaryError {
2850    /// Package not found in the package graph.
2851    #[error("package `{name}` (id: `{package_id}`) not found in cargo metadata")]
2852    PackageNotFound {
2853        /// The package name.
2854        name: String,
2855
2856        /// The package ID that was looked up.
2857        package_id: String,
2858    },
2859
2860    /// Error parsing rust build metadata.
2861    #[error("error parsing rust build metadata")]
2862    RustBuildMeta(#[source] RustBuildMetaParseError),
2863}
2864
2865#[cfg(feature = "self-update")]
2866mod self_update_errors {
2867    use super::*;
2868    use crate::update::PrereleaseKind;
2869    use mukti_metadata::ReleaseStatus;
2870    use semver::{Version, VersionReq};
2871
2872    /// An error that occurs while performing a self-update.
2873    ///
2874    /// Returned by methods in the [`update`](crate::update) module.
2875    #[derive(Debug, Error)]
2876    #[non_exhaustive]
2877    pub enum UpdateError {
2878        /// Failed to read release metadata from a local path on disk.
2879        #[error("failed to read release metadata from `{path}`")]
2880        ReadLocalMetadata {
2881            /// The path that was read.
2882            path: Utf8PathBuf,
2883
2884            /// The error that occurred.
2885            #[source]
2886            error: std::io::Error,
2887        },
2888
2889        /// An error was generated by `self_update`.
2890        #[error("self-update failed")]
2891        SelfUpdate(#[source] self_update::errors::Error),
2892
2893        /// An HTTP error occurred while performing a request.
2894        #[error("error performing HTTP request")]
2895        Http(#[source] ureq::Error),
2896
2897        /// An error occurred while reading an HTTP response body.
2898        #[error("error reading HTTP response body")]
2899        HttpBody(#[source] std::io::Error),
2900
2901        /// The server's Content-Length header was present but could not be
2902        /// parsed.
2903        #[error("Content-Length header present but could not be parsed as an integer: {value:?}")]
2904        ContentLengthInvalid {
2905            /// The raw header value.
2906            value: String,
2907        },
2908
2909        /// The server's Content-Length header didn't match the number of bytes
2910        /// received.
2911        #[error("content length mismatch: expected {expected} bytes, received {actual} bytes")]
2912        ContentLengthMismatch {
2913            /// The expected number of bytes (from the Content-Length header).
2914            expected: u64,
2915            /// The actual number of bytes received.
2916            actual: u64,
2917        },
2918
2919        /// Deserializing release metadata failed.
2920        #[error("deserializing release metadata failed")]
2921        ReleaseMetadataDe(#[source] serde_json::Error),
2922
2923        /// This version was not found.
2924        #[error("version `{version}` not found (known versions: {})", known_versions(.known))]
2925        VersionNotFound {
2926            /// The version that wasn't found.
2927            version: Version,
2928
2929            /// A list of all known versions.
2930            known: Vec<(Version, ReleaseStatus)>,
2931        },
2932
2933        /// No version was found matching a requirement.
2934        #[error("no version found matching requirement `{req}`")]
2935        NoMatchForVersionReq {
2936            /// The version requirement that had no matches.
2937            req: VersionReq,
2938        },
2939
2940        /// No stable (non-prerelease) version was found.
2941        #[error("no stable version found")]
2942        NoStableVersion,
2943
2944        /// No version matching the requested prerelease kind was found.
2945        #[error("no version found matching {} channel", kind.description())]
2946        NoVersionForPrereleaseKind {
2947            /// The kind of prerelease that was requested.
2948            kind: PrereleaseKind,
2949        },
2950
2951        /// The specified mukti project was not found.
2952        #[error("project {not_found} not found in release metadata (known projects: {})", known.join(", "))]
2953        MuktiProjectNotFound {
2954            /// The project that was not found.
2955            not_found: String,
2956
2957            /// Known projects.
2958            known: Vec<String>,
2959        },
2960
2961        /// No release information was found for the given target triple.
2962        #[error(
2963            "for version {version}, no release information found for target `{triple}` \
2964            (known targets: {})",
2965            known_triples.iter().join(", ")
2966        )]
2967        NoTargetData {
2968            /// The version that was fetched.
2969            version: Version,
2970
2971            /// The target triple.
2972            triple: String,
2973
2974            /// The triples that were found.
2975            known_triples: BTreeSet<String>,
2976        },
2977
2978        /// The current executable could not be determined.
2979        #[error("the current executable's path could not be determined")]
2980        CurrentExe(#[source] std::io::Error),
2981
2982        /// A temporary directory could not be created.
2983        #[error("temporary directory could not be created at `{location}`")]
2984        TempDirCreate {
2985            /// The location where the temporary directory could not be created.
2986            location: Utf8PathBuf,
2987
2988            /// The error that occurred.
2989            #[source]
2990            error: std::io::Error,
2991        },
2992
2993        /// The temporary archive could not be created.
2994        #[error("temporary archive could not be created at `{archive_path}`")]
2995        TempArchiveCreate {
2996            /// The archive file that couldn't be created.
2997            archive_path: Utf8PathBuf,
2998
2999            /// The error that occurred.
3000            #[source]
3001            error: std::io::Error,
3002        },
3003
3004        /// An error occurred while writing to a temporary archive.
3005        #[error("error writing to temporary archive at `{archive_path}`")]
3006        TempArchiveWrite {
3007            /// The archive path for which there was an error.
3008            archive_path: Utf8PathBuf,
3009
3010            /// The error that occurred.
3011            #[source]
3012            error: std::io::Error,
3013        },
3014
3015        /// An error occurred while reading from a temporary archive.
3016        #[error("error reading from temporary archive at `{archive_path}`")]
3017        TempArchiveRead {
3018            /// The archive path for which there was an error.
3019            archive_path: Utf8PathBuf,
3020
3021            /// The error that occurred.
3022            #[source]
3023            error: std::io::Error,
3024        },
3025
3026        /// A checksum mismatch occurred. (Currently, the SHA-256 checksum is checked.)
3027        #[error("SHA-256 checksum mismatch: expected: {expected}, actual: {actual}")]
3028        ChecksumMismatch {
3029            /// The expected checksum.
3030            expected: String,
3031
3032            /// The actual checksum.
3033            actual: String,
3034        },
3035
3036        /// An error occurred while renaming a file.
3037        #[error("error renaming `{source}` to `{dest}`")]
3038        FsRename {
3039            /// The rename source.
3040            source: Utf8PathBuf,
3041
3042            /// The rename destination.
3043            dest: Utf8PathBuf,
3044
3045            /// The error that occurred.
3046            #[source]
3047            error: std::io::Error,
3048        },
3049
3050        /// An error occurred while running `cargo nextest self setup`.
3051        #[error("cargo-nextest binary updated, but error running `cargo nextest self setup`")]
3052        SelfSetup(#[source] std::io::Error),
3053    }
3054
3055    fn known_versions(versions: &[(Version, ReleaseStatus)]) -> String {
3056        use std::fmt::Write;
3057
3058        // Take the first few versions here.
3059        const DISPLAY_COUNT: usize = 4;
3060
3061        let display_versions: Vec<_> = versions
3062            .iter()
3063            .filter(|(v, status)| v.pre.is_empty() && *status == ReleaseStatus::Active)
3064            .map(|(v, _)| v.to_string())
3065            .take(DISPLAY_COUNT)
3066            .collect();
3067        let mut display_str = display_versions.join(", ");
3068        if versions.len() > display_versions.len() {
3069            write!(
3070                display_str,
3071                " and {} others",
3072                versions.len() - display_versions.len()
3073            )
3074            .unwrap();
3075        }
3076
3077        display_str
3078    }
3079
3080    /// An error occurred while parsing an [`UpdateVersion`](crate::update::UpdateVersion).
3081    #[derive(Debug, Error)]
3082    pub enum UpdateVersionParseError {
3083        /// The version string is empty.
3084        #[error("version string is empty")]
3085        EmptyString,
3086
3087        /// The input is not a valid version requirement.
3088        #[error(
3089            "`{input}` is not a valid semver requirement\n\
3090                (hint: see https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html for the correct format)"
3091        )]
3092        InvalidVersionReq {
3093            /// The input that was provided.
3094            input: String,
3095
3096            /// The error.
3097            #[source]
3098            error: semver::Error,
3099        },
3100
3101        /// The version is not a valid semver.
3102        #[error("`{input}` is not a valid semver{}", extra_semver_output(.input))]
3103        InvalidVersion {
3104            /// The input that was provided.
3105            input: String,
3106
3107            /// The error.
3108            #[source]
3109            error: semver::Error,
3110        },
3111    }
3112
3113    fn extra_semver_output(input: &str) -> String {
3114        // If it is not a valid version but it is a valid version
3115        // requirement, add a note to the warning
3116        if input.parse::<VersionReq>().is_ok() {
3117            format!(
3118                "\n(if you want to specify a semver range, add an explicit qualifier, like ^{input})"
3119            )
3120        } else {
3121            "".to_owned()
3122        }
3123    }
3124}
3125
3126#[cfg(feature = "self-update")]
3127pub use self_update_errors::*;
3128
3129#[cfg(test)]
3130mod tests {
3131    use super::*;
3132
3133    #[test]
3134    fn display_error_chain() {
3135        let err1 = StringError::new("err1", None);
3136
3137        insta::assert_snapshot!(format!("{}", DisplayErrorChain::new(&err1)), @"err1");
3138
3139        let err2 = StringError::new("err2", Some(err1));
3140        let err3 = StringError::new("err3\nerr3 line 2", Some(err2));
3141
3142        insta::assert_snapshot!(format!("{}", DisplayErrorChain::new(&err3)), @"
3143        err3
3144        err3 line 2
3145          caused by:
3146          - err2
3147          - err1
3148        ");
3149    }
3150
3151    #[test]
3152    fn display_error_list() {
3153        let err1 = StringError::new("err1", None);
3154
3155        let error_list =
3156            ErrorList::<StringError>::new("waiting on the water to boil", vec![err1.clone()])
3157                .expect(">= 1 error");
3158        insta::assert_snapshot!(format!("{}", error_list), @"err1");
3159        insta::assert_snapshot!(format!("{}", DisplayErrorChain::new(&error_list)), @"err1");
3160
3161        let err2 = StringError::new("err2", Some(err1));
3162        let err3 = StringError::new("err3", Some(err2));
3163
3164        let error_list =
3165            ErrorList::<StringError>::new("waiting on flowers to bloom", vec![err3.clone()])
3166                .expect(">= 1 error");
3167        insta::assert_snapshot!(format!("{}", error_list), @"err3");
3168        insta::assert_snapshot!(format!("{}", DisplayErrorChain::new(&error_list)), @"
3169        err3
3170          caused by:
3171          - err2
3172          - err1
3173        ");
3174
3175        let err4 = StringError::new("err4", None);
3176        let err5 = StringError::new("err5", Some(err4));
3177        let err6 = StringError::new("err6\nerr6 line 2", Some(err5));
3178
3179        let error_list = ErrorList::<StringError>::new(
3180            "waiting for the heat death of the universe",
3181            vec![err3, err6],
3182        )
3183        .expect(">= 1 error");
3184
3185        insta::assert_snapshot!(format!("{}", error_list), @"
3186        2 errors occurred waiting for the heat death of the universe:
3187        * err3
3188            caused by:
3189            - err2
3190            - err1
3191        * err6
3192          err6 line 2
3193            caused by:
3194            - err5
3195            - err4
3196        ");
3197        insta::assert_snapshot!(format!("{}", DisplayErrorChain::new(&error_list)), @"
3198        2 errors occurred waiting for the heat death of the universe:
3199        * err3
3200            caused by:
3201            - err2
3202            - err1
3203        * err6
3204          err6 line 2
3205            caused by:
3206            - err5
3207            - err4
3208        ");
3209    }
3210
3211    #[derive(Clone, Debug, Error)]
3212    struct StringError {
3213        message: String,
3214        #[source]
3215        source: Option<Box<StringError>>,
3216    }
3217
3218    impl StringError {
3219        fn new(message: impl Into<String>, source: Option<StringError>) -> Self {
3220            Self {
3221                message: message.into(),
3222                source: source.map(Box::new),
3223            }
3224        }
3225    }
3226
3227    impl fmt::Display for StringError {
3228        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3229            write!(f, "{}", self.message)
3230        }
3231    }
3232}