Files
next.js/.gitattributes
Jimmy Lai 69ae2c10e7 fix: normalize CRLF line endings in jscodeshift tests on Windows (#88008)
## Summary

Fixes flaky jscodeshift codemod tests on Windows CI (e.g.,
`next-async-request-api-dynamic-apis.test.js`,
`built-in-next-font.test.js`, `new-link.test.js`).

### Problem

Windows CI runners have `core.autocrlf=true` by default, which causes
test failures because:

1. Fixture files get checked out with CRLF line endings
2. jscodeshift's printer (recast) outputs CRLF on Windows
3. String comparison fails between expected and actual output

### Solution

Patches jscodeshift's test utilities in `jest-setup-after-env.ts`
(Windows only) to normalize line endings:

```typescript
const normalizeLF = (str: string) =>
  str
    .replace(/\r\n/g, '\n')           // CRLF → LF
    .replace(/[ \t]+$/gm, '')         // Remove trailing whitespace per line
    .replace(/\n*$/, '\n')            // Ensure exactly one trailing newline
```

**Key changes:**
- **Patches `runInlineTest`** - Normalizes input, runs transform,
normalizes output, then compares
- **Patches `defineTest`** - Reads fixtures with normalization, uses
patched `runInlineTest`
- **Handles closure issue** - jscodeshift's internal functions use
closures that bypass exports, so we replace the functions entirely

Also includes a belt-and-suspenders `sed` command in the workflow to
strip CRLF from source files.

### Why not just fix `.gitattributes`?

The `.gitattributes` `eol=lf` setting helps, but doesn't solve the
problem because:
1. jscodeshift's recast printer uses platform-native line endings when
generating output
2. Fixture files may have inconsistent trailing whitespace/newlines
3. Self-hosted Windows runners may have different git configurations

### Follow-up

**TODO:** Configure Windows self-hosted runners with `git config
--system core.autocrlf false` so the `sed` workaround can be removed.
See the `TODO` comment in `build_reusable.yml`.

## Test Plan

- Windows CI unit tests should pass consistently
- Normalization only runs on Windows (`process.platform === 'win32'`)
- Other platforms unaffected

---------

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: JJ Kasper <jj@jjsweb.site>
2026-01-03 09:15:48 +01:00

13 lines
440 B
Plaintext