{"record":{"id":"c7739cc10ba10ded","repo":"clockworklabs/SpacetimeDB","slug":"environment-key-count-exceeds-limit","errorCode":null,"errorMessage":"Environment key count exceeds limit","messagePattern":"Environment key count exceeds limit","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/cli/src/subcommands/env.rs","lineNumber":136,"sourceCode":"\nfn render(body: &[u8], query: &Query) -> anyhow::Result<String> {\n    // Validate the requested projection before rendering any response values.\n    let results: Vec<spacetimedb_client_api_messages::http::SqlStmtResult<Vec<String>>> =\n        serde_json::from_slice(body).map_err(|_| anyhow::anyhow!(\"Invalid environment read response\"))?;\n    ensure!(results.len() == 1, \"Invalid environment read result count\");\n    let result = &results[0];\n    let expected: &[&str] = match query {\n        Query::List => &[\"key\", \"value\"],\n        Query::Get(_) => &[\"value\"],\n    };\n    ensure!(\n        result.schema.elements.len() == expected.len()\n            && result.schema.elements.iter().zip(expected).all(|(column, name)| {\n                column.name.as_deref() == Some(*name) && column.algebraic_type == spacetimedb_lib::AlgebraicType::String\n            }),\n        \"Invalid environment read projection\"\n    );\n    ensure!(result.rows.len() <= MAX_ENV_VARS, \"Environment key count exceeds limit\");\n    for row in &result.rows {\n        ensure!(row.len() == expected.len(), \"Invalid environment read row\");\n        if matches!(query, Query::List) {\n            validate_key(&row[0]).context(\"Invalid environment key in response\")?;\n        }\n        ensure!(\n            row.last().unwrap().len() <= MAX_ENV_VALUE_BYTES,\n            \"Environment read value exceeds limit\"\n        );\n    }\n    match query {\n        Query::List => {\n            let mut rows: Vec<_> = result.rows.iter().collect();\n            rows.sort_unstable_by(|a, b| a[0].cmp(&b[0]));\n            let rows = rows.into_iter().map(|row| {\n                Ok::<_, std::convert::Infallible>(spacetimedb_lib::sats::product![row[0].as_str(), row[1].as_str()])\n            });\n            let table = sql::build_table(","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/eddf9f5014579a50d4b67630e28b6e15cad9c4af/crates/cli/src/subcommands/env.rs#L118-L154","documentation":"Thrown by `render` when the environment read returns more rows than `MAX_ENV_VARS` (spacetimedb_lib::environment::MAX_ENV_VARS). The CLI caps how many environment variables it will process/display, so an over-large result is rejected as inconsistent with server-side limits.","triggerScenarios":"`result.rows.len() > MAX_ENV_VARS` — the environment table contains more keys than the enforced maximum, typically because an older server or direct write path allowed exceeding the cap, or a misconfigured environment accumulated too many variables.","commonSituations":"Inherited environments with many variables set before the limit existed; bulk scripts writing hundreds of env vars via older CLI versions; `spacetime env list` on a long-lived environment.","solutions":["Remove unneeded variables with `spacetime env unset KEY` until the count is within the limit.","Upgrade the server so it enforces the same MAX_ENV_VARS cap on writes.","Audit the environment's variables and consolidate related values into fewer keys."],"exampleFix":"// before: 200 keys set, MAX_ENV_VARS = 100\nspacetime env list   # Environment key count exceeds limit\n// after\nspacetime env unset STALE_KEY_1\nspacetime env unset STALE_KEY_2   # bring count under the limit","handlingStrategy":"validation","validationCode":"// cap env var writes client-side\nif var_count > spacetimedb_lib::environment::MAX_ENV_VARS {\n    return Err(\"too many environment variables\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Track how many env vars an environment holds; prune stale ones.","Enforce the MAX_ENV_VARS cap in automation that sets vars.","Upgrade servers so they enforce the same limit."],"tags":["cli","spacetimedb","limits","environment"],"backgroundTag":"value-out-of-range","analyzedSha":"eddf9f5014579a50d4b67630e28b6e15cad9c4af","analyzedAt":"2026-09-20T12:15:59.611Z","contentChangedAt":"2026-09-20T12:15:59.611Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}