Stabilize import topological sort and add comprehensive test coverage#16149
Stabilize import topological sort and add comprehensive test coverage#16149
Conversation
|
@copilot Go Keys in maps are not sorted and have no guaranteed order. Review topological map that relies on maps for stability. |
…ests Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
|
@copilot merge main and recompile |
…bility Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Fixed in ba27ddd. The topological sort now sorts ALL map iterations:
Both loops that iterate over the |
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical stability issue in the import topological sort algorithm by ensuring all map iterations are deterministic. The algorithm previously had non-deterministic behavior due to Go's randomized map iteration order in two critical locations: the in-degree calculation phase and the in-degree reduction phase of Kahn's algorithm. The fix extracts map keys into slices and sorts them before iteration in both locations, ensuring stable, reproducible topological ordering.
Changes:
- Fixed non-deterministic map iteration in in-degree calculation phase by sorting keys before iteration
- Fixed non-deterministic map iteration in in-degree reduction phase by sorting keys before iteration
- Added 5 comprehensive test cases covering diverse import graph topologies (wide trees, reverse alphabetical ordering, multi-level chains, parallel branches with shared dependencies, and mixed naming patterns)
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pkg/parser/import_processor.go | Fixed stability issues by adding sorted iteration in both in-degree calculation (lines 872-886) and in-degree reduction (lines 914-932) phases of the topological sort algorithm |
| pkg/parser/import_topological_test.go | Added 5 new comprehensive test cases (lines 131-297) covering wide trees, reverse alphabetical ordering, multi-level chains, parallel branches, and mixed naming patterns to ensure stability across diverse graph topologies |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The branch is already up to date with origin/main. Ran Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Stable Topological Sort for Imports
Plan
Changes Made
1. Fixed ALL Stability Issues in Topological Sort (
pkg/parser/import_processor.go)Problem: The topological sort algorithm had TWO places where it iterated over Go maps with randomized iteration order:
Both caused non-deterministic output.
Solution: Modified BOTH loops to iterate over dependencies in sorted order:
Fix #1 - In-degree calculation (lines 872-886):
Fix #2 - In-degree reduction (lines 914-932):
2. Added Comprehensive Test Cases (
pkg/parser/import_topological_test.go)Added 5 new test cases covering diverse import graph topologies:
Test Results
All topological sort tests pass successfully:
TestImportTopologicalSort(9 test cases)TestImportTopologicalSortWithSectionsTestImportTopologicalSortPreservesAlphabeticalForSameLevelpkg/parsertests passTechnical Details
The fix ensures deterministic ordering at THREE levels:
This guarantees that the topological sort produces the same output every time, regardless of Go's map iteration randomization, by ensuring ALL map iterations are replaced with sorted slice iterations.
Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.