pnpm/pnpm · error

marker environment serializes to an object

Error message

marker environment serializes to an object

What it means

`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.

Solutions

  1. Confirm the value being serialized is a MarkerEnvironment struct, not a wrapper or scalar
  2. Check for recent changes to the marker-environment crate's Serialize impl
  3. Use serde_json::to_value and match on Value::Object with a descriptive error instead of expect

Example fix

// before
.as_object()
.expect("marker environment serializes to an object")
// after
.as_object()
.ok_or_else(|| anyhow!("marker environment did not serialize to a JSON object"))?
Defensive patterns

Strategy: type-guard

Validate before calling

let env_value = serde_json::to_value(&target.environment)?;
if !env_value.is_object() {
    return Err("marker environment must serialize to a JSON object".into());
}

Type guard

fn as_object(v: &serde_json::Value) -> Option<&serde_json::Map<String, serde_json::Value>> { v.as_object() }

Try / catch

let obj = env.as_object().ok_or_else(|| anyhow!("marker environment not an object"))?;

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of pnpm/pnpm@49dbe22c32 (2026-09-15). Data as JSON: /api/errors/2c4cefe502162892. Report an issue: GitHub.

Appendix: source

Thrown at pnpm/crates/python-resolver/src/lockfile.rs:152

    /// marker variable a requirement in the solved graph reads. Those are
    /// the parts of the target the solution can depend on, so a PEP 751
    /// installer refuses the lockfile where they differ and nowhere else.
    /// The version is the minor one, unless a locked package's
    /// `Requires-Python` tells patch releases of it apart.
    pub fn new(
        packages: &Packages,
        target: &Target,
        requirements: &[Requirement],
        solution: BTreeMap<PackageName, Version>,
        inputs: Inputs,
        requires_python: Option<String>,
    ) -> Result<Self> {
        let referenced =
            referenced_marker_keys(packages, requirements, &solution, &target.environment)?;
        let environment = serde_json::to_value(&target.environment).into_diagnostic()?;
        let marker = environment
            .as_object()
            .expect("marker environment serializes to an object")
            .iter()
            .filter(|(key, _)| referenced.contains(key.as_str()))
            .map(|(key, value)| {
                let value = value.as_str().expect("marker environment values are strings");
                if value.contains(['\'', '"', '\n', '\r']) {
                    bail!(
                        "Python environment value cannot be represented as a lockfile marker: {key}",
                    );
                }
                Ok(format!("{key} == '{value}'"))
            })
            .collect::<Result<Vec<_>>>()?
            .join(" and ");
        Ok(Self {
            lock_version: "1.0".to_string(),
            created_by: "pnpm".to_string(),
            environments: vec![marker],
            requires_python,

View on GitHub (pinned to 49dbe22c32)