From 0ba15b7862c29f9dcfb61f2d4ae66d08ad30cad6 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Sat, 24 Oct 2026 16:11:36 -0500 Subject: [PATCH] fix(graph): report a self-dependency as the cycle it is (GitHub #43) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A node that needs the token it provides is a cycle of one, so say so in the same words as every other cycle (`cycle detected: a -> a`), and name the token and the fix: cycle detected: auth -> auth (it needs 'user', which only it provides). Remove 'user' from its needs, or move the work that produces 'user' into a step it needs. "missing dependency … it provides it itself" pointed the reader at a missing producer, when the producer is there and the problem is the loop. Signed-off-by: Diego Resendez --- CHANGELOG.md | 5 +++-- src/graph.ts | 12 ++++++++---- test/graph.test.ts | 2 +- test/one-producer.test.ts | 4 ++-- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a353098..2cddc26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -118,8 +118,9 @@ npm treats versions as semver and semver forbids leading zeros. - **A node that needs the token it provides fails `createApp`** (GitHub #43). The graph check skipped a dependency whose producer was the node itself, so a step with `needs: ['user']` and `provides: ['user']` booted, and at request time read a value nobody had written. It now fails - like any other missing dependency: `missing dependency: user needed by auth — it provides 'user' - itself`. Since a token has one producer (GitHub #35), no other node can supply it; only a seeded + as the cycle it is, naming the token and the fix: `cycle detected: auth -> auth (it needs 'user', + which only it provides). Remove 'user' from its needs, or move the work that produces 'user' into + a step it needs.` Since a token has one producer (GitHub #35), no other node can supply it; only a seeded token (`req`, `params`) still satisfies a node's own need, because the node reads the seed. `overrides` do not exempt the node: they replace its runner after the graph is checked. To migrate, remove the token from the node's `needs`, or move the work that produces it into a diff --git a/src/graph.ts b/src/graph.ts index ee6c836..7772f05 100644 --- a/src/graph.ts +++ b/src/graph.ts @@ -88,11 +88,15 @@ export function topoSort(nodes: GraphNode[], seedKeys: string[], note = ''): Gra if (!producer) throw new Error(`missing dependency: ${key} needed by ${node.name}${note}`); - // A node is not its own dependency (GitHub #43): it would read a value nobody wrote yet. - // `producers` already allows only one producer per token, so no other node can supply it; - // only a seed makes the need real, and the seed case returned above. + // A node that needs its own token is a cycle of one (GitHub #43): it would read a value nobody + // wrote yet. `producers` already allows one producer per token, so no other node can supply + // it; only a seed makes the need real, and the seed case continued above. Same wording as the + // cycle check below, so both read as the one error they are. if (producer === node) { - throw new Error(`missing dependency: ${key} needed by ${node.name} — it provides '${key}' itself${note}`); + throw new Error( + `cycle detected: ${node.name} -> ${node.name} (it needs '${key}', which only it provides)${note}. ` + + `Remove '${key}' from its needs, or move the work that produces '${key}' into a step it needs.`, + ); } } } diff --git a/test/graph.test.ts b/test/graph.test.ts index 73c5014..c915d41 100644 --- a/test/graph.test.ts +++ b/test/graph.test.ts @@ -57,7 +57,7 @@ describe('one producer per token', () => { describe('self-dependency', () => { it('counts as missing when the token is not seeded', () => { expect(() => topoSort([node('auth', ['user'], ['user'])], [])).toThrow( - /missing dependency: user needed by auth — it provides 'user' itself/, + /cycle detected: auth -> auth \(it needs 'user', which only it provides\)/, ); }); diff --git a/test/one-producer.test.ts b/test/one-producer.test.ts index d07b445..7e479dd 100644 --- a/test/one-producer.test.ts +++ b/test/one-producer.test.ts @@ -88,9 +88,9 @@ describe('one producer per token', () => { // check that rejects the node runs before any override could. it('a node that needs the token it provides fails boot, with or without an override', () => { const loop = adds('loop', { kind: 'step', name: 'loop', needs: ['loop'], provides: ['loop'] }); - expect(() => createApp({ modules: [AppModule], plugins: [loop] })).toThrow(/missing dependency: loop needed by loop/); + expect(() => createApp({ modules: [AppModule], plugins: [loop] })).toThrow(/cycle detected: loop -> loop/); expect(() => createApp({ modules: [AppModule], plugins: [loop], overrides: { loop: 1 } })).toThrow( - /missing dependency: loop needed by loop/, + /cycle detected: loop -> loop/, ); });