{"record":{"id":"f04107df767e6eac","repo":"JuliusBrussee/caveman","slug":"optional-profile-credentials-cannot-be-array-eleme","errorCode":null,"errorMessage":"optional profile credentials cannot be array elements","messagePattern":"optional profile credentials cannot be array elements","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/index.ts","lineNumber":5819,"sourceCode":"\n// renderDeep applies renderTemplate to every string leaf of a JSON value — used to\n// render an agent's inline-config template before it is stringified into an env var.\n// Optional credential references disappear as whole object properties when their\n// source variable is unavailable. Secrets never enter generated JSON: the retained\n// value is the agent-native `$OPENAI_API_KEY` reference, not its expansion.\nfunction renderDeep(v: unknown, gw = gatewayURL(), env: NodeJS.ProcessEnv = process.env, options: RenderDeepOptions = {}): unknown {\n  if (v === OPTIONAL_OPENAI_KEY_ENV_TEMPLATE) {\n    const key = env.OPENAI_API_KEY;\n    const inherited = typeof key === \"string\" && !!key.trim() && !/[\\r\\n]/.test(key);\n    return (options.optionalOpenAIKeyEnvAvailable ?? inherited)\n      ? options.optionalOpenAIKeyReference ?? \"$OPENAI_API_KEY\"\n      : undefined;\n  }\n  if (typeof v === \"string\") return renderTemplate(v, gw);\n  if (Array.isArray(v)) {\n    return v.map((item) => {\n      const rendered = renderDeep(item, gw, env, options);\n      if (rendered === undefined) throw new Error(\"optional profile credentials cannot be array elements\");\n      return rendered;\n    });\n  }\n  if (v && typeof v === \"object\") {\n    const out: Record<string, unknown> = {};\n    for (const [k, val] of Object.entries(v as Record<string, unknown>)) {\n      const rendered = renderDeep(val, gw, env, options);\n      if (rendered !== undefined) out[k] = rendered;\n    }\n    return out;\n  }\n  return v;\n}\n\nfunction stripJson5Comments(s: string): string {\n  let out = \"\";\n  let inString = false;\n  let escaped = false;","sourceCodeStart":5801,"sourceCodeEnd":5837,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/cli/src/index.ts#L5801-L5837","documentation":"When rendering optional profile credentials, arrays are expanded element-by-element: each item is rendered and must itself resolve to a defined value. If any element renders to undefined (i.e. an optional credential was absent), the renderer refuses the whole array rather than embedding a null/undefined hole in the credentials structure.","triggerScenarios":"renderDeep encounters an Array value in the profile credentials tree, and at least one element renders to undefined — typically an element that is itself an optional/env-backed credential template whose referenced env var or gateway value is missing, so it legitimately resolves to undefined but sits inside an array where omission is not allowed.","commonSituations":"An env-templated credential like \"${MY_TOKEN}\" placed inside an array in the profile config while MY_TOKEN is unset; copying a profile template that uses optional values in list position; refactor moving a scalar optional credential into an array (e.g. header lists) without noticing optionality breaks arrays.","solutions":["Move optional credentials out of arrays — make the whole array optional or provide a concrete value for every element","Set the env var the array element's template references so it renders to a defined value","Use a default/fallback in the template (e.g. ${VAR:-default}) so the element never renders undefined","Replace the array with an object keyed structure where each optional field can be omitted independently","If the element is genuinely optional, drop empty elements before rendering instead of relying on the renderer"],"exampleFix":"// before\n{ \"headers\": [\"Bearer ${OPTIONAL_TOKEN}\"] }  // OPTIONAL_TOKEN unset\n// after\n{ \"header\": \"Bearer ${OPTIONAL_TOKEN:-none}\" }  // or provide a concrete value","handlingStrategy":"validation","validationCode":"function assertNoOptionalInArray(profile: Record<string, unknown>, env: NodeJS.ProcessEnv) {\n  for (const [k, v] of Object.entries(profile)) {\n    if (Array.isArray(v)) {\n      for (const item of v) {\n        const m = typeof item === 'string' ? item.match(/\\$\\{([A-Z0-9_]+)\\}/) : null;\n        if (m && !(m[1] in env)) throw new Error(`${k} array element needs env ${m[1]}`);\n      }\n    }\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  cli.renderProfile(profile);\n} catch (err) {\n  if ((err as Error).message === 'optional profile credentials cannot be array elements') {\n    console.error('An array element rendered to undefined — set its env var or move it out of the array.');\n  } else throw err;\n}","preventionTips":["Never place env-templated optional values in array positions","Use ${VAR:-default} fallbacks for templates that may be unset","Prefer object-shaped credentials so optional fields can be omitted","Validate profiles with a schema that bans optional list elements"],"tags":["config","credentials","template-rendering","optionality"],"backgroundTag":"invalid-config-value","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}