Skip to content

fix: avoid crash when contributors tag is at the start of a file - #521

Merged
JimMadge merged 2 commits into
all-contributors:mainfrom
davidpavlovschi:fix/contributors-tag-at-start-of-file
Aug 6, 2026
Merged

JimMadge merged 2 commits into
all-contributors:mainfrom
davidpavlovschi:fix/contributors-tag-at-start-of-file

Conversation

@davidpavlovschi

Copy link
Copy Markdown
Contributor

What:

Fixes the crash in all-contributors add/generate when the file being updated starts immediately with the list tag (no title or any content before it — the p5.js CONTRIBUTORS.md case from #376). As reported in the issue:

RangeError: Invalid count value: -1
    at String.repeat (<anonymous>)
    at .../dist/generate/index.js:19:186
    at module.exports (.../dist/generate/index.js:75:78)

Closes #376

Why:

In injectListBetweenTags (src/generate/index.js), nbSpaces is derived from lastIndexOf('\n', startOfOpeningTagIndex). When the tag is at the very start of the file, lastIndexOf returns -1, Math.max(0, …) clamps it to 0, so nbSpaces is 0 and ' '.repeat(nbSpaces - 1) throws RangeError: Invalid count value: -1. For a tag at column 0 on any later line, nbSpaces is 1 and the same expression is repeat(0) — so a zero indent is exactly the established behaviour for an unindented tag; only the file-start position crashes.

How:

Clamp the count: ' '.repeat(Math.max(0, nbSpaces - 1)). No existing output changes — every current snapshot passes untouched. The sibling badge path (replaceBadge, ' '.repeat(nbSpaces)) subtracts nothing and cannot go negative, so it is deliberately left alone.

Added a regression test in src/generate/__tests__/index.js that runs generate() over content whose first byte is <!-- ALL-CONTRIBUTORS-LIST:START --> and asserts a table is injected. It fails on main (reproducing the same RangeError at the String.repeat frame, seen from the test runner rather than the built dist/ bundle) and passes with this change.

Verified locally with npm ci: npx vitest run — 15 files, 114 tests, all passing, no snapshot updates; npx eslint src/generate and npx prettier --check clean.

Checklist:

  • Documentation N/A
  • Tests
  • Ready to be merged
  • Added myself to contributors table N/A

When a file begins with the ALL-CONTRIBUTORS-LIST:START tag, nbSpaces is 0 and
' '.repeat(nbSpaces - 1) throws RangeError: Invalid count value: -1.

Clamp the indent to zero, which matches the width already produced for a tag
sitting at column 0 on any later line.

Closes all-contributors#376
@JimMadge
JimMadge requested a review from a team August 4, 2026 13:25

@JimMadge JimMadge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot @davidpavlovschi 🎉.

The code in this section is a little hard to understand. I think this is about retaining any indent that the contributors table had before.

I haven't figured out why there is a need for the -1 though 🤔. I'm wondering if there is a better way to handle all cases. This fixes the issue though so I think we can get it merged without worrying about rewriting the entire function.

Comment thread src/generate/index.js Resolved
Fold the -1 and the Math.max(0, ...) into the nbSpaces calculation so the
value can never be negative, and keep a plain ' '.repeat(nbSpaces) at the
use site, as suggested in review.

@JimMadge JimMadge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks @davidpavlovschi 🙏 🎉

@lwasser how is the release prep going? Can we get this fix in (or following shortly in a patch release)?

@JimMadge
JimMadge merged commit 34d63ca into all-contributors:main Aug 6, 2026
6 checks passed
@JimMadge

JimMadge commented Aug 6, 2026

Copy link
Copy Markdown
Member

@allcontributors please add @davidpavlovschi for code

@allcontributors

Copy link
Copy Markdown
Contributor

@JimMadge

I've put up a pull request to add @davidpavlovschi! 🎉

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.

Crash when adding contributors and there is no content before the all contributors comment tag

2 participants