{"record":{"id":"9d04f803ff2b3274","repo":"dbt-labs/dbt-core","slug":"package-vars-should-be-initialized-for-package-p","errorCode":null,"errorMessage":"Package vars should be initialized for package: {package_name}","messagePattern":"Package vars should be initialized for package: (.+?)","errorType":"exception","errorClass":"minijinja::Error (InvalidOperation)","httpStatus":null,"severity":"error","filePath":"crates/dbt-jinja-vars/src/configured_var.rs","lineNumber":70,"sourceCode":"                    .lookup(TARGET_PACKAGE_NAME, &[])\n                    .and_then(|value| value.as_str().map(str::to_string))\n            })\n            .ok_or_else(|| {\n                Error::new(\n                    ErrorKind::InvalidOperation,\n                    format!(\n                        \"'TARGET_PACKAGE_NAME' should be set. Missing in configured var context while looking up var: {var_name}\"\n                    ),\n                )\n            })\n    }\n}\n\nimpl VarFunction for ConfiguredVar {\n    fn contains_var(&self, state: &State<'_, '_>, var_name: &str) -> Result<bool, Error> {\n        let package_name = self.package_name(state, var_name)?;\n        let vars_lookup = self.vars.get(&package_name).ok_or_else(|| {\n            Error::new(\n                ErrorKind::InvalidOperation,\n                format!(\"Package vars should be initialized for package: {package_name}\"),\n            )\n        })?;\n        Ok(vars_lookup.contains_key(var_name))\n    }\n\n    fn call_as_function(\n        &self,\n        state: &State<'_, '_>,\n        var_name: String,\n        default_value: Option<Value>,\n    ) -> Result<Value, Error> {\n        // 1. CLI vars\n        if let Some(value) = self.cli_vars.get(&var_name) {\n            return Ok(cli_var_value_to_minijinja(value));\n        }\n        // 2. Check if this is dbt_project.yml parsing","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/dbt-labs/dbt-core/blob/0267ce9170576975b76b64ce856b2e5848e96617/crates/dbt-jinja-vars/src/configured_var.rs#L52-L88","documentation":"`ConfiguredVar::contains_var` resolves the package name and then requires an entry in the pre-initialized `vars` map for that package. This error is thrown when no package-vars map was initialized for the resolved package, meaning the caller never populated namespace vars before checking `var` existence. It's an initialization-contract error rather than a user-data problem.","triggerScenarios":"Calling `contains_var(state, var_name)` (backing `var(...) is none` style checks) when `ConfiguredVar.vars` has no entry for the package resolved from TARGET_PACKAGE_NAME — e.g. a package with no vars ever registered.","commonSituations":"Cross-package var checks where the dependency package never had vars initialized; tooling that constructs ConfiguredVar with only the root package's vars; parse-time checks before vars collection completes.","solutions":["Initialize the vars map for every package (including empty maps) before evaluating templates that call var()/contains_var().","Verify the package name resolved from TARGET_PACKAGE_NAME matches a package you actually initialized vars for.","Check the vars-collection phase completed before the template evaluation."],"exampleFix":"// before\nlet vars = HashMap::from([(\"root_pkg\".to_string(), root_vars)]);\n// after\nlet vars = HashMap::from([\n    (\"root_pkg\".to_string(), root_vars),\n    (\"dep_pkg\".to_string(), dep_vars), // ensure every package has an entry (possibly empty)\n]);\n","handlingStrategy":"validation","validationCode":"// pre-check before evaluation\nfn all_packages_initialized(vars: &HashMap<String, HashMap<String, Value>>, pkgs: &[String]) -> bool {\n    pkgs.iter().all(|p| vars.contains_key(p))\n}","typeGuard":null,"tryCatchPattern":"match result {\n    Err(e) if e.to_string().contains(\"Package vars should be initialized\") => {\n        // initialize vars map for the missing package and re-evaluate\n    }\n    other => other,\n}","preventionTips":["Insert an (possibly empty) vars entry for every package during context construction.","Ensure var collection completes before template evaluation.","Validate that package names from TARGET_PACKAGE_NAME match registered keys."],"tags":["vars","initialization","jinja"],"backgroundTag":"internal-invariant-violation","analyzedSha":"0267ce9170576975b76b64ce856b2e5848e96617","analyzedAt":"2026-09-07T21:53:39.732Z","contentChangedAt":"2026-09-07T21:53:39.732Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}