{"record":{"id":"8dc110220810de81","repo":"clockworklabs/SpacetimeDB","slug":"invalid-environment-read-result-count","errorCode":null,"errorMessage":"Invalid environment read result count","messagePattern":"Invalid environment read result count","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/cli/src/subcommands/env.rs","lineNumber":123,"sourceCode":"    let mut body = Vec::new();\n    let limit = MAX_ENV_VARS * (MAX_ENV_KEY_BYTES + MAX_ENV_VALUE_BYTES) * 6 + 64 * 1024;\n    let mut stream = response.bytes_stream();\n    while let Some(chunk) = stream.next().await {\n        let chunk = chunk?;\n        ensure!(\n            body.len().saturating_add(chunk.len()) <= limit,\n            \"Environment read response exceeds limit\"\n        );\n        body.extend_from_slice(&chunk);\n    }\n    render(&body, &query)\n}\n\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        }","sourceCodeStart":105,"sourceCodeEnd":141,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/eddf9f5014579a50d4b67630e28b6e15cad9c4af/crates/cli/src/subcommands/env.rs#L105-L141","documentation":"Thrown by `render` when the parsed JSON body is valid `SqlStmtResult` JSON but the top-level array does not contain exactly one statement result. The environment read always issues a single SQL statement, so any other count indicates a protocol mismatch.","triggerScenarios":"`serde_json` succeeds but `results.len() != 1` — e.g. an empty array from a server that dropped the statement, or multiple results from an incompatible/newer server API.","commonSituations":"Running a CLI built against a different protocol version than the server; custom proxies splitting statements; server bug returning zero results for the env query.","solutions":["Align CLI and server versions so the SQL response protocol matches.","Retry the command to rule out a transient server-side glitch.","If you control a proxy, ensure it passes the SQL response through unmodified."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":"fn is_single_result(r: &[SqlStmtResult<Vec<String>>]) -> bool { r.len() == 1 }","tryCatchPattern":null,"preventionTips":["Match CLI and server protocol versions.","Pass SQL responses through proxies unmodified.","Retry on transient server errors that could yield empty results."],"tags":["cli","spacetimedb","response-parsing","protocol"],"backgroundTag":"unexpected-response-shape","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"}