Skip to curriculum
Jump to the editor

14.12. Order package builds by their dependencies

Hard
Topics
35–50 min

A monorepo holds dozens of packages that depend on each other: the app needs the UI kit, which needs the design tokens and the icons. A build tool must build every package after the ones it depends on, and as many as possible at the same time. It must also refuse a workspace where packages depend on each other in a loop, and say exactly where the loop is. And after a change, it should rebuild only what the change affects.

Write two functions in plan-builds.js. planBuilds(packages) returns the build in stages: every package in a stage can build in parallel, because all its dependencies were built in earlier stages. rebuildOrder(packages, changed) returns the stages for rebuilding the changed packages and everything that depends on them.

Signature

JAVASCRIPT
function planBuilds(packages)            // returns an array of stages
function rebuildOrder(packages, changed) // returns an array of stages
  • packages: an object; each key is a package name, each value the array of names it depends on.
  • changed: an array of package names.
  • A stage is an array of package names, sorted alphabetically. The result lists the stages from first to last.

Requirements

  1. Every package appears in exactly one stage, after the stages of all its dependencies.
  2. Each package goes in the earliest stage it can: the first stage holds every package with no dependencies, and a package goes in the stage right after its last dependency.
  3. Each stage is sorted by name. An empty workspace gives []. A dependency listed twice counts once.
  4. A dependency that is not a key of packages throws an Error that names the package and the missing dependency.
  5. A dependency cycle (including a package that depends on itself) throws an Error whose message contains "cycle" and the path joined with " -> ", and whose cycle property is that path as an array: each package depends on the next, the first and last are the same, and no other name repeats. Packages that only wait on the cycle are not part of the path.
  6. planBuilds does not change packages, handles a chain of 10,000 packages without running out of stack, and plans 5,000 packages well under a second.
  7. rebuildOrder returns the stages for the changed packages and every package that depends on one of them, directly or not. Packages outside that set count as already built.
  8. rebuildOrder throws an Error for a changed name that is not in the workspace, and throws like planBuilds when the workspace has a missing dependency or a cycle. A name listed twice counts once; no changes give [].

Examples

JAVASCRIPT
const packages = {
  utils: [],
  tokens: [],
  icons: ["tokens"],
  ui: ["utils", "tokens", "icons"],
  api: ["utils"],
  web: ["ui", "api"],
  docs: ["ui"],
};

planBuilds(packages);
// [["tokens", "utils"], ["api", "icons"], ["ui"], ["docs", "web"]]
JAVASCRIPT
rebuildOrder(packages, ["icons"]);
// [["icons"], ["ui"], ["docs", "web"]]  (tokens and utils did not change)
JAVASCRIPT
planBuilds({ app: ["x"], x: ["y"], y: ["x"] });
// throws Error: "Dependency cycle: x -> y -> x"; error.cycle is ["x", "y", "x"]
// (a cycle may start at any package on it; app is blocked, not on the cycle)

Environment

  • Plain JavaScript in a browser page; the tests call the globals planBuilds and rebuildOrder from plan-builds.js. Both are synchronous.
  • The wording of the error messages is up to you, within the rules above.
  • Running the builds is not part of this problem; running async tasks as their dependencies finish is problem 14.9.

Hints

  1. For each package, what number tells you it is ready? What has to happen to that number when a stage is built?
  2. When no package is ready but some are left, every one of them waits on another one that is left. What do you find if you keep following those?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…