{"id":"4d571bcf1d0ce156","repo":"rust-lang/cargo","slug":"artifact-dir-was-not-locked-during-clean","errorCode":null,"errorMessage":"artifact-dir was not locked during clean","messagePattern":"artifact-dir was not locked during clean","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_clean.rs","lineNumber":297,"sourceCode":"            // Remove the uplifted copy.\n            for target in pkg.targets() {\n                if target.is_custom_build() {\n                    continue;\n                }\n                let crate_name: Rc<str> = target.crate_name().into();\n                for &mode in &[\n                    CompileMode::Build,\n                    CompileMode::Test,\n                    CompileMode::Check { test: false },\n                ] {\n                    for (compile_kind, layout) in &layouts {\n                        let triple = target_data.short_name(compile_kind);\n                        let (file_types, _unsupported) = target_data\n                            .info(*compile_kind)\n                            .rustc_outputs(mode, target.kind(), triple, clean_ctx.gctx)?;\n                        let artifact_dir = layout\n                            .artifact_dir()\n                            .expect(\"artifact-dir was not locked during clean\");\n                        let uplift_dir = match target.kind() {\n                            TargetKind::ExampleBin | TargetKind::ExampleLib(..) => {\n                                Some(artifact_dir.examples())\n                            }\n                            // Tests/benchmarks are never uplifted.\n                            TargetKind::Test | TargetKind::Bench => None,\n                            _ => Some(artifact_dir.dest()),\n                        };\n                        if let Some(uplift_dir) = uplift_dir {\n                            for file_type in file_types {\n                                let uplifted_filename = file_type.uplift_filename(target);\n\n                                // Dep-info generated by Cargo itself.\n                                let dep_info = Path::new(&uplifted_filename)\n                                    .with_extension(\"d\")\n                                    .to_string_lossy()\n                                    .into_owned();\n","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_clean.rs#L279-L315","documentation":"This panic fires during cargo clean when computing the artifact output directory for uplifted files. The code calls layout.artifact_dir().expect(\"artifact-dir was not locked during clean\"). The artifact_dir() method returns Option<&ArtifactDirLayout> — it is None when the artifact-dir layout was not initialized. During a clean of artifact-producing targets, the layout must have an artifact-dir.","triggerScenarios":"Running cargo clean on a target directory where the build layout was initialized without an artifact-dir (e.g., the build used a legacy layout or the artifact-dir feature was not engaged), but the clean code path iterates targets that expect artifact output locations.","commonSituations":"Cleaning a target directory left over from an older cargo version that used a different layout; running cargo clean --target with a configuration that doesn't set up artifact-dir; target directory manually modified or partially deleted leaving inconsistent layout state.","solutions":["Run cargo clean without --target or other flags to do a full clean.","Delete the target directory manually (rm -rf target/) and rebuild from scratch.","Update cargo — older target directories may use layouts incompatible with newer clean logic.","If using CARGO_TARGET_DIR or artifact-dir config, ensure the configuration is consistent across builds."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"// Before cargo clean, verify target dir layout is consistent\nlet target_dir = std::path::Path::new(\"target\");\nif !target_dir.exists() {\n    println!(\"target directory does not exist; nothing to clean\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run cargo clean without extra flags for a full clean.","If cargo clean panics, manually remove the target directory: rm -rf target/.","Keep CARGO_TARGET_DIR and artifact-dir configuration consistent across builds.","Avoid mixing cargo versions against the same target directory."],"tags":["rust","cargo","panic","invariant","cargo-clean","build-layout","artifact-dir"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}