Skip to content

Fix dependency computation by topological sorting#189

Open
otacke wants to merge 1 commit into
h5p:masterfrom
otacke:fix-preloading
Open

Fix dependency computation by topological sorting#189
otacke wants to merge 1 commit into
h5p:masterfrom
otacke:fix-preloading

Conversation

@otacke

@otacke otacke commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

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.Question and H5P.JoubelUI are 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 be H5P.Question, even though H5P.Question itself needs H5P.JoubelUI to 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.JoubelUI isn't available yet when H5P.Question runs, 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), computeDependencies now 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:

  • I searched for existing issues/PRs first
  • I linked related issues/discussions
  • I tested my changes locally

Read the contribution guidelines to get your pr accepted more quickly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

1 participant