{"record":{"id":"90279bb162504de3","repo":"clockworklabs/SpacetimeDB","slug":"view-evaluation-failed-during-environment-publication","errorCode":null,"errorMessage":"view evaluation failed during environment publication","messagePattern":"view evaluation failed during environment publication","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/core/src/host/wasm_common/module_host_actor.rs","lineNumber":849,"sourceCode":"        let tx = db.begin_mut_tx(IsolationLevel::Serializable, Workload::Internal);\n        let (tx, _) = db.with_auto_rollback(tx, |tx| -> anyhow::Result<()> {\n            use spacetimedb_datastore::system_tables::{read_hash_from_col, StModuleFields, ST_MODULE_ID};\n            let row = tx\n                .iter(ST_MODULE_ID)?\n                .next()\n                .context(\"database program is not initialized\")?;\n            anyhow::ensure!(\n                read_hash_from_col(row, StModuleFields::ProgramHash)? == self.info.module_hash,\n                \"database program changed before publication\"\n            );\n            crate::db::environment::replace(db, tx, self.info.module_def.environment(), &environment)?;\n            Ok(())\n        })?;\n        let (out, _, trapped) = self.evaluate_subscribed_views(tx, inst)?;\n        if trapped || out.outcome != ViewOutcome::Success {\n            let (_, metrics, reducer) = db.rollback_mut_tx(out.tx);\n            db.report_mut_tx_metrics(reducer, metrics, None);\n            return Ok(UpdateDatabaseResult::ErrorExecutingMigration(anyhow::anyhow!(\n                \"view evaluation failed during environment publication\"\n            )));\n        }\n        let event = ModuleEvent {\n            timestamp: Timestamp::now(),\n            caller_identity: self.info.owner_identity,\n            caller_connection_id: None,\n            function_call: ModuleFunctionCall::update(),\n            status: EventStatus::Committed(DatabaseUpdate::default()),\n            reducer_return_value: None,\n            execution_budget_used: out.execution_budget_used,\n            host_execution_duration: out.total_duration,\n            request_id: None,\n            timer: None,\n        };\n        let durable_offset = db.durable_tx_offset();\n        let CommitAndBroadcastEventSuccess { tx_offset, .. } =\n            commit_and_broadcast_event(&self.info.subscriptions, None, event, out.tx);","sourceCodeStart":831,"sourceCodeEnd":867,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/eddf9f5014579a50d4b67630e28b6e15cad9c4af/crates/core/src/host/wasm_common/module_host_actor.rs#L831-L867","documentation":"After applying an environment update and its transactional changes, the actor runs evaluate_subscribed_views on the updated instance. If a view traps or its outcome is not Success, the transaction is rolled back and the caller receives UpdateDatabaseResult::ErrorExecutingMigration with this message — the environment change itself was fine, but the module's subscribed views could not be evaluated against it, so publication is treated as a failed migration.","triggerScenarios":"Publishing a database/environment update where at least one subscribed view traps during re-evaluation (panic, error, or non-Success ViewOutcome) after the environment rows were replaced.","commonSituations":"A view reads an environment key that was removed or renamed in the new environment; a buggy view reducer that panics on the new data; views incompatible with the newly published module.","solutions":["Inspect server logs to find which view trapped and fix its logic in the module.","Ensure views only reference environment keys that still exist after the update.","Fix the module, then republish; the failed transaction was rolled back so state is unchanged."],"exampleFix":"// before: view reads a removed env key\nlet key = ctx.env_get(\"OLD_KEY\").unwrap();\n\n// after: tolerate missing keys\nlet key = ctx.env_get(\"OLD_KEY\").unwrap_or_default();","handlingStrategy":"try-catch","validationCode":"// preflight: ensure every view-referenced env key exists in the new environment\nfor key in view_env_keys { assert!(new_environment.contains_key(key), \"missing env key {key}\"); }","typeGuard":null,"tryCatchPattern":"match result { UpdateDatabaseResult::ErrorExecutingMigration(e) if e.to_string().contains(\"view evaluation failed\") => inspect_and_fix_views(), _ => {} }","preventionTips":["Write defensive views (no unwrap, default missing env keys)","Test view evaluation with the new environment before publishing","Keep views tolerant to env key changes"],"tags":["views","publish","migration","environment"],"backgroundTag":"invalid-state-transition","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"}