{"record":{"id":"cd6ef3ee56d97d46","repo":"mastra-ai/mastra","slug":"environment-variable-keys-must-be-unique","errorCode":null,"errorMessage":"Environment variable keys must be unique","messagePattern":"Environment variable keys must be unique","errorType":"validation","errorClass":"DuplicateEnvironmentVariableKeyError","httpStatus":null,"severity":"error","filePath":"packages/playground-ui/src/lib/env-file/env-file.ts","lineNumber":54,"sourceCode":"      duplicates.add(key);\n    } else {\n      seen.add(key);\n    }\n  }\n\n  return duplicates;\n}\n\nexport function collectEnvironmentVariables(rows: readonly EnvironmentVariableEntry[]): Record<string, string> {\n  const envVars: Record<string, string> = {};\n  const seen = new Set<string>();\n\n  for (const row of rows) {\n    const key = row.key.trim();\n    if (!key) continue;\n\n    if (seen.has(key)) {\n      throw new DuplicateEnvironmentVariableKeyError(key);\n    }\n\n    seen.add(key);\n    envVars[key] = row.value;\n  }\n\n  return envVars;\n}\n\n/**\n * Parse text in `.env` file format into key-value entries.\n *\n * Handles:\n * - `KEY=value`\n * - Blank lines and comment lines (`#`)\n * - Double-quoted and single-quoted values\n * - Multi-line quoted values, such as private keys\n * - Values containing `=`","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/playground-ui/src/lib/env-file/env-file.ts#L36-L72","documentation":"collectEnvironmentVariables converts editor rows of key/value pairs into an env-var record. Before assigning a value it validates that each trimmed, non-empty key is unique; a duplicate key would silently overwrite a previous value, so the library throws DuplicateEnvironmentVariableKeyError to force the user to fix the form instead.","triggerScenarios":"Calling collectEnvironmentVariables (via useCustomEnvironmentVariablesEditor or the envVars flow) with a rows array containing two or more rows whose trimmed key strings are identical, e.g. rows [{key:'API_KEY',...},{key:'API_KEY',...}] or keys differing only by surrounding whitespace.","commonSituations":"Users typing the same variable twice in the environment editor, pasting a .env file where a key is defined in two sections, or importing rows where trailing whitespace makes duplicates hard to see in the UI.","solutions":["Find the duplicate key reported by the error and remove or rename one of the rows in the editor.","Deduplicate rows before calling collectEnvironmentVariables if you build rows programmatically (keep the last entry if override semantics are desired).","Trim/normalize keys when constructing rows so accidental whitespace variants do not create near-duplicates.","Add UI-level duplicate detection (disable save / highlight the offending row) to catch it before submission."],"exampleFix":"// before\nconst rows = [{ key: ' API_KEY ', value: 'a' }, { key: 'API_KEY', value: 'b' }];\nconst vars = collectEnvironmentVariables(rows); // throws\n\n// after\nconst rows = [{ key: 'API_KEY', value: 'b' }]; // single, deduplicated entry\nconst vars = collectEnvironmentVariables(rows);","handlingStrategy":"validation","validationCode":"const keys = rows.map(r => r.key.trim()).filter(Boolean);\nconst dupes = keys.filter((k, i) => keys.indexOf(k) !== i);\nif (dupes.length) throw new Error(`Duplicate env keys: ${[...new Set(dupes)].join(', ')}`);","typeGuard":"function hasUniqueKeys(rows: { key: string }[]): boolean {\n  const keys = rows.map(r => r.key.trim()).filter(Boolean);\n  return new Set(keys).size === keys.length;\n}","tryCatchPattern":"try {\n  const vars = collectEnvironmentVariables(rows);\n} catch (e) {\n  if (e instanceof DuplicateEnvironmentVariableKeyError) {\n    highlightRowWithKey(e.key); // surface duplicate to the user\n  } else throw e;\n}","preventionTips":["Deduplicate rows (keeping last value) before collecting.","Trim keys when rows are created, not just when collected.","Show duplicate-key warnings live in the editor UI."],"tags":["validation","environment-variables","duplicate-key","playground-ui"],"backgroundTag":"duplicate-key","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}