dbt-labs/dbt-core · error · minijinja::Error (InvalidOperation)

'TARGET_PACKAGE_NAME' should be set. Missing in configured v

Error message

'TARGET_PACKAGE_NAME' should be set. Missing in configured var context while looking up var: {var_name}

What it means

`ConfiguredVar::package_name` resolves the current package name from the Jinja state via a `TARGET_PACKAGE_NAME` lookup to scope `var()` resolution. This error is thrown when that key is absent from the configured var context while a var lookup is in progress, so the var cannot be attributed to a package. It signals that the var evaluation context was not fully populated by the caller.

Source

Thrown at crates/dbt-jinja-vars/src/configured_var.rs:56

    /// Returns a copy whose package namespace does not depend on a Jinja context key.
    pub fn bind_package(&self, package_name: impl Into<String>) -> Self {
        Self {
            package_name: Some(package_name.into()),
            ..self.clone()
        }
    }

    fn package_name(&self, state: &State<'_, '_>, var_name: &str) -> Result<String, Error> {
        self.package_name
            .clone()
            .or_else(|| {
                state
                    .lookup(TARGET_PACKAGE_NAME, &[])
                    .and_then(|value| value.as_str().map(str::to_string))
            })
            .ok_or_else(|| {
                Error::new(
                    ErrorKind::InvalidOperation,
                    format!(
                        "'TARGET_PACKAGE_NAME' should be set. Missing in configured var context while looking up var: {var_name}"
                    ),
                )
            })
    }
}

impl VarFunction for ConfiguredVar {
    fn contains_var(&self, state: &State<'_, '_>, var_name: &str) -> Result<bool, Error> {
        let package_name = self.package_name(state, var_name)?;
        let vars_lookup = self.vars.get(&package_name).ok_or_else(|| {
            Error::new(
                ErrorKind::InvalidOperation,
                format!("Package vars should be initialized for package: {package_name}"),
            )
        })?;

View on GitHub (pinned to 0267ce9170)

Solutions

  1. Ensure the evaluation context sets `TARGET_PACKAGE_NAME` (the package the var lookup belongs to) before calling var().
  2. If evaluating during dbt_project.yml parsing, use the code path that supplies a default or the special `dbt_project.yml` package name.
  3. Check upstream code that constructs the var context and add the TARGET_PACKAGE_NAME binding.

Example fix

// before
let ctx = Context::new(); // TARGET_PACKAGE_NAME missing
// after
let ctx = Context::from([("TARGET_PACKAGE_NAME", Value::from("my_package"))]);
Defensive patterns

Strategy: validation

Validate before calling

// Rust caller pre-check before var evaluation
fn context_has_package_name(ctx: &dyn StringValue) -> bool { /* state.lookup("TARGET_PACKAGE_NAME").is_some() */ }

Try / catch

match configured.contains_var(state, var_name) {
    Err(e) if e.to_string().contains("TARGET_PACKAGE_NAME") => {
        // rebuild context with TARGET_PACKAGE_NAME set, then retry once
    }
    other => other,
}

Prevention

When it happens

Trigger: Calling `var('my_var')` (or `contains_var`/`call_as_function`, which both route through `package_name`) during a phase where the context lacks TARGET_PACKAGE_NAME — e.g. parsing dbt_project.yml, custom `var` evaluation outside a node context, or a partially built state.

Common situations: Evaluating vars in a bare Minijinja state during tests or tools; invoking var() during dbt_project.yml parsing without a package-scoped context; custom integrations that build the context without setting TARGET_PACKAGE_NAME.

Understand the failure class

Background: "missing required config value" errors: why libraries refuse to start when a configuration key is empty, unset, or blank — this error's family across 48 libraries.

Related errors


AI-assisted analysis of dbt-labs/dbt-core@0267ce9170 (2026-09-07). Data as JSON: /api/errors/7f745431a6d4abac. Report an issue: GitHub.