Skip to content

Remove UTF-8 BOM from tracked source and config files #52

Closed

Description

@hacksynth

Problem

Multiple tracked source/config files start with a UTF-8 BOM (EF BB BF). Most files in the repo do not, so the encoding state is inconsistent.

Next.js/SWC currently tolerates these files, but BOMs can still cause avoidable problems:

  • Some lint rules, diff tools, shell scripts, and cross-platform tooling treat the first token/path differently.
  • It suggests inconsistent encoding behavior in the edit/generation pipeline, likely from Windows editors or tools writing UTF-8 with BOM.
  • Future bundlers/runtimes or bridges such as Bun/Deno may be less forgiving.
  • Prettier can preserve BOM in some flows, so formatting may not converge on a clean encoding state.

Verified Current State

I checked tracked files by reading the first 3 bytes and currently found 52 tracked files with a UTF-8 BOM.

Examples include:

  • src/middleware.ts
  • src/app/layout.tsx
  • src/app/not-found.tsx
  • src/components/ui/button.tsx
  • src/components/ui/dropdown-menu.tsx
  • src/components/ui/toast.tsx
  • src/components/ui/avatar.tsx
  • src/components/ui/label.tsx
  • src/components/ui/input.tsx
  • src/components/ui/card.tsx
  • src/components/decorative-background.tsx
  • src/components/practice-session.tsx
  • src/lib/ai/prompts.ts
  • src/lib/ai/types.ts
  • src/lib/utils.ts
  • src/lib/prisma.ts
  • src/types-next-auth.d.ts
  • next.config.mjs

Full tracked-file detection output:

.gitattributes
.github/ISSUE_TEMPLATE/config.yml
.gitignore
SUPPORT.md
eslint.config.mjs
next.config.mjs
src/app/(auth)/login/page.tsx
src/app/(auth)/register/page.tsx
src/app/(main)/analysis/page.tsx
src/app/(main)/exam/[attemptId]/page.tsx
src/app/(main)/exam/[attemptId]/result/page.tsx
src/app/(main)/layout.tsx
src/app/(main)/loading.tsx
src/app/(main)/plan/[id]/page.tsx
src/app/(main)/practice/case/[id]/page.tsx
src/app/(main)/practice/random/page.tsx
src/app/(main)/practice/session/page.tsx
src/app/(main)/profile/page.tsx
src/app/(main)/wrong-notes/page.tsx
src/app/api/ai/diagnosis/route.ts
src/app/api/ai/explain/route.ts
src/app/api/ai/status/route.ts
src/app/api/analysis/route.ts
src/app/api/auth/[...nextauth]/route.ts
src/app/api/exam/[id]/result/route.ts
src/app/api/knowledge/[id]/route.ts
src/app/api/knowledge/route.ts
src/app/api/practice/cases/[id]/result/route.ts
src/app/api/practice/cases/[id]/route.ts
src/app/api/practice/summary/route.ts
src/app/api/profile/heatmap/route.ts
src/app/api/profile/knowledge-stats/route.ts
src/app/api/profile/stats/route.ts
src/app/globals.css
src/app/layout.tsx
src/app/not-found.tsx
src/components/decorative-background.tsx
src/components/practice-session.tsx
src/components/ui/avatar.tsx
src/components/ui/button.tsx
src/components/ui/card.tsx
src/components/ui/dropdown-menu.tsx
src/components/ui/input.tsx
src/components/ui/label.tsx
src/components/ui/toast.tsx
src/lib/ai/prompts.ts
src/lib/ai/types.ts
src/lib/prisma.ts
src/lib/utils.ts
src/middleware.ts
src/types-next-auth.d.ts
tsconfig.json

Reproduction

POSIX example:

for f in $(find src -name '*.ts' -o -name '*.tsx'); do
  head -c 3 "$f" | od -c | head -1 | grep -q '357 273 277' && echo "BOM: $f"
done

PowerShell example for tracked files:

$files = git ls-files
foreach ($file in $files) {
  if (-not (Test-Path -LiteralPath $file -PathType Leaf)) { continue }
  $bytes = [System.IO.File]::ReadAllBytes((Resolve-Path -LiteralPath $file))
  if ($bytes.Length -ge 3 -and $bytes[0] -eq 0xEF -and $bytes[1] -eq 0xBB -and $bytes[2] -eq 0xBF) {
    $file
  }
}

Current Guardrails

.editorconfig already contains:

[*]
charset = utf-8

However, this has not prevented BOMs from entering or remaining in tracked files. The project also has a pre-commit hook that runs lint-staged, but there is no explicit no-BOM check.

Suggested Fix

  1. Remove the UTF-8 BOM from all tracked text/source files in one cleanup commit.
  2. Keep .editorconfig as UTF-8 without BOM policy. If needed, clarify the comment/policy around UTF-8 without BOM.
  3. Add an automated guard to prevent regressions, for example:
    • a small scripts/check-no-bom.* script used in CI and/or lint-staged, or
    • a pre-commit hook step that rejects staged files starting with EF BB BF.

Example one-off cleanup command for POSIX environments:

sed -i '1s/^\xEF\xBB\xBF//' $(grep -rlI $'\xEF\xBB\xBF' src/ next.config.mjs)

For cross-platform compatibility, a small Node cleanup/check script may be safer than relying on sed -i behavior.

Acceptance Criteria

  • No tracked source/config files start with UTF-8 BOM bytes EF BB BF.
  • Existing file contents are otherwise unchanged apart from BOM removal.
  • .editorconfig continues to specify UTF-8 encoding.
  • There is a regression guard in CI, pre-commit, or lint-staged that fails on BOM-prefixed text/source files.
  • npm run format:check, npm run lint, and npm run typecheck still pass.

Activity

  1. hacksynth commented on May 2, 2026

    @hacksynth
    OwnerAuthor

    @claude 请处理这个 issue。

    请按 issue 描述和 acceptance criteria 落地修复,保持改动范围聚焦;完成后请提交 PR,并在本 issue 回复 PR 链接。

  2. claude commented on May 2, 2026

    @claude

    Claude finished @hacksynth's task in 6m 30s —— View job • claude/issue-52-20260502-1624 • Create PR ➔


    Fixing UTF-8 BOM in tracked source files

    • Gather context (read CLAUDE.md and repo structure)
    • Remove UTF-8 BOM from all 52 tracked files
    • Create scripts/check-no-bom.js regression guard script (check + --fix modes)
    • Wire check into package.json (check:bom script + lint-staged for all text file extensions)
    • Commit and push changes

    Summary

    52 tracked files had their UTF-8 BOM (EF BB BF) stripped. File contents are otherwise unchanged.

    Regression guard added:

    • scripts/check-no-bom.js — runs in check mode by default (exits 1 on any BOM), or --fix mode to strip in-place. Also accepts explicit file paths (used by lint-staged).
    • package.json — new "check:bom" script + lint-staged entry covering *.{ts,tsx,mjs,cjs,js,json,css,md,yml,yaml,d.ts} so staged files are blocked if they contain a BOM.
    • .editorconfig already specifies charset = utf-8 and is unchanged.
  3. hacksynth commented on May 2, 2026

    @hacksynth
    OwnerAuthor

    @claude claude/issue-52-20260502-1624 还不能开 PR:新增的 scripts/check-no-bom.js 使用 ESM import 语法,但项目 package.json 没有 type: module,Node 会把 .js 当 CommonJS,npm run check:bom / lint-staged 会因 SyntaxError: Cannot use import statement outside a module 失败。请改成 CommonJS require(...),或把脚本改名为 .mjs 并同步 package/lint-staged 调用。修完后请确认 node scripts/check-no-bom.js 能直接运行。

  4. claude commented on May 2, 2026

    @claude

    Claude finished @hacksynth's task in 10m 21s —— View job • claude/issue-52-20260502-1632 • Create PR ➔


    Fixed ESM/CJS issue in scripts/check-no-bom.js

    • Gather context (read branch state and package.json)
    • Root cause: previous script used ESM import — package.json has no "type":"module" so Node treats .js as CommonJS
    • Rewrote scripts/check-no-bom.js using CommonJS require('fs') / require('child_process') — runs directly as node scripts/check-no-bom.js
    • Removed UTF-8 BOM (EF BB BF) from all 52 tracked source/config files
    • Updated package.json: added "check:bom" script + lint-staged entry covering all text file extensions
    • Committed (54 files, 1 new script) and pushed to claude/issue-52-20260502-1632

    What changed

    scripts/check-no-bom.js — CommonJS only, no ESM syntax:

    'use strict';
    const fs = require('fs');
    const { execSync } = require('child_process');
    // ...
    • Default mode: scans all git ls-files tracked files, exits 1 with list if any BOM found
    • --fix mode: strips BOM in-place
    • Accepts explicit file paths (used by lint-staged)

    package.json additions:

    "check:bom": "node scripts/check-no-bom.js"
    "lint-staged": {
      "*.{ts,tsx,mjs,cjs,js,json,css,md,yml,yaml,d.ts}": ["node scripts/check-no-bom.js"],
      ...
    }

    · claude/issue-52-20260502-1632

  5. added a commit that references this issue on May 2, 2026
    4bb3611
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions