Skip to content

Fix requirements-txt-fixer reordering --index-url after --extra-index-url (#612) - #1270

Open
sudorm-rf0 wants to merge 1 commit into
pre-commit:mainfrom
sudorm-rf0:fix-index-url-ordering-612
Open

Fix requirements-txt-fixer reordering --index-url after --extra-index-url (#612)#1270
sudorm-rf0 wants to merge 1 commit into
pre-commit:mainfrom
sudorm-rf0:fix-index-url-ordering-612

Conversation

@sudorm-rf0

Copy link
Copy Markdown

Summary

Fixes #612requirements-txt-fixer sorts requirements alphabetically, which
reorders --index-url after --extra-index-url. That breaks pip: the primary
index URL is silently dropped because pip only honours the first
--index-url it sees.

Before

A requirements.txt written in the correct order:

--index-url https://primary.example.com
--extra-index-url https://extra.example.com
foo==1.0

was "fixed" (reordered) to:

--extra-index-url https://extra.example.com
--index-url https://primary.example.com
foo==1.0

With --extra-index-url first, pip drops the primary index URL.

After

--index-url is now always kept before --extra-index-url, matching pip's
expected semantics.

Root cause

In requirements_txt_fixer.py, each line (including --index-url and
--extra-index-url) is treated as a Requirement and sorted by name via
Requirement.__lt__. Since '--extra-index-url' < '--index-url'
alphabetically, the extra index ended up first.

Fix

Special-case the two options in Requirement.__lt__ so --index-url always
sorts before --extra-index-url. All other ordering (packages, comments,
duplicates) is unchanged.

Tests

Added two cases to tests/requirements_txt_fixer_test.py:

  • --index-url already first → left unchanged (PASS).
  • --extra-index-url first → reordered so --index-url comes first (FAIL,
    file rewritten).

Verification

  • pytesttests/requirements_txt_fixer_test.py: 33 passed (including the
    two new cases).
  • pre-commit run --files ... — all hooks pass (flake8, mypy, pyupgrade,
    reorder-python-imports, add-trailing-comma, autopep8).

…-url (pre-commit#612)

pip uses the first --index-url as the primary index and treats later --extra-index-url entries as additional indexes. If --extra-index-url sorts before --index-url (alphabetical order), the primary index URL is silently dropped. Keep --index-url ordered first. Fixes pre-commit#612.
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