{"id":"0a7015570d368904","repo":"rust-lang/cargo","slug":"package-is-a-table","errorCode":null,"errorMessage":"package is a table","messagePattern":"package is a table","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/ops/cargo_new.rs","lineNumber":972,"sourceCode":"    Ok(())\n}\n\n// Update the manifest with the inherited workspace package keys.\n// If the option is not set, the key is removed from the manifest.\n// If the option is set, keep the value from the manifest.\nfn update_manifest_with_inherited_workspace_package_keys(\n    opts: &MkOptions<'_>,\n    manifest: &mut toml_edit::DocumentMut,\n    workspace_package_keys: &toml_edit::Table,\n) {\n    if workspace_package_keys.is_empty() {\n        return;\n    }\n\n    let try_remove_and_inherit_package_key = |key: &str, manifest: &mut toml_edit::DocumentMut| {\n        let package = manifest[\"package\"]\n            .as_table_mut()\n            .expect(\"package is a table\");\n        package.remove(key);\n        let mut table = toml_edit::Table::new();\n        table.set_dotted(true);\n        table[\"workspace\"] = toml_edit::value(true);\n        package.insert(key, toml_edit::Item::Table(table));\n    };\n\n    // Inherit keys from the workspace.\n    // Only keep the value from the manifest if the option is set.\n    for (key, _) in workspace_package_keys {\n        if key == \"edition\" && opts.edition.is_some() {\n            continue;\n        }\n        if key == \"publish\" && opts.registry.is_some() {\n            continue;\n        }\n\n        try_remove_and_inherit_package_key(key, manifest);","sourceCodeStart":954,"sourceCodeEnd":990,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/ops/cargo_new.rs#L954-L990","documentation":"Invariant in `update_manifest_with_inherited_workspace_package_keys`. It takes `manifest[\"package\"].as_table_mut().expect(\"package is a table\")`. The manifest being edited was just produced by `cargo new`, which always writes a `[package]` table, so the downcast should always succeed.","triggerScenarios":"Reachable only if the freshly-created `Cargo.toml`'s top-level `package` key is not a table — e.g. a malformed template, a `toml_edit` bug, or a code path that mutates `manifest[\"package\"]` to a scalar before this function runs.","commonSituations":"Custom cargo builds that alter the package section; corrupt `cargo new` templates; extremely rare in released cargo. Real users invoking `cargo new` in a workspace never see this.","solutions":["Report a cargo bug if reproduced with stock `cargo new` inside a workspace that has `[workspace.package]` keys.","Inspect the generated `Cargo.toml` before the crash — confirm `[package]` is present and tabular.","If patching cargo, guard with `bail!(\"expected [package] table\")` instead of `expect` to surface a real error."],"exampleFix":"// before\nlet package = manifest[\"package\"].as_table_mut().expect(\"package is a table\");\n\n// after\nlet package = manifest[\"package\"].as_table_mut()\n    .ok_or_else(|| anyhow::anyhow!(\"[package] is missing or not a table in generated manifest\"))?;","handlingStrategy":"validation","validationCode":"// Validate the generated manifest has a [package] table before workspace inheritance.\nif manifest.get(\"package\").and_then(|p| p.as_table()).is_none() {\n    return Err(anyhow!(\"generated manifest is missing a [package] table\"));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure `cargo new` runs in a directory with a reachable workspace root only when intended.","Avoid tooling that mutates `[package]` between creation and workspace-key inheritance."],"tags":["cargo","panic","invariant","cargo-new","workspace","toml-edit","rust"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}