🐛 Sort need links and backlinks naturally in needs.json and HTML#1695
Open
🐛 Sort need links and backlinks naturally in needs.json and HTML#1695
Conversation
Need link and backlink lists are now naturally sorted (e.g. ``REQ_2`` < ``REQ_9`` < ``REQ_10``) so build outputs are reproducible regardless of need load order, including when ``needs_external_needs`` is configured. Sorting happens unconditionally — no ``needs_reproducible_json`` flag is required — and applies to both ``needs.json`` and HTML output. Implementation: ``NeedItem.sort_links()`` sorts ``_links``, ``_backlinks``, and per-part backlinks in place using a natural sort key that splits digit runs and compares them as ints. ``resolve_links()`` calls this on every need at the end of the post-processing pass, so all downstream consumers (JSON serialization, HTML rendering of outgoing/incoming refs) see sorted lists. Tests: new ``tests/test_needs_sort.py`` with YAML-driven parametric cases covering alphabetical/natural/mixed sorting, backlinks, and custom link types — checks both the JSON and rendered HTML against snapshots. Closes #1371
Adds two fixture cases with identical RST content but opposite values for ``needs_reproducible_json``. Their snapshots end up byte-identical, which documents that sorting happens regardless of the flag.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1695 +/- ##
==========================================
+ Coverage 86.87% 89.32% +2.44%
==========================================
Files 56 72 +16
Lines 6532 10340 +3808
==========================================
+ Hits 5675 9236 +3561
- Misses 857 1104 +247
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Member
Author
|
This PR is missing a performance analysis. Sorting links in big projects can be expensive. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1371.
Need link and backlink lists are now naturally sorted (e.g.
REQ_2<REQ_9<REQ_10) so build outputs are reproducible regardless of need load order, including whenneeds_external_needsis configured. Sorting happens unconditionally — noneeds_reproducible_jsonflag is required — and applies to bothneeds.jsonand HTML output.This matches the behaviour
@ubmarcoconfirmed was desired: sort by default, in both outputs, irrespective of theneeds_reproducible_jsonsetting (which still keeps its existing role of stripping timestamps).Implementation
NeedItem.sort_links()sorts_links,_backlinksand per-partbacklinksin place using a natural sort key (digit runs are compared as ints, with the result alternatingstr/intso mixed-type comparisons are well-defined).resolve_links()callssort_links()on every need at the end of the post-processing pass, so all downstream consumers — JSON serialization inneedsfile.pyand the HTML rendering of outgoing/incoming refs inroles/need_outgoing.pyandroles/need_incoming.py— see sorted lists.Tests
New
tests/test_needs_sort.pyis YAML-driven and snapshot-based, modelled ontests/schema/test_schema.py:tests/fixtures/sort_links.ymldeclares 5 cases:alphabetical_outgoing— basic alphabetical sort of outgoing linksnatural_outgoing— natural sort with digits (provesREQ_10lands afterREQ_9)mixed_alphanumeric— mixed letter+number IDsbacklinks_sorted— backlinks naturally sorted (SPEC_1, SPEC_2, SPEC_10)custom_link_types— sort works for custom link types (implements,derives)needs.json, derives link-field names from the embeddedneeds_schema(field_type ∈ {links, backlinks}), and snapshots the link/backlink fields per need. It also readsindex.html, extracts(source need, target need)ref pairs by matchingtitle="<source>"on link anchors, and snapshots that mapping. Both snapshots clearly show the natural ordering.Existing snapshots in 8 unrelated test files were updated for the new sorted order; all changes are mechanical reorderings within link lists.
Test plan
pytest tests/test_needs_sort.py— 5 cases pass, 10 snapshots matchpytest tests/— no link-related regressionsChangelog
The bug-fix entry is in a new
Unreleasedsection above 8.0.0 (now marked released on 19.03.2026).