6.6 No import cycles: examples

Rule: §6.6 in the code standards.

What counts

A cycle of any length counts: if A imports B, nothing B imports, directly or through other modules, imports A. Only value imports form edges. An import type is erased at compile time, so it never joins a cycle. Break a cycle by moving the code, by extracting a third module that both sides import, or by merging the two modules.

Under ESM a cycle can throw a ReferenceError when one module reads a binding the other has not initialised yet, and which side runs first depends on the import order.

Good: a leaf extracted to keep the graph acyclic

src/groups/groupState.ts:3-9 explains why the group-load state lives in its own leaf:

// Leaf module (no imports) holding which on-demand tool groups have been
// activated this session. A separate leaf so tool-def modules (e.g.
// tools/playtest.ts) can read load state via isGroupLoaded() WITHOUT importing
// the heavy groups.ts — which would form an import cycle once groups.ts derives
// its tool lookup from catalogue.ts
// (catalogue → tools/playtest → groups → catalogue). groups.ts mutates
// `loadedGroups` directly (shared Set); it is never reassigned.

So src/tools/playtest.ts:8 imports the leaf:

import { isGroupLoaded } from "../groups/groupState.js";

(groupState.ts is also one of the existing module stores listed under B7 for §6.11. The leaf’s place in the graph is the point here.)

Bad: the same read through the heavy module

A sketch, not code in the repo:

// src/tools/playtest.ts
import { isGroupLoaded } from "../groups/groups.js";

src/registration/catalogue.ts:42 imports tools/playtest.ts, and src/groups/groupCatalogue.ts:16 imports registration/catalogue.ts, so this import would close a four-module cycle.

Source


This site uses Just the Docs, a documentation theme for Jekyll.