jackwener/OpenCLI · error

Failed to parse fixture

Error message

Failed to parse fixture ${p}: ${err instanceof Error ? err.message : String(err)}

What it means

loadFixture reads a fixture JSON file for a site/command pair and parses it. If the file exists but is unreadable or is not valid JSON, the low-level error is wrapped in a descriptive Error naming the fixture path and the parse failure, so callers know exactly which fixture is corrupt.

Solutions

  1. Open the path from the error message and fix the JSON syntax (validate with jq or a JSON linter).
  2. Delete the corrupt fixture and regenerate it with writeFixture by re-running the command that produces it.
  3. Check file permissions/readability if the wrapped message is a read error rather than a parse error.
  4. Verify the file has no BOM, comments, or trailing commas.

Example fix

// before (fixtures/foo.json)
{ "steps": [1, 2,] }
// after
{ "steps": [1, 2] }
Defensive patterns

Strategy: fallback

Validate before calling

import { readFileSync, existsSync } from 'fs';
function fixtureLooksValid(p) {
  if (!existsSync(p)) return true;
  try { JSON.parse(readFileSync(p, 'utf-8')); return true; } catch { return false; }
}

Type guard

function isFixture(v: unknown): v is Fixture {
  return typeof v === 'object' && v !== null && !Array.isArray(v);
}

Try / catch

try {
  const fixture = loadFixture(site, command);
} catch (err) {
  console.error(err.message);
  // regenerate the corrupt fixture
  writeFixture(site, command, defaultFixture);
  fixture = defaultFixture;
}

Prevention

When it happens

Trigger: The fixture file at fixturePath(site, command) exists but contains malformed JSON (trailing commas, truncated writes, hand-edited syntax errors) or cannot be read (permissions, encoding).

Common situations: Manually editing a fixture and breaking JSON; a crashed run leaving a half-written fixture via writeFixture; fixtures committed with BOM or comments; concurrent runs corrupting the file.

Understand the failure class

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/f7ab1ed97beef9d3. Report an issue: GitHub.

Appendix: source

Thrown at src/browser/verify-fixture.ts:122

  /^hn_id$/i,
  /^username$/i,
  /^handle$/i,
  /^uri$/i,
];

export function fixturePath(site: string, command: string): string {
  return path.join(os.homedir(), '.opencli', 'sites', site, 'verify', `${command}.json`);
}

export function loadFixture(site: string, command: string): Fixture | null {
  const p = fixturePath(site, command);
  if (!fs.existsSync(p)) return null;
  try {
    const raw = fs.readFileSync(p, 'utf-8');
    const parsed = JSON.parse(raw) as Fixture;
    return parsed;
  } catch (err) {
    throw new Error(`Failed to parse fixture ${p}: ${err instanceof Error ? err.message : String(err)}`);
  }
}

export function writeFixture(site: string, command: string, fixture: Fixture): string {
  const p = fixturePath(site, command);
  fs.mkdirSync(path.dirname(p), { recursive: true });
  fs.writeFileSync(p, `${JSON.stringify(fixture, null, 2)}\n`, 'utf-8');
  return p;
}

/**
 * Derive a reasonable fixture from sample output. Used by `--write-fixture`
 * to seed a first draft the author can hand-tune.
 *
 * Heuristics:
 * - rowCount.min = 1 if rows non-empty, else 0
 * - columns = keys from the first row
 * - types = typeof of the first row's values, with "number|string" for mixed

View on GitHub (pinned to 49907e53dc)