{"id":"9a76eae6444adc95","repo":"rust-lang/cargo","slug":"env-var-was-not-array","errorCode":null,"errorMessage":"env var was not array","messagePattern":"env var was not array","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/context/mod.rs","lineNumber":1114,"sourceCode":"            // Keep existing config if higher priority than env (e.g., --config CLI),\n            // otherwise clear for env\n            if output\n                .first()\n                .map(|o| o.definition() > &env_def)\n                .unwrap_or_default()\n            {\n                return Ok(());\n            } else {\n                output.clear();\n            }\n        }\n\n        if self.cli_unstable().advanced_env && env_val.starts_with('[') && env_val.ends_with(']') {\n            // Parse an environment string as a TOML array.\n            let toml_v = env_val.parse::<toml::Value>().map_err(|e| {\n                ConfigError::new(format!(\"could not parse TOML list: {}\", e), env_def.clone())\n            })?;\n            let values = toml_v.as_array().expect(\"env var was not array\");\n            for value in values {\n                // Until we figure out how to deal with it through `-Zadvanced-env`,\n                // complex array types are unsupported.\n                let s = value.as_str().ok_or_else(|| {\n                    ConfigError::new(\n                        format!(\"expected string, found {}\", value.type_str()),\n                        env_def.clone(),\n                    )\n                })?;\n                output.push(CV::String(s.to_string(), env_def.clone()))\n            }\n        } else {\n            output.extend(\n                env_val\n                    .split_whitespace()\n                    .map(|s| CV::String(s.to_string(), env_def.clone())),\n            );\n        }","sourceCodeStart":1096,"sourceCodeEnd":1132,"githubUrl":"https://github.com/rust-lang/cargo/blob/0e07a155371a6ce88ae53a2c00df940280c09a67/src/context/mod.rs#L1096-L1132","documentation":"This panic fires inside merge_env_in_list() when parsing an environment variable as a TOML array under the -Z advanced-env unstable feature. The code checks env_val.starts_with('[') && env_val.ends_with(']') and then parses it with toml::Value::parse. It expects the result to be an array, but a TOML value like [table] or [{...}] could parse successfully as a non-array (inline table) despite the bracket check.","triggerScenarios":"Setting a CARGO_ environment variable (with -Zadvanced-env enabled) to a value that starts with [ and ends with ] but parses as a TOML inline table rather than an array — e.g., CARGO_SOMELIST = \"{ key = 'val' }\" would fail the bracket check, but edge cases in TOML parsing could produce a non-array Value.","commonSituations":"Using -Zadvanced-env to pass list-valued config via environment variables and accidentally providing a value that TOML interprets as a table or other non-array type; cargo version mismatch where the TOML parser accepts constructs the expect was not designed for.","solutions":["Ensure the environment variable value is a valid TOML array of strings, e.g. CARGO_REGISTRY_CREDENTIAL_PROVIDER=\"['cargo:token-from-stdin']\".","Disable -Zadvanced-env if not needed and use --config or config files instead.","Validate the env var with a TOML parser externally before invoking cargo."],"exampleFix":"// before\nlet values = toml_v.as_array().expect(\"env var was not array\");\n// after\nlet toml_v = env_val.parse::<toml::Value>().map_err(|e| {\n    ConfigError::new(format!(\"could not parse TOML list: {}\", e), env_def.clone())\n})?;\nlet values = toml_v.as_array().ok_or_else(|| {\n    ConfigError::new(format!(\"expected array, found {}\", toml_v.type_str()), env_def.clone())\n})?;","handlingStrategy":"validation","validationCode":"// Validate env var is a TOML array before passing to cargo\nfn validate_env_array(var_name: &str) -> Result<(), String> {\n    if let Ok(val) = std::env::var(var_name) {\n        if val.starts_with('[') && val.ends_with(']') {\n            let parsed: toml::Value = val.parse().map_err(|e| format!(\"{}: {}\", var_name, e))?;\n            if !parsed.is_array() {\n                return Err(format!(\"{}: expected TOML array, got {}\", var_name, parsed.type_str()));\n            }\n        }\n    }\n    Ok(())\n}","typeGuard":"fn is_valid_toml_array(val: &str) -> bool {\n    if !(val.starts_with('[') && val.ends_with(']')) { return false; }\n    val.parse::<toml::Value>().ok().map_or(false, |v| v.is_array())\n}","tryCatchPattern":null,"preventionTips":["Always use proper TOML array syntax for CARGO_ env vars with -Zadvanced-env: [\"item1\", \"item2\"].","Avoid -Zadvanced-env in production; prefer config files or --config CLI flags.","Pre-validate env vars with an external TOML parser before invoking cargo."],"tags":["rust","cargo","panic","invariant","env-vars","unstable","toml"],"analyzedSha":"0e07a155371a6ce88ae53a2c00df940280c09a67","analyzedAt":"2026-08-06T01:46:58.334Z","schemaVersion":2}