Fix dependency computation by topological sorting#189
Open
otacke wants to merge 1 commit into
Open
Conversation
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.
Thank you for your interest in contributing to H5P! 🎉
Please fill in the below sections to make your pull request:
What kind of change does this PR introduce?
Bug: Dependencies do not get loaded in correct order, e.g. H5P.Question before JoubelUI which then causes crashes when H5P.Question wants to use JoubelUI.
What is the current behaviour?
The dependency computation that compiles the list of scripts to be loaded sorts on a depth-level-first approach and then by reference count within the same level. This works well in many cases, but does not guarantee a valid dependency order.
For example,
H5P.QuestionandH5P.JoubelUIare both used by several other content types. Because of that, they both end up grouped together at the same "level" in the dependency tree. Within that group, the one that's used by more content types is simply listed first — and in this case that happens to beH5P.Question, even thoughH5P.Questionitself needsH5P.JoubelUIto already be loaded. The cli tool has no way of knowing that a relationship like this exists between two entries that landed in the same group, so it can get the order backwards. When that happens,H5P.JoubelUIisn't available yet whenH5P.Questionruns, which breaks anything in H5P.Question that relies on it.What is the new behaviour?
After computing the existing level + weight order (kept as a fallback),
computeDependenciesnow runs a topological sort over the actual dependencies recorded for each library. This guarantees a library's own dependencies always precede it in the returned order. The original level + weight order is still used to break ties between independent libraries and, if a circular dependency is ever encountered, to deterministically resolve it instead of hanging or producing undefined ordering.Net effect (what my issue was): joubel-ui.js now consistently loads before question.js with no change to load order for libraries that aren't affected by this edge case.
PR Checklist
Before submitting, please confirm:
Read the contribution guidelines to get your pr accepted more quickly.