Skip to content

Fix requirements-txt-fixer reordering pip options#1249

Closed
Bahtya wants to merge 1 commit intopre-commit:mainfrom
Bahtya:fix-requirements-txt-fixer-pip-options-ordering
Closed

Fix requirements-txt-fixer reordering pip options#1249
Bahtya wants to merge 1 commit intopre-commit:mainfrom
Bahtya:fix-requirements-txt-fixer-pip-options-ordering

Conversation

@Bahtya
Copy link
Copy Markdown

@Bahtya Bahtya commented Apr 9, 2026

Summary

Fixes #612

The requirements-txt-fixer hook was alphabetically sorting all lines in requirements.txt, including pip options like --index-url and --extra-index-url. This caused --extra-index-url to be sorted above --index-url (since "extra" < "index" alphabetically), which breaks pip's index resolution order.

Changes

  • pre_commit_hooks/requirements_txt_fixer.py: Added a check in Requirement.__lt__ so that when both requirements being compared start with -- (i.e., are pip options), they maintain their original relative order instead of being sorted alphabetically. Regular package requirements continue to be sorted alphabetically.

  • tests/requirements_txt_fixer_test.py: Added 3 test cases:

    • --index-url before --extra-index-url stays in order (PASS)
    • --extra-index-url before --index-url stays in order (PASS, original order preserved)
    • Packages are still sorted while pip options remain in place (FAIL triggers sort, pip options unchanged)

Test plan

  • All 416 existing tests pass
  • 3 new test cases cover the reported bug
  • Verified that --index-url / --extra-index-url ordering is preserved
  • Verified that regular package sorting still works correctly alongside pip options

Bahtya

The requirements-txt-fixer was alphabetically sorting all lines, including
pip options like --index-url and --extra-index-url. This caused
--extra-index-url to be sorted above --index-url, breaking pip's index
resolution order since --index-url must come first.

Fix by preserving the original relative order of lines that start with
`--` (pip options), while still sorting regular package requirements
alphabetically.

Closes pre-commit#612

Bahtya
@asottile
Copy link
Copy Markdown
Member

asottile commented Apr 9, 2026

ai slop prs are not acceptable

@asottile asottile closed this Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

requirements-txt-fixer ordering breaks using --index-url and --extra-index-url together in requirements.txt

2 participants