yamadashy/repomix · error · RepomixError

Failed to generate JSON output: ${error instanceof Error ? e

Error message

Failed to generate JSON output: ${error instanceof Error ? error.message : 'Unknown error'}

What it means

JSON.stringify threw while serializing the assembled jsonDocument, so Repomix rethrows as this RepomixError with the original error as `cause`. The usual underlying reason is a circular reference or BigInt value in the document tree.

Source

Thrown at src/core/output/outputGenerate.ts:222

        staged: renderContext.gitDiffStaged,
      },
    }),
    ...(renderContext.gitLogEnabled && {
      gitLogs: renderContext.gitLogCommits?.map((commit) => ({
        date: commit.date,
        message: commit.message,
        files: commit.files,
      })),
    }),
    ...(renderContext.instruction && {
      instruction: renderContext.instruction,
    }),
  };

  try {
    return JSON.stringify(jsonDocument, null, 2);
  } catch (error) {
    throw new RepomixError(
      `Failed to generate JSON output: ${error instanceof Error ? error.message : 'Unknown error'}`,
      error instanceof Error ? { cause: error } : undefined,
    );
  }
};

const generateHandlebarOutput = async (
  config: RepomixConfigMerged,
  renderContext: RenderContext,
  processedFiles?: ProcessedFile[],
): Promise<string> => {
  try {
    const compiledTemplate = getCompiledTemplate(config.output.style);
    return `${compiledTemplate(renderContext).trim()}\n`;
  } catch (error) {
    if (error instanceof RangeError && error.message === 'Invalid string length') {
      let largeFilesInfo = '';
      if (processedFiles && processedFiles.length > 0) {

View on GitHub (pinned to f465ad9093)

Solutions

  1. Inspect `error.cause` — circular structure usually names the offending object path.
  2. Ensure any custom file data merged into the output contains only JSON-safe values (no BigInt/circular refs).
  3. Update repomix if triggered by stock config — report it as a bug.
  4. Pre-sanitize: JSON.parse(JSON.stringify(payload)) or use a safe-stringify helper for custom data.

Example fix

// before
file.metadata.parent = filesRoot; // circular
// after
delete file.metadata.parent; // keep document JSON-safe
Defensive patterns

Strategy: try-catch

Validate before calling

function assertJsonSafe(obj: unknown, seen = new Set()): void {
  if (obj && typeof obj === 'object') {
    if (seen.has(obj)) throw new Error('Circular reference');
    seen.add(obj);
    for (const v of Object.values(obj)) assertJsonSafe(v, seen);
  } else if (typeof obj === 'bigint') throw new Error('BigInt not JSON-safe');
}

Type guard

const isJsonSafe = (v: unknown, seen = new Set()): boolean => {
  if (typeof v === 'bigint') return false;
  if (v && typeof v === 'object') {
    if (seen.has(v)) return false;
    seen.add(v);
    return Object.values(v).every(x => isJsonSafe(x, seen));
  }
  return true;
};

Try / catch

try {
  const json = await generateOutput({ ...config, output: { ...config.output, style: 'json' } }, ctx);
} catch (e) {
  if (e instanceof RepomixError && e.message.startsWith('Failed to generate JSON output')) {
    console.error('JSON build failed:', (e as { cause?: Error }).cause?.message);
  }
}

Prevention

When it happens

Trigger: generateParsableJsonOutput(renderContext) builds a document containing a value JSON.stringify cannot handle (circular structure, BigInt, unstable objects) and the stringify call throws.

Common situations: Programmatic use where renderContext/file metadata contains circular references (e.g. objects referencing parents); custom file processors injecting BigInt sizes; upstream bugs in metadata assembly.

Related errors


AI-assisted analysis of yamadashy/repomix@f465ad9093 (2026-08-29). Data as JSON: /api/errors/311657edbfded8ba. Report an issue: GitHub.