{"id":"9820f31c9076391e","repo":"rust-lang/cargo","slug":"has-custom-build-should-have-runcustombuild","errorCode":null,"errorMessage":"has_custom_build should have RunCustomBuild","messagePattern":"has_custom_build should have RunCustomBuild","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/compiler/custom_build.rs","lineNumber":1320,"sourceCode":"            if let Some(links) = unit.pkg.manifest().links() {\n                if let Some(output) = unit.links_overrides.get(links) {\n                    let metadata = build_runner.get_run_build_script_metadata(unit);\n                    build_runner.build_script_outputs.lock().unwrap().insert(\n                        unit.pkg.package_id(),\n                        metadata,\n                        output.clone(),\n                    );\n                }\n            }\n        }\n\n        let mut ret = BuildScripts::default();\n\n        // If a package has a build script, add itself as something to inspect for linking.\n        if !unit.target.is_custom_build() && unit.pkg.has_custom_build() {\n            let script_metas = build_runner\n                .find_build_script_metadatas(unit)\n                .expect(\"has_custom_build should have RunCustomBuild\");\n            for script_meta in script_metas {\n                add_to_link(&mut ret, unit.pkg.package_id(), script_meta);\n            }\n        }\n\n        if unit.mode.is_run_custom_build() {\n            parse_previous_explicit_deps(build_runner, unit);\n        }\n\n        // We want to invoke the compiler deterministically to be cache-friendly\n        // to rustc invocation caching schemes, so be sure to generate the same\n        // set of build script dependency orderings via sorting the targets that\n        // come out of the `Context`.\n        let mut dependencies: Vec<Unit> = build_runner\n            .unit_deps(unit)\n            .iter()\n            .map(|d| d.unit.clone())\n            .collect();","sourceCodeStart":1302,"sourceCodeEnd":1338,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/compiler/custom_build.rs#L1302-L1338","documentation":"When a package has a custom build script (`unit.pkg.has_custom_build()`), Cargo fetches its link metadata via `find_build_script_metadatas(unit).expect(\"has_custom_build should have RunCustomBuild\")`. The expect encodes the invariant that a package flagged as having a build script always has a corresponding `RunCustomBuild` unit whose metadata can be located. Failure means `has_custom_build()` and the unit/metadata bookkeeping disagree.","triggerScenarios":"A package whose `Cargo.toml` declares `build = ...` (so `has_custom_build` is true) but whose unit graph lacks the `RunCustomBuild` unit, or whose `find_build_script_metadatas` returns `Err`; inconsistent state between `Package` metadata and the compiled build-script outputs.","commonSituations":"Interrupted build leaving `target/` with stale metadata; Cargo version mismatch between who wrote `target/` and who is reading it; metabuild/`build.rs` rename without `cargo clean`.","solutions":["`cargo clean` and rebuild to regenerate build-script metadata.","Ensure only one Cargo version operates on a given `target/` dir.","If reproducible, report upstream with the minimal `Cargo.toml` that has a `build.rs`."],"exampleFix":"// before\nlet script_metas = build_runner\n    .find_build_script_metadatas(unit)\n    .expect(\"has_custom_build should have RunCustomBuild\");\n// after\nlet script_metas = build_runner\n    .find_build_script_metadatas(unit)\n    .with_context(|| format!(\"package `{}` flagged has_custom_build but its RunCustomBuild metadata is missing\", unit.pkg.package_id()))?;","handlingStrategy":"validation","validationCode":"// (cargo-internal) before reading link metadata, confirm a RunCustomBuild unit exists for the pkg:\n// assert!(build_runner.unit_graph.iter().any(|(u, _)| u.pkg == unit.pkg && u.mode.is_run_custom_build()));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["`cargo clean` to regenerate build-script metadata.","Keep one Cargo version per target dir.","Run `cargo clean` after renaming/removing a `build.rs`."],"tags":["cargo","build-scripts","metadata","unit-graph","internal-invariant"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}