{"record":{"id":"2c4cefe502162892","repo":"pnpm/pnpm","slug":"marker-environment-serializes-to-an-object","errorCode":null,"errorMessage":"marker environment serializes to an object","messagePattern":"marker environment serializes to an object","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pnpm/crates/python-resolver/src/lockfile.rs","lineNumber":152,"sourceCode":"    /// marker variable a requirement in the solved graph reads. Those are\n    /// the parts of the target the solution can depend on, so a PEP 751\n    /// installer refuses the lockfile where they differ and nowhere else.\n    /// The version is the minor one, unless a locked package's\n    /// `Requires-Python` tells patch releases of it apart.\n    pub fn new(\n        packages: &Packages,\n        target: &Target,\n        requirements: &[Requirement],\n        solution: BTreeMap<PackageName, Version>,\n        inputs: Inputs,\n        requires_python: Option<String>,\n    ) -> Result<Self> {\n        let referenced =\n            referenced_marker_keys(packages, requirements, &solution, &target.environment)?;\n        let environment = serde_json::to_value(&target.environment).into_diagnostic()?;\n        let marker = environment\n            .as_object()\n            .expect(\"marker environment serializes to an object\")\n            .iter()\n            .filter(|(key, _)| referenced.contains(key.as_str()))\n            .map(|(key, value)| {\n                let value = value.as_str().expect(\"marker environment values are strings\");\n                if value.contains(['\\'', '\"', '\\n', '\\r']) {\n                    bail!(\n                        \"Python environment value cannot be represented as a lockfile marker: {key}\",\n                    );\n                }\n                Ok(format!(\"{key} == '{value}'\"))\n            })\n            .collect::<Result<Vec<_>>>()?\n            .join(\" and \");\n        Ok(Self {\n            lock_version: \"1.0\".to_string(),\n            created_by: \"pnpm\".to_string(),\n            environments: vec![marker],\n            requires_python,","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/pnpm/pnpm/blob/49dbe22c32011a09be9e7a4315c380a0415a944f/pnpm/crates/python-resolver/src/lockfile.rs#L134-L170","documentation":"`lockfile::new` serializes the target marker environment (`MarkerEnvironment`) to a `serde_json::Value` and then calls `.as_object().expect(\"marker environment serializes to an object\")` to filter its keys. The `expect` encodes the invariant that a struct serializing field-by-field always yields a JSON object. The panic means the serialized environment came back as an array, string, or other non-object value - only possible if the environment type's Serialize impl changed fundamentally.","triggerScenarios":"Replacing `MarkerEnvironment` with a type whose Serialize output is not a JSON map (e.g. a newtype wrapping an array or string); serializing a scalar by mistake; a dependency upgrade that changes the environment struct to a transparent wrapper.","commonSituations":"Refactoring the lockfile writer to share serialization code with another environment representation; pinning mismatched versions of the pep440/marker crates where the struct definition and its serialization diverge.","solutions":["Confirm the value being serialized is a MarkerEnvironment struct, not a wrapper or scalar","Check for recent changes to the marker-environment crate's Serialize impl","Use serde_json::to_value and match on Value::Object with a descriptive error instead of expect"],"exampleFix":"// before\n.as_object()\n.expect(\"marker environment serializes to an object\")\n// after\n.as_object()\n.ok_or_else(|| anyhow!(\"marker environment did not serialize to a JSON object\"))?","handlingStrategy":"type-guard","validationCode":"let env_value = serde_json::to_value(&target.environment)?;\nif !env_value.is_object() {\n    return Err(\"marker environment must serialize to a JSON object\".into());\n}","typeGuard":"fn as_object(v: &serde_json::Value) -> Option<&serde_json::Map<String, serde_json::Value>> { v.as_object() }","tryCatchPattern":"let obj = env.as_object().ok_or_else(|| anyhow!(\"marker environment not an object\"))?;","preventionTips":["Keep MarkerEnvironment a plain struct of string fields","Round-trip serialize/deserialize MarkerEnvironment in unit tests after crate upgrades","Avoid transparent newtype wrappers around the environment type"],"tags":["rust","serialization","lockfile","markers","python"],"backgroundTag":"internal-invariant-violation","analyzedSha":"49dbe22c32011a09be9e7a4315c380a0415a944f","analyzedAt":"2026-09-15T20:33:00.202Z","contentChangedAt":"2026-09-15T20:33:00.202Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}