{"record":{"id":"5a191ed9dc42dfbf","repo":"rust-lang/cargo","slug":"target-must-support-bin","errorCode":null,"errorMessage":"target must support `bin`","messagePattern":"target must support `bin`","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src/compiler/build_runner/compilation_files.rs","lineNumber":430,"sourceCode":"    /// This should only to be used when a `Unit` is not available.\n    pub fn bin_link_for_target(\n        &self,\n        target: &Target,\n        kind: CompileKind,\n        bcx: &BuildContext<'_, '_>,\n    ) -> CargoResult<Option<PathBuf>> {\n        assert!(target.is_bin());\n        let Some(dest) = self.layout(kind).artifact_dir().map(|v| v.dest()) else {\n            return Ok(None);\n        };\n        let info = bcx.target_data.info(kind);\n        let (file_types, _) = info\n            .rustc_outputs(\n                CompileMode::Build,\n                &TargetKind::Bin,\n                bcx.target_data.short_name(&kind),\n            )\n            .expect(\"target must support `bin`\");\n\n        let file_type = file_types\n            .iter()\n            .find(|file_type| file_type.flavor == FileFlavor::Normal)\n            .expect(\"target must support `bin`\");\n\n        Ok(Some(dest.join(file_type.uplift_filename(target))))\n    }\n\n    /// Returns the filenames that the given unit will generate.\n    ///\n    /// Note: It is not guaranteed that all of the files will be generated.\n    pub(super) fn outputs(\n        &self,\n        unit: &Unit,\n        bcx: &BuildContext<'a, 'gctx>,\n    ) -> CargoResult<Arc<Vec<OutputFile>>> {\n        self.outputs[unit]","sourceCodeStart":412,"sourceCodeEnd":448,"githubUrl":"https://github.com/rust-lang/cargo/blob/42eee92bc9aa4449f4e25d84e4ceb1cf8e0f87f9/src/compiler/build_runner/compilation_files.rs#L412-L448","documentation":"This is an internal Cargo assertion (a panic, not a returned error) inside `CompilationFiles::bin_link_for_target`, which computes the uplifted output path for a binary target when no Unit is available. Line 424 unwraps the Result of `TargetInfo::rustc_outputs(CompileMode::Build, &TargetKind::Bin, triple)`, which shells out to rustc to learn what file type rustc emits for a `bin` crate. The panic means Cargo could not successfully query (or parse the output of) rustc for the bin file type of the active compile target — an invariant Cargo assumes always holds for bin targets.","triggerScenarios":"Calling `cargo build`/`cargo run` (or the library API `bin_link_for_target`) where the rustc subprocess used to discover bin output file types fails or returns unparseable output: a broken or half-installed rustup toolchain, a rustc/cargo version mismatch, or a custom `--target my-spec.json` whose spec makes `rustc --print file-names --crate-type bin` fail. A sibling expect (no FileFlavor::Normal entry) covers the 'bin unsupported by target' case; line 424 specifically fires on the hard Err path (rustc query failure).","commonSituations":"Corrupt or mid-update rustup installations; swapping rustc versions while a build daemon/IDE keeps old target-info caches; custom bare-metal/wasm target JSON files that omit fields rustc's file-names query needs; mixing cargo from one toolchain with rustc from another (e.g., cargo-as-a-library embedding); rarely, a genuine Cargo regression where a non-bin-capable kind reaches this code path.","solutions":["Verify the toolchain is intact: `rustup update` (or reinstall the toolchain), then retry `cargo build` in a trivial bin crate on the same triple to isolate the failure.","If using a custom target spec, confirm rustc itself can answer the bin query: `rustc --print file-names --crate-type bin --target my-target.json empty.rs` — if that errors, fix the JSON spec or use a target whose spec supports bins.","Ensure cargo and rustc come from the same toolchain (`rustup which cargo`, `rustup which rustc`) and that no stale `.fingerprint`/target-info cache from an older rustc is being reused — `cargo clean` or `rm -rf target` removes it.","If the panic reproduces on a healthy toolchain with a stock target, it is a Cargo bug: report it to https://github.com/rust-lang/cargo/issues with the repro, cargo/rustc versions (`cargo version --verbose`), and full backtrace (`RUST_BACKTRACE=1 cargo build`)."],"exampleFix":"# before: panic on a custom target\nrustup run mytoolchain cargo build --target boards/my-target.json\n#   -> `target must support `bin``\n\n# after: prove rustc can report bin outputs for this spec, then build\nrustc --print file-names --crate-type bin --target boards/my-target.json /dev/null\ncargo build --target boards/my-target.json","handlingStrategy":"validation","validationCode":"# Before building, prove rustc can discover bin outputs for the exact toolchain+triple:\nrustc --print file-names --crate-type bin --target \"$TRIPLE\" empty.rs \\\n  || echo \"ERROR: rustc cannot report bin file types for $TRIPLE — fix toolchain/spec first\"\n# And confirm cargo/rustc come from the same toolchain:\n[ \"$(dirname \"$(rustup which cargo)\")\" = \"$(dirname \"$(rustup which rustc)\")\" ] \\\n  || echo \"ERROR: cargo and rustc are from different toolchains\"","typeGuard":"// For callers embedding cargo as a library: narrow before asking for bin paths.\nfn as_bin_target<'a>(target: &'a cargo::core::Target) -> Option<&'a cargo::core::Target> {\n    target.is_bin().then_some(target)\n}\n// Only pass targets that survived the guard to bin_link_for_target.","tryCatchPattern":null,"preventionTips":["Pin one toolchain per workspace with rust-toolchain.toml so cargo and rustc never diverge.","Never swap rustc versions (rustup update) while builds or IDE build daemons are running; restart the IDE/rust-analyzer afterwards.","Validate custom target JSON specs with `rustc --print file-names --crate-type bin --target spec.json` before wiring them into CI.","After toolchain changes, `cargo clean` (or delete target/) to purge caches built by the previous rustc.","For cargo-as-a-library code, treat internal `.expect()`s as bugs to report — capture `RUST_BACKTRACE=1` output when they fire."],"tags":["rust","cargo","panic","internal-assertion","rustc","bin-target","custom-target"],"backgroundTag":"internal-assertion-panic","analyzedSha":"42eee92bc9aa4449f4e25d84e4ceb1cf8e0f87f9","analyzedAt":"2026-08-21T19:15:00.589Z","contentChangedAt":"2026-08-21T19:15:00.589Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}