{"record":{"id":"f56ea9433507c6dc","repo":"mastra-ai/mastra","slug":"invalid-env-value-for-key-multiline-values-are","errorCode":null,"errorMessage":"Invalid ENV value for ${key}: multiline values are not supported.","messagePattern":"Invalid ENV value for (.+?): multiline values are not supported\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/services/service.env.ts","lineNumber":22,"sourceCode":"  abstract getEnvValue(key: string): Promise<string | null>;\n  abstract setEnvValue(key: string, value: string): Promise<void>;\n}\n\nfunction escapeRegExp(value: string): string {\n  return value.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&');\n}\n\nexport class FileEnvService extends EnvService {\n  private static readonly ENV_KEY_REGEX = /^[A-Za-z_][A-Za-z0-9_]*$/;\n\n  private readonly filePath: string;\n\n  private validateEnvEntry(key: string, value: string): void {\n    if (!FileEnvService.ENV_KEY_REGEX.test(key)) {\n      throw new Error(`Invalid ENV key: ${key}`);\n    }\n    if (/[\\r\\n]/.test(value)) {\n      throw new Error(`Invalid ENV value for ${key}: multiline values are not supported.`);\n    }\n  }\n\n  constructor(filePath: string) {\n    super();\n    this.filePath = filePath;\n  }\n\n  private envLineRegex(key: string, captureValue = false): RegExp {\n    const pattern = captureValue ? `^${escapeRegExp(key)}=(.*)$` : `^${escapeRegExp(key)}=.*$`;\n    return new RegExp(pattern, 'm');\n  }\n\n  private async updateEnvData({\n    key,\n    value,\n    filePath = this.filePath,\n    data,","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/services/service.env.ts#L4-L40","documentation":"FileEnvService.validateEnvEntry throws this when the VALUE for an env entry contains a carriage return or newline. .env files are line-based (KEY=VALUE per line), so multiline values would corrupt parsing of subsequent entries. The CLI rejects such values up front rather than writing a broken file.","triggerScenarios":"Calling updateEnvData with a value containing '\\n' or '\\r', e.g. multiline secrets (JSON blobs, private keys, certificates) pasted as-is, or values read from a multiline source.","commonSituations":"Storing a private key or JSON as an env var without encoding; copying a value from a terminal that included a trailing newline; generating values programmatically from multiline input.","solutions":["Replace newlines with an escape sequence, e.g. '\\\\n', and decode on read","Base64-encode multiline content: Buffer.from(value).toString('base64')","Strip whitespace: value.replace(/[\\r\\n]+/g, ' ') or .trim() if it is only trailing","Store multiline secrets in a file and pass a path in the env var instead"],"exampleFix":"// before\nawait service.updateEnvData({ PRIVATE_KEY: fs.readFileSync('key.pem', 'utf8') });\n// after\nawait service.updateEnvData({ PRIVATE_KEY: Buffer.from(fs.readFileSync('key.pem', 'utf8')).toString('base64') });","handlingStrategy":"validation","validationCode":"function assertSingleLineEnvValue(key: string, value: string) {\n  if (/\\r|\\n/.test(value)) throw new Error(`Env value for ${key} must not contain newlines`);\n}\n// or encode multiline content:\nconst encoded = /\\r|\\n/.test(value) ? Buffer.from(value).toString('base64') : value;","typeGuard":"const isSingleLine = (value: string): boolean => !/[\\r\\n]/.test(value);","tryCatchPattern":"try {\n  await service.updateEnvData(entries);\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Invalid ENV value')) {\n    console.error('Multiline env values are unsupported; base64-encode or flatten them.');\n  } else throw e;\n}","preventionTips":["Trim values read from files/terminals to drop stray trailing newlines","Encode multiline content (base64 or \\\\n escapes) before storing in .env","Keep multiline secrets in dedicated files referenced by path","Validate all entries with a single pre-write check loop"],"tags":["env","validation","cli-services"],"backgroundTag":"invalid-env-value","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}