Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Heuristic:20000419 Fauxnix CRLF Normalization

From Leeroopedia
Knowledge Sources
Domains Output_Normalization, Cross_Platform, Testing
Last Updated 2026-10-26 00:00 GMT

Overview

Normalize `\r\n` → `\n` before comparing cross-shell output so that CRLF differences between PowerShell and Git Bash don't cause false mismatches in differential testing or corrupt user-visible output.

Description

PowerShell 5.1's console host terminates each `Write-Output` object with CRLF and a final newline. Git Bash uses LF. Without normalization, every line of output would differ between the two engines. Fauxnix handles this in two places: (1) `normalizeHostNewlines` in `encoding.ts` rewrites CRLF → LF for host output, but only when every LF is part of a CRLF pair and the buffer ends with a newline — this preserves intentional CR/CRLF from exact writers like `fx-write`, `printf`, and `echo -n`. (2) `normalizeNewlines` in the differential runner (`test/differential/run.ts`) aggressively converts both `\r\n` → `\n` and lone `\r` → `\n` before comparing fauxnix vs Git Bash streams. The differential identity check (`sameIdentity`) compares normalized stdout, normalized stderr, and exact exitCode.

Usage

Use this heuristic when comparing output from two shells that may use different line endings. Normalize before comparison, but preserve intentional CRLF in exact-writer output by checking that the normalization is safe (all LFs are part of CRLF pairs). For pure identity testing where exact writers are not involved, aggressive normalization (`\r\n` and lone `\r` → `\n`) is sufficient.

The Insight (Rule of Thumb)

  • Action: Normalize CRLF → LF before cross-shell comparison; use `normalizeNewlines` for differential testing and `normalizeHostNewlines` for host output.
  • Value: `normalizeHostNewlines` only normalizes when safe (all LFs are CRLF-paired + buffer ends with `\n`); `normalizeNewlines` always converts `\r\n` and `\r` → `\n`.
  • Trade-off: Exact writers that intentionally emit CR/CRLF are preserved by `normalizeHostNewlines` but flattened by the differential `normalizeNewlines` — acceptable because the corpus doesn't test exact-writer CRLF.
  • Compatibility: The differential identity check also compares `exitCode` exactly (no normalization needed for integers).

Reasoning

Windows uses CRLF; Unix and Git Bash use LF. PowerShell's `Write-Output` adds CRLF after each object. If fauxnix returns `hello\r\n` and bash returns `hello\n`, a naive byte comparison would report a mismatch — but semantically they are identical. Normalizing before comparison eliminates this false positive. The key subtlety is that some commands (`echo -n`, `printf`, `fx-write`) intentionally emit exact bytes including CR. `normalizeHostNewlines` detects this by verifying that every LF in the buffer is part of a CRLF pair — if any lone LF exists, the buffer is left untouched, preserving exact-writer output. The differential runner uses aggressive normalization because its corpus cases don't depend on CRLF preservation.

Code Evidence

Safe host newline normalization from `src/encoding.ts:53-60`:

export function normalizeHostNewlines(s: string): string {
  if (s.length === 0 || !s.includes('\n')) return s;
  if (!s.endsWith('\n')) return s;
  for (let i = 0; i < s.length; i++) {
    if (s[i] === '\n' && (i === 0 || s[i - 1] !== '\r')) return s;
  }
  return s.replace(/\r\n/g, '\n');
}

Differential newline normalization from `test/differential/run.ts:123-125`:

export function normalizeNewlines(s: string): string {
  return s.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
}

Identity comparison from `test/differential/run.ts:127-133`:

export function sameIdentity(a: Streams, b: Streams): boolean {
  return (
    normalizeNewlines(a.stdout) === normalizeNewlines(b.stdout) &&
    normalizeNewlines(a.stderr) === normalizeNewlines(b.stderr) &&
    a.exitCode === b.exitCode
  );
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment