{"record":{"id":"f796aad19c7c0b63","repo":"cross-rs/cross","slug":"should-contain-at-least-one","errorCode":null,"errorMessage":"should contain at least one","messagePattern":"should contain at least one","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"src/docker/image.rs","lineNumber":48,"sourceCode":"    pub reference: ImageReference,\n    // The toolchain triple the image is built for\n    pub toolchain: Vec<ImagePlatform>,\n}\n\nimpl PossibleImage {\n    pub fn to_definite_with(&self, engine: &Engine, msg_info: &mut MessageInfo) -> Result<Image> {\n        let ImageReference::Name(name) = self.reference.clone() else {\n            eyre::bail!(\"cannot make definite Image from unqualified PossibleImage\");\n        };\n\n        if self.toolchain.is_empty() {\n            Ok(Image {\n                name,\n                platform: ImagePlatform::DEFAULT,\n            })\n        } else {\n            let platform = if self.toolchain.len() == 1 {\n                self.toolchain.first().expect(\"should contain at least one\")\n            } else {\n                let same_arch = self\n                    .toolchain\n                    .iter()\n                    .filter(|platform| {\n                        &platform.architecture\n                            == engine.arch.as_ref().unwrap_or(&Architecture::Amd64)\n                    })\n                    .collect::<Vec<_>>();\n\n                if same_arch.len() == 1 {\n                    // pick the platform with the same architecture\n                    same_arch.first().expect(\"should contain one element\")\n                } else if let Some(platform) = same_arch\n                    .iter()\n                    .find(|platform| &platform.os == engine.os.as_ref().unwrap_or(&Os::Linux))\n                {\n                    *platform","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/cross-rs/cross/blob/8c1a8aa4b661711f4b7b6ac07c2e8929ce2f7d27/src/docker/image.rs#L30-L66","documentation":"`Image::to_definite_with` (src/docker/image.rs:48) picks the image platform from `self.toolchain`. When the toolchain list has exactly one entry, it takes it with `first().expect(\"should contain at least one\")`. The expect encodes the invariant that a len==1 slice always has a first element; it is a defensive assertion, not a user-facing validation, and is unreachable while the length check is present.","triggerScenarios":"Only fires if the `self.toolchain.len() == 1` guard is removed/changed or the collection mutates between the check and `first()`. Normal execution: single-toolchain image resolution takes this branch; multi-toolchain images go through the same_arch filtering path.","commonSituations":"Users never see this panic directly; it appears during maintenance/refactoring. Confusion arises when image platform selection behaves unexpectedly — that is governed by the len check and the arch-filtering branch, not the expect.","solutions":["Treat as an internal invariant; report as a bug if it ever triggers","If refactoring, replace the len+expect pattern with `if let [platform] = self.toolchain.as_slice()` or `only = self.toolchain.first()?` with explicit error handling","If you hit platform-selection issues, inspect the toolchain image list rather than this expect"],"exampleFix":"// before\nlet platform = if self.toolchain.len() == 1 {\n    self.toolchain.first().expect(\"should contain at least one\")\n} else { ... };\n// after\nlet platform = match self.toolchain.as_slice() {\n    [only] => only,\n    _ => /* multi-platform arch filtering */ ...,\n};","handlingStrategy":"validation","validationCode":"// inspect resolved toolchain images before definite-image resolution\nassert!(!image.toolchain.is_empty(), \"toolchain image list must be non-empty\");","typeGuard":"fn single<T>(v: &[T]) -> Option<&T> { if v.len() == 1 { v.first() } else { None } }","tryCatchPattern":null,"preventionTips":["Keep the len==1 guard immediately before any first()/expect so the invariant remains obvious in review","Prefer slice patterns (`if let [only] = slice`) over len-check + expect when refactoring"],"tags":["docker","unwrap-expect","platform-selection"],"backgroundTag":"internal-invariant-violation","analyzedSha":"8c1a8aa4b661711f4b7b6ac07c2e8929ce2f7d27","analyzedAt":"2026-09-13T15:10:43.988Z","contentChangedAt":"2026-09-13T15:10:43.988Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}