{"record":{"id":"421508a71c6e4b78","repo":"clockworklabs/SpacetimeDB","slug":"message","errorCode":null,"errorMessage":"{message}","messagePattern":"\\{message\\}","errorType":"exception","errorClass":"NodesError","httpStatus":null,"severity":"error","filePath":"crates/core/src/host/instance_env.rs","lineNumber":335,"sourceCode":"            return Err(DBError::Other(anyhow::anyhow!(\n                \"environment access requires a host-dispatched root module function\"\n            ))\n            .into());\n        }\n        if let Ok(mut tx) = self.get_tx() {\n            tx.record_table_scan(&self.func_type, ST_ENV_ID);\n            return self.read_declared_environment(&*tx, key);\n        }\n        if !matches!(self.func_type, FuncCallType::Procedure) {\n            return Err(NodesError::NotInTransaction);\n        }\n        self.relational_db()\n            .with_read_only(Workload::Internal, |tx| self.read_declared_environment(tx, key))\n    }\n\n    fn read_declared_environment(&self, state: &impl StateView, key: &str) -> Result<Option<String>, NodesError> {\n        use spacetimedb_datastore::system_tables::{StModuleFields, ST_MODULE_ID};\n        let fail = |message| NodesError::from(DBError::Other(anyhow::anyhow!(\"{message}\")));\n        let (hash, module) = self\n            .environment_module\n            .as_ref()\n            .ok_or_else(|| fail(\"environment schema is not available\"))?;\n        let declaration = module\n            .environment()\n            .get(key)\n            .ok_or_else(|| fail(\"environment key is not declared\"))?;\n        // Check inside this same snapshot. A suspended old procedure must never\n        // combine its declarations with values installed for a different module.\n        let row = state\n            .iter(ST_MODULE_ID)\n            .map_err(DBError::from)?\n            .next()\n            .ok_or_else(|| fail(\"database program is not initialized\"))?;\n        let current_hash = spacetimedb_datastore::system_tables::read_hash_from_col(row, StModuleFields::ProgramHash)\n            .map_err(DBError::from)?;\n        if current_hash != *hash {","sourceCodeStart":317,"sourceCodeEnd":353,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/eddf9f5014579a50d4b67630e28b6e15cad9c4af/crates/core/src/host/instance_env.rs#L317-L353","documentation":"read_declared_environment resolves a configuration key against the environment schema declared by the current environment module. It builds failures as NodesError::from(DBError::Other(anyhow!(message))) so the message text itself is the error. It fails when no environment module is attached, the key has no declaration, or the underlying database read fails.","triggerScenarios":"Called by env_get after the transactional path fails: the instance has no environment_module (module not published with an environment), the requested key is absent from the module's declared environment, or the st_env read in a read-only transaction returns an error.","commonSituations":"Querying a key that was never declared via publishing; reading env values before the environment schema is available (module still initializing); typos in the key name.","solutions":["Publish the database with an environment so the environment module/schema is attached.","Check the exact key spelling against the declared environment; add the key via a publish.","If 'environment schema is not available', wait for module initialization to complete and retry."],"exampleFix":"// before: reading undeclared key\nlet v = env_get(\"APY_KEY\");\n\n// after: declared key published via module\nlet v = env_get(\"API_KEY\").expect(\"API_KEY declared at publish time\");","handlingStrategy":"try-catch","validationCode":"// ensure key declared before reading\nlet declared = module.environment().map(|e| e.contains_key(key)).unwrap_or(false);","typeGuard":null,"tryCatchPattern":"match read_declared_environment(tx, key) { Ok(v) => v, Err(e) => { log::warn!(\"env read failed: {e}\"); None } }","preventionTips":["Declare all keys at publish time","Spell keys consistently (constant list shared by publisher and reader)","Handle missing keys with defaults"],"tags":["wasm","module","environment","database"],"backgroundTag":"record-not-found","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"}