Skip to content

cycle-init-read ​

Code that runs while a module loads reads a binding imported through an import cycle: a top-level const initializer, a top-level call, class X extends Y, a static field or static block, and the module's own functions that this code calls (an async one up to its first await). The message names the cycle.

Why: in a cycle, one module runs before the others have finished loading. Which one depends on which module of the cycle is imported first: the app's entry, a test that imports one file, a worker, a dev server loading native ES modules in another order than the bundle. Whatever that module reads from the others at load isn't initialised yet: a const, let or class throws ReferenceError: Cannot access 'X' before initialization, a var or an enum is undefined. The same code works in one entry and crashes at startup in another, and moving an unrelated import can flip it.

❌ Rejected: two files, renderer.ts and islands.ts, that import each other

ts
// islands.ts
import { ready } from "./renderer.ts";

export const FOG_COLOR = "#223";

export function whenDrawn(): Promise<void> {
  return ready;
}
ts
// renderer.ts
import { FOG_COLOR } from "./islands.ts";

declare function paint(color: string): Promise<void>;

export async function start(): Promise<void> {
  await paint(FOG_COLOR);
}

// Runs at load, and `start` reads FOG_COLOR before its first `await`.
export const ready = start();

safe-ts check on renderer.ts:

text
error[cycle-init-read]: `start()` runs while this module loads, and reads `FOG_COLOR`, a `const` of
islands.ts. That module is in an import cycle with this one (renderer.ts → islands.ts → renderer.ts):
when islands.ts is imported first (by the app, a test, a worker), this module runs before islands.ts
has finished, and reading it throws `ReferenceError: Cannot access 'FOG_COLOR' before initialization`

✅ Accepted: started from the entry, once every module has loaded

ts
// renderer.ts
import { FOG_COLOR } from "./islands.ts";

declare function paint(color: string): Promise<void>;

export function startRenderer(): Promise<void> {
  return paint(FOG_COLOR);
}
ts
// main.ts, the entry
import { startRenderer } from "./renderer.ts";

void startRenderer();

Or break the cycle: move what both files need (FOG_COLOR here) into a module that imports neither. (These examples span files, so they aren't checked like the other pages' examples; the fixture crates/safe_ts_core/tests/fixtures/import_cycles tests them.)

What counts ​

  • Load-time code: the top level of the module (statements, const initializers, export default <expression>), a class's extends, decorators, computed keys, static fields and static blocks, and callbacks run on the spot (an IIFE, new Promise(executor), map, forEach, filter, reduce, find, some, every, flatMap, sort, Array.from). Functions it calls in the same module are followed, through their own calls; an async function's body counts up to its first await (the operand of that await included: await paint(FOG_COLOR) reads it now).
  • Not load-time code: the bodies of functions, methods and arrow functions it only creates (event handlers, onClick = () => X), instance fields, the rest of an async function after its first await, and generator bodies. By the time they run, every module has loaded.
  • What is read: a const, let, class, var, enum or export default <expression> of the other module; or a call of its function when that function reads one of its own module's bindings (defaults() returning a module-level TABLE). Calling an imported function declaration that reads nothing else is fine: function declarations exist before their module runs.
  • The cycle: the imports that stay at runtime. import type, and imports used only as types, are stripped before the code runs, so they don't make a cycle. Re-exports (export { x } from "./x.ts") do, and the binding is followed to the module that declares it.
  • In a Vue component, <script setup> and the template run per component, not at load; the plain <script> block runs at load.

What the rule can't see: a read through a function of another module that the called function calls in turn, a function passed as a value and called at load, dynamic import() and workers (they load later, so they don't form a cycle here), and a cycle through files it doesn't follow (packages in node_modules). It reports every load-time read through a cycle, including one that works today because the entry happens to import the modules in the right order: that order is what changes when a test or another entry imports a file on its own.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra