Skip to content

fix(header): scan full-length metadata block, not just first 40 lines - #1

Closed
Shinrai wants to merge 4 commits into
nextfrom
fix/header-scan-line-limit
Closed

Shinrai wants to merge 4 commits into
nextfrom
fix/header-scan-line-limit

Conversation

@Shinrai

@Shinrai Shinrai commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Closing — this repo doesn't have the CLDMV next/hotfixes staging workflow wired in yet (next is currently stale, 3 commits behind master, and there's no CLDMV/.github reusable-workflow reference in .github/workflows/). Re-opening once the repo is properly bootstrapped onto the v4 release flow.

Shinrai added 4 commits June 7, 2026 21:05
…aders

matchHeaderSegment bounded an existing header with the *styled* blockEnd
token (" */", with a leading space) and a `/**`-only opener. Any real
closer that wasn't byte-for-byte " */" — e.g. `**/`, a tab-indented `*/`,
or `*/` at column 0 — was skipped, so the lazy match ran on to the next
`*/` downstream (inside a `//` line comment, a string literal, or another
block comment) and everything between was deleted on replace. With no
later `*/` in range it instead prepended a duplicate header; same for `/*`
single-asterisk openers, which the `/**` anchor never matched.

Match the real comment grammar instead: anchor on `/*` (which prefixes
both `/*` and `/**`) and terminate at the first `*/` by trimming the
styled space off blockEnd. Block comments don't nest, so the first `*/`
is always the header's own terminator. Generation (renderHeaderLines)
still emits the styled " */". One chokepoint fixes both the data-loss and
duplicate-header cases for every block detector (node/json/php/css and the
default); no detector changes needed. HTML and line-comment syntaxes were
unaffected and are untouched.

Adds regression tests: `**/` closer with a later `*/` in a `//` comment,
`/*` opener, tab-indented closer, column-0 closer, a `//` line containing
`/** */`, and a string literal containing `*/`.
…port (v1.3.0)

A SOURCE directory sharing a name with a build/output dir (e.g. tools/build) was being
skipped; the project's own .gitignore can now drive exclusions instead of a hard-coded list.

- Split DEFAULT_IGNORE_FOLDERS into ALWAYS_IGNORE_FOLDERS (.git, node_modules — skipped at
  any depth) and ROOT_IGNORE_FOLDERS (dist, build, coverage, tmp, .next, .turbo — skipped only
  at the project root). A nested tools/build is now processed; a top-level /build still isn't.
- Add .gitignore support (new `ignore` dependency): `gitignore` option (false disables, a
  path/array loads those files, otherwise auto-detect <projectRoot>/.gitignore), applied to the
  directory walk and the final file filter.
- getCommentSyntaxForFile gains a `detectors` injection, matching detectProjectFromMarkers.
- Bump 1.2.3 -> 1.3.0.

Also clears the dev-dependency npm audit advisories (vitest 3 -> 4.1.8 + @vitest/coverage-v8
4.1.8, plus transitive vite/postcss/picomatch/brace-expansion) -> 0 vulnerabilities.

README documents the gitignore option + ignore scoping. New tests cover .gitignore
(auto-detect/disable/explicit/array, nested-vs-top-level build, any-depth node_modules) and the
defensive tie-break + null-syntax fall-through branches. 132 tests, 100% coverage, tsc clean.
With an `exports` map defined, package.json is otherwise unreadable
(ERR_PACKAGE_PATH_NOT_EXPORTED), which breaks tooling that reads the package version
(CLI --version flags, compat checks, etc.). Re-expose it. Patch bump 1.3.0 -> 1.3.1.
findProjectHeader() only inspected the first DEFAULT_MAX_HEADER_SCAN_LINES
lines when locating an existing header block. If the block's closing */
landed after line 40, the matcher never saw it, so replaceOrInsertHeader()
took the insert branch instead of replace, leaving a duplicate header with
stale fields behind. Raise the cap to 200 to keep the near-top-of-file
intent while covering legitimately long metadata blocks.
@Shinrai Shinrai closed this Jul 18, 2026
@cldmv-bot
cldmv-bot Bot deleted the fix/header-scan-line-limit branch July 30, 2026 03:31
cldmv-bot Bot added a commit that referenced this pull request Nov 1, 2026
# `@cldmv/fix-headers` v2.2.4 Changelog

**Release Date**: October 2026
**Release Type**: Patch
**Branch**: `release/2.2.4`

---

## Overview

Version 2.2.4 makes a file carry exactly one header. Stacked copies of a header are merged into one whatever their first line is, repeated fields are dropped as duplicates, and a stale `@Copyright` for the same holder goes away while one naming another holder is kept and reported as a warning. Only lines that must stay at the very top of a file (a shebang, a Python encoding line, `#cloud-config`, `@charset`, Go build constraints, `<?php`, Markdown front matter, a Vim modeline) stay above the header, and files where an earlier version wrote the header above such a line are repaired. `#` headers are now found in files with CRLF line endings or a byte-order mark. These are the follow-ups to the v2.2.3 header-text fix, tracked in [#162](#162).

This changes what a run writes to files that have a stacked or duplicated header, a stale `@Copyright` line, or a header an earlier version placed above a shebang or directive: the first run after upgrading cleans them up. Files with a single, clean header are written exactly as before.

The rest of the release is packaging and hardening: the entry files are now `src/index.mjs` and `src/index.cjs`, a source checkout run without the `fix-headers-dev` condition stops with a clear message, the Node.js floor is 22.13.0, and two CodeQL ReDoS alerts are fixed.

---

## 🐛 Bug Fixes

### One header per file ([#166](#166), fixes [#162](#162))

- **Stacked headers are merged.** A comment is a file header when it holds a `@Project:` or `@Filename:` line. Every file header stacked directly after the first one (blank lines only in between) is merged into it, whatever its first line is and whatever project or file it names. v2.2.3 only merged a stacked comment whose first line was `@Project:`, so one starting with description text was left as a second header.
- **Repeated fields are duplicates.** A second `@Project`, `@Date` or `@Author` line is dropped along with a `-----` line next to it. In v2.2.3 these were kept as text.
- **Additional `@Copyright` lines are compared by holder.** A second `@Copyright` naming the same holder as the regenerated one (a stale `2013-2025` line next to the new `2013-2026` one) is dropped. One naming another holder (`Copyright (c) 2010 Mozilla Foundation`) is kept as a field directly under the header's own and reported: in the result's new `copyrightWarnings` array (`{ file, copyright }`), in each change's `extraCopyrights`, and on the CLI as a `warning: <file> keeps an additional @copyright: <value>` line with `warnings=<n>` in the summary. The warning never changes the exit code.
- **The v2.2.3 lead line is cleaned up.** A block v2.2.3 moved below the header with the lead line `Moved out of the file header by fix-headers:` and a `@Project:` line is merged as a copy of the header, and the lead line is dropped with its fields. Newly moved blocks no longer get a lead line, because they never hold a `@Project:` or `@Filename:` line.

### Only position-critical lines stay above the header ([#166](#166))

The header is now the first thing in the file, except for lines already at the very top that would break the file or its tooling if moved: a `#!` shebang (JavaScript/TypeScript, Python, Rust scripts), a PEP 263 encoding line, `#cloud-config`, `@charset`, `//go:build` / `// +build` constraints, the `<?php` line, Markdown front matter, and a vi/Vim/ex modeline in any comment style. Nothing further down is moved up.

- **Earlier output is repaired.** When an earlier version wrote the header above one of these lines (`#!/usr/bin/env python3`, header, `# -*- coding: utf-8 -*-`), the line moves back up to join the others and the header is rewritten below them. A byte-order mark left below a header goes back to byte 0.
- **Modelines are recognised by Vim's grammar.** Only real `vi:`/`vim:`/`ex:` modelines stay above a `#` header; prose such as `# vi: is the editor we use` is header text.
- **PHP templates are skipped.** A PHP file that does not open with `<?php` is skipped and reported, because a comment at the top would be output as text. A file where only a fix-headers header comes before `<?php` is repaired instead.

### `#` headers in CRLF and byte-order-mark files ([#166](#166))

A `#` header in a file with CRLF line endings or a UTF-8 byte-order mark was not found, so a second header was added above it. It is now found and updated in place. The header is written in the file's own line ending, a header an earlier version wrote with `\n` into a CRLF file is rewritten with CRLF, and the byte-order mark stays at the start of the file, ahead of the header and any shebang.

### Entry files and the development check ([#165](#165), fixes [#157](#157) and [#158](#158))

- **Entry files.** The ES module entry is `src/index.mjs` (was `src/fix-header.mjs`) and the CommonJS wrapper is its sibling `src/index.cjs` (was `src/cjs/index.cjs`), so `require("./index.mjs")` resolves the same way from source and from `dist/`. The published `dist/index.mjs`, `dist/index.cjs` and `bin/fix-headers.mjs` paths and the package's exports are unchanged. The `fix-headers-dev` condition now covers `require` as well as `import`.
- **Development check.** A source checkout run without `--conditions=fix-headers-dev` would silently use a stale `dist/` build. The entry now loads a root `devcheck.mjs` first, synchronously, which stops the process with a message saying how to run it. The file is never published, so installed copies skip it. It is loaded through `process.getBuiltinModule`, with no static `node:module` import and no top-level await, so the entry stays safe for bundlers and keeps working through `require()`.

### Linear-time text checks (CodeQL [#1](https://github.com/CLDMV/fix-headers/security/code-scanning/1), [#2](https://github.com/CLDMV/fix-headers/security/code-scanning/2), [#165](#165), [#166](#166))

The check for an existing `<…>` tag in the author name and the trailing-slash trim on folder paths used regular expressions that backtrack polynomially on long runs of `<` or `/`. Both are now index scans with the same results. The copyright-holder and modeline matching added in this release are linear-time too.

## 🔧 CI & tooling

- **Node.js floor 22.13.0** ([#165](#165)). `engines.node` is `>=22.13.0` (was `>=22.12.0`), and the CI matrix floor matches. Node 22.12.x prints an `ExperimentalWarning` when `require()` loads an ES module, which the CommonJS entry does.
- The repository's own `cli`, `fix:headers` and CommonJS test scripts and the Vitest config pass `--conditions=fix-headers-dev` ([#165](#165)).

## 📚 Documentation

- **NEW:** [docs/changelog/v2/v2.2.4.md](./v2.2.4.md): this changelog.
- README: new "What stays above the header" section with a per-file-type table, a rewritten "Text in the header block" section, a new "Additional `@Copyright` lines" section, `copyrightWarnings` in the API result, line endings and byte-order marks under the header layout, PHP templates in the skipped files, and the 22.13.0 floor ([#165](#165), [#166](#166)).
- README What's New promoted to v2.2.4.

## 🔧 Dependencies

Development dependencies only; nothing in the published package changes.

- `minimatch` 10.2.6 → 11.0.3 through an npm override, for [GHSA-g3hv-75wg-m249](GHSA-g3hv-75wg-m249) (glob pattern amplification, Dependabot [#14](https://github.com/CLDMV/fix-headers/security/dependabot/14)). ESLint still declares `^10.2.4`.
- `katex` 0.16.47 → 0.19.1 through an npm override, for [GHSA-238p-pmpm-9mq7](GHSA-238p-pmpm-9mq7) (prototype pollution gadget, Dependabot [#7](https://github.com/CLDMV/fix-headers/security/dependabot/7)). It comes in through `@eslint/markdown`, whose math extension still declares `^0.16.0`.

---

## Upgrade notes

- No breaking change to the `fixHeaders` API or the CLI. The result gains a `copyrightWarnings` array, which is always present.
- **Node.js 22.13.0 or later is required** (was 22.12.0).
- **Expect a one-time diff** in files with a stacked or duplicated header, a stale `@Copyright` line, a block v2.2.3 moved below the header with its lead line, a header above a shebang or directive, or a CRLF or byte-order-mark `#` file that got a second header. Run with `--dry-run --diff` first to review it.
- In a fix-headers source checkout, run the source with `node --conditions=fix-headers-dev`; the npm scripts already do.



<details>
<summary>👥 Contributors</summary>

- @Shinrai

</details>

---

<!-- coverage-start -->

![coverage](https://img.shields.io/badge/coverage-99.6%25-brightgreen?style=for-the-badge&logo=vitest&logoColor=white)

| Metric | Coverage |
|--------|----------|
| Statements | 99.9% |
| Branches   | 98.8% |
| Functions  | 100.0% |
| Lines      | 99.9% |

*Avg: **99.6%** · `3b2cd33` · Node lts/**

<!-- coverage-end -->

<!-- co-authors -->

Co-authored-by: Shinrai <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant