altimate-code icon indicating copy to clipboard operation
altimate-code copied to clipboard

execDbtShow returns malformed result when dbt emits truthy non-array data.preview

Open sahrizvi opened this issue 5 months ago • 1 comments

Symptom

execDbtShow in packages/dbt-tools/src/dbt-cli.ts returns { data: <non-array> } to callers whenever dbt emits a data.preview value that is truthy but neither a string nor an array (e.g. data.preview = {} or data.preview = 42). Downstream callers that then do result.data.map(...) or read result.data.length crash with TypeError outside the tested path.

Root cause

The preview-handling block in Tier 1 assumes the else branch is always an array:

let rows: Record<string, unknown>[]
if (typeof preview === "string") {
  const parsed = safeJsonParse(preview)
  if (Array.isArray(parsed)) {
    rows = parsed
  } else {
    rows = []
  }
} else {
  rows = preview // assumes array, but `preview` is only known to be truthy
}

The previewLine match upstream picks rows via l.data?.preview / l.data?.rows / l.result?.preview / l.result?.rows. If any of those is truthy but non-array (object, number, etc., in a future dbt version), it flows through to rows unchecked.

Expected

The non-string branch should explicitly check Array.isArray(preview) before assigning, falling through to Tier 2 / Tier 3 otherwise.

Suggested fix

} else if (Array.isArray(preview)) {
  rows = preview
} else {
  rows = []
}

Add a regression test covering data.preview = {} (and arguably = 42, = null).

History

This predates the execDbtShow error-bubbling work in #932 / #933. Filing separately so it gets its own focused review.

sahrizvi avatar Jun 15 '26 14:06 sahrizvi

Fixed by 32ffe32af in #933. The Tier 1 preview block in execDbtShow now guards the non-string branch with Array.isArray(preview) and emits empty rows for any unexpected shape (object, number, null) instead of assigning the non-array value into rows and returning malformed { data: <non-array> } to callers. Two regression tests added (preview = {} and preview = 42).

sahrizvi avatar Jun 15 '26 16:06 sahrizvi