Skip to content

Commit 6a25a87

Browse files
committed
Merge remote-tracking branch 'origin/next' into claude/project-thread-x1ore2-4557
2 parents 63a0fdf + 9034647 commit 6a25a87

11 files changed

Lines changed: 221 additions & 49 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
In the production build, `getContext` (and so `useContext`) with no owner now reads as if under an empty root: it returns the context's default, and throws `ContextNotFoundError` when there is none. Before, it threw `NoOwnerError` even when the context had a default, so a component created outside any owner (JSX at module scope, in a timer or event handler, or after an `await`) crashed in production while dev and observe, whose `createComponent` runs each component in a root, returned the default. Dev still throws `NoOwnerError` for a bare read with no owner. Reads under an owner are unchanged.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
A chain's verdict no longer flips to `false` mid-hold where a diamond's stays `true` (#4562). When a superseded flight's error is staged in a hold (D-41), the settle walk now marks every derivation it fully settles with the staging, not only a join it reaches a second time, so `isPending` stays `true` until the commit whatever the graph shape (F-9, F-2). The mark moved behind the verdict engine's hooks (`installVerdictHooks`), so builds without `isPending`/`latest` carry none of it.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
Internal: the snapshot walk drops a redundant overlay guard before materializing a pending backing (`materializePB` already returns the backing unchanged without an overlay). No behavior change.

‎packages/signals/src/core/async.ts‎

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
CONFIG_STAGED_ERROR,
1111
CONFIG_SYNC,
1212
CONFIG_VERDICT,
13-
EFFECT_TRACKED,
1413
EFFECT_USER,
1514
NOT_PENDING,
1615
REACTIVE_DIRTY,
@@ -55,6 +54,7 @@ import {
5554
txOf,
5655
freshHook,
5756
holdWorkHook,
57+
stageMarkHook,
5858
guessing,
5959
laneStageHook,
6060
storeParkHook
@@ -255,32 +255,6 @@ export function stageError(el: Computed<any>, error: any, inPass?: boolean): boo
255255
return true;
256256
}
257257

258-
/** D-41: a derivation of a staged error carries it, staged with the same
259-
* frame — marked, not re-run, as a published error marks it (a re-run would
260-
* replace the frame its content owns) — and its own verdict readers
261-
* re-derive (the settle walk). Only a verdict reads a derivation's staging
262-
* before its commit, so only the verdict engine marks. Not a boundary's
263-
* output, an async memo, an effect or a boundary's tree (`EFFECT_TRACKED <<
264-
* 23` spans CONFIG_FRAME and every effect type), nor one already carrying a
265-
* staged error: the commit's publish reaches them. A pending mark supersedes
266-
* the staging (`propagateStatus`). */
267-
function stageMark(node: Computed<any>, el: Computed<any>): void {
268-
if (
269-
!(
270-
node._config &
271-
(CONFIG_REDERIVE | CONFIG_STAGED_ERROR | CONFIG_ASYNC | (EFFECT_TRACKED << 23))
272-
)
273-
) {
274-
node._pendingValue = el._pendingValue;
275-
node._config |= CONFIG_STAGED_ERROR;
276-
queuePendingNode(node);
277-
// A derived store's readers are its family's (store/projection.ts): they
278-
// re-derive from the staging, as from the derive's status (#4189).
279-
(node as any)._fam?.st(STATUS_ERROR);
280-
settlePendingSource(node);
281-
}
282-
}
283-
284258
export function settleErroredDependents(el: Computed<any>, error: any): void {
285259
let scheduled = false;
286260
walkDependents(el, (node, seen) => {
@@ -376,12 +350,10 @@ export function settlePendingSource(el: Computed<any>, source: Computed<any> = e
376350
// equal to the committed value notifies nobody — the source settling
377351
// is its verdict changing: it runs again. (Verdict readers exist only
378352
// with the verdict engine installed.)
379-
// D-41: a derivation of a staged error carries it (`stageMark`).
353+
// D-41: a derivation of a staged error carries it (verdict.ts `stageMark`).
380354
return (
381355
holdWorkHook &&
382-
(node._config & CONFIG_VERDICT
383-
? enqueueSub(node)
384-
: el._config & CONFIG_STAGED_ERROR && stageMark(node, el)),
356+
(node._config & CONFIG_VERDICT ? enqueueSub(node) : stageMarkHook!(node, el)),
385357
false
386358
);
387359
node._time = clock;
@@ -417,6 +389,10 @@ export function settlePendingSource(el: Computed<any>, source: Computed<any> = e
417389
// again at release time — deferred so unobserved() can't unlink subs
418390
// lists this walk is still iterating.
419391
if (!node._subs && node._config & CONFIG_AUTO_DISPOSE) (released ??= []).push(node);
392+
// D-41: and a node fully settled here carries the staging as well — a
393+
// chain's single arrival marks what a diamond's second arrival does
394+
// (F-9, F-2: graph shape is not observable, #4562).
395+
stageMarkHook?.(node, el);
420396
}
421397
});
422398

‎packages/signals/src/core/context.ts‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,28 @@ export function createContext<T>(defaultValue?: T, description?: string): Contex
3030
* `useContext` (in `solid-js`), which wraps this primitive. Exposed here for
3131
* cross-package wiring (e.g. hydration-aware context plumbing).
3232
*
33-
* @throws `NoOwnerError` if there's no owner at the time of call.
33+
* @throws `NoOwnerError` in dev if there's no owner at the time of call; production
34+
* reads the default instead (see below).
3435
* @throws `ContextNotFoundError` if a context value has not been set yet.
3536
*
3637
* @internal
3738
*/
3839
export function getContext<T>(context: Context<T>, owner: Owner | null = getOwner()): T {
39-
if (!owner) {
40-
throw new NoOwnerError();
41-
}
42-
4340
// `undefined` alone means unset — a provided `null` is a value (no `??`).
44-
let value = owner._context[context.id] as T | undefined;
41+
let value: T | undefined;
42+
if (owner) value = owner._context[context.id] as T | undefined;
43+
// Dev keeps the loud error. Production reads with no owner as if under an
44+
// empty root: the default, else `ContextNotFoundError`. That is what dev
45+
// gives a component created outside any owner, since dev `createComponent`
46+
// wraps every component in a root; prod `createComponent` does not.
47+
else if (__DEV__) throw new NoOwnerError();
4548
if (value === undefined) value = context.defaultValue;
4649

4750
if (value === undefined) {
4851
// Dev names the context and why it has no value: no provider, or a
4952
// provider that passed `undefined` (the key is set, to `undefined`).
5053
throw __DEV__
51-
? new ContextNotFoundError(context, context.id in owner._context)
54+
? new ContextNotFoundError(context, context.id in owner!._context)
5255
: new ContextNotFoundError();
5356
}
5457

‎packages/signals/src/core/scheduler.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,17 @@ export let observeFlightHook: ((c: Computed<any>, el: Computed<any>) => boolean)
183183
/** D-25's hold work (verdict.ts `holdWork`): on `source` going pending,
184184
* whether verdict reader `n` waits on it instead of re-deriving. */
185185
export let holdWorkHook: ((n: Computed<any>, source: Computed<any>) => unknown) | null = null;
186+
/** D-41's staged-error mark (verdict.ts `stageMark`): the settle walk's
187+
* reached node `n` carries staged error source `el`'s staging. */
188+
export let stageMarkHook: ((n: Computed<any>, el: Computed<any>) => void) | null = null;
186189
export function installVerdictHooks(
187190
observeFlight: NonNullable<typeof observeFlightHook>,
188-
holdWork: NonNullable<typeof holdWorkHook>
191+
holdWork: NonNullable<typeof holdWorkHook>,
192+
stageMark: NonNullable<typeof stageMarkHook>
189193
): void {
190194
observeFlightHook = observeFlight;
191195
holdWorkHook = holdWork;
196+
stageMarkHook = stageMark;
192197
}
193198
/** External-source bridge (wired by enableExternalSource(); undefined while
194199
* no config is active — including after _resetExternalSourceConfig()). */

‎packages/signals/src/core/verdict.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ import {
1717
CONFIG_HELD_FRAME,
1818
CONFIG_INPUTS_PUBLISHED,
1919
CONFIG_OVERRIDE,
20+
CONFIG_REDERIVE,
2021
CONFIG_STAGED_ERROR,
2122
CONFIG_VERDICT,
2223
EFFECT_RENDER,
24+
EFFECT_TRACKED,
2325
NOT_PENDING,
2426
REACTIVE_DISPOSED,
2527
REACTIVE_FRAME_READ,
@@ -51,6 +53,7 @@ import {
5153
unflushedValue,
5254
verdict
5355
} from "./core.js";
56+
import { settlePendingSource } from "./async.js";
5457
import { forgetThrownRead, warnStrictReadUntracked } from "./dev.js";
5558
import { NotReadyError } from "./error.js";
5659
import { link } from "./graph.js";
@@ -666,7 +669,37 @@ function holdWork(n: Computed<any>, source: Computed<any>): unknown {
666669
// Installed at module evaluation — present exactly when something imports
667670
// `latest` or `isPending`. (`read` dispatches to `verdictValue` through
668671
// `verdict`, set while a window is open.)
669-
installVerdictHooks(observeFlight, holdWork);
672+
/** D-41: a derivation of a staged error carries it, staged with the same
673+
* frame — marked, not re-run, as a published error marks it (a re-run would
674+
* replace the frame its content owns) — and its own verdict readers
675+
* re-derive (the settle walk). Only a verdict reads a derivation's staging
676+
* before its commit, so only the verdict engine marks. Not a boundary's
677+
* output, an async memo, an effect or a boundary's tree (`EFFECT_TRACKED <<
678+
* 23` spans CONFIG_FRAME and every effect type), nor one already carrying a
679+
* staged error: the commit's publish reaches them. A pending mark supersedes
680+
* the staging (`propagateStatus`). Installed as `stageMarkHook`: the settle
681+
* walk (`settlePendingSource`) marks every node it reaches from a staged
682+
* error — a diamond's revisited join and a chain's single arrival alike
683+
* (#4562). */
684+
function stageMark(node: Computed<any>, el: Computed<any>): void {
685+
if (
686+
el._config & CONFIG_STAGED_ERROR &&
687+
!(
688+
node._config &
689+
(CONFIG_REDERIVE | CONFIG_STAGED_ERROR | CONFIG_ASYNC | (EFFECT_TRACKED << 23))
690+
)
691+
) {
692+
node._pendingValue = el._pendingValue;
693+
node._config |= CONFIG_STAGED_ERROR;
694+
queuePendingNode(node);
695+
// A derived store's readers are its family's (store/projection.ts): they
696+
// re-derive from the staging, as from the derive's status (#4189).
697+
(node as any)._fam?.st(STATUS_ERROR);
698+
settlePendingSource(node);
699+
}
700+
}
701+
702+
installVerdictHooks(observeFlight, holdWork, stageMark);
670703
/** A store read inside an untracked `isPending` probe found a live mark on
671704
* a record with no node to carry it (store/affects.ts): not final. */
672705
GlobalQueue._witnessMark = () => {

‎packages/signals/src/store/store.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,13 +2747,10 @@ function snapshotWalk(value: any, seen: Map<object, any>, fam: StoreFamily | nul
27472747
const pb = t.pb;
27482748
if (pb !== null) {
27492749
const k = t.k;
2750-
backing =
2751-
inDraft(t) || writeOverride || k === null || !(k._config & CONFIG_HELD)
2752-
? pb
2753-
: stagingReader() !== null
2754-
? asBacking(untrack(() => readNode(k)))
2755-
: t.v;
2756-
if (backing === pb && t.ovl) backing = materializePB(t);
2750+
if (inDraft(t) || writeOverride || k === null || !(k._config & CONFIG_HELD)) backing = pb;
2751+
else if (stagingReader() !== null) backing = asBacking(untrack(() => readNode(k)));
2752+
// materializePB is a no-op (returns pb) without an overlay.
2753+
if (backing === pb) backing = materializePB(t);
27572754
}
27582755
if (optHooks && optRead(t)) backing = optHooks.view(t, backing);
27592756
if (backing === src) break;

‎packages/signals/tests/dist-artifacts.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,70 @@ describe("@solidjs/signals cleanup order per tier", () => {
232232
}
233233
});
234234

235+
describe("@solidjs/signals context read with no owner per tier", () => {
236+
// Dev and observe `createComponent` (solid-js) run every component in a
237+
// root, so a component created with no owner (module-scope JSX, a timer,
238+
// after an `await`) reads its context default there. Prod `createComponent`
239+
// adds no root, so prod `getContext` reads a missing owner as an empty
240+
// root: the default, else `ContextNotFoundError` — what dev gives that
241+
// component. A bare read with no owner and no component stays a
242+
// `NoOwnerError` in dev, the loud tier.
243+
const cores: Record<keyof typeof TIERS, () => Promise<any>> = {
244+
prod: () => import("../dist/prod/index.js"),
245+
observe: () => import("../dist/observe/index.js"),
246+
dev: () => import("../dist/dev.js")
247+
};
248+
for (const tier of ["prod", "observe"] as const) {
249+
test(`${tier}: a component run with no owner reads the context default`, async () => {
250+
const { createContext, getContext, getOwner, untrack } = await cores[tier]();
251+
const Theme = createContext("light");
252+
// The prod `createComponent` shape: `untrack(() => Comp(props))`.
253+
const seen = untrack(() => {
254+
expect(getOwner()).toBeNull();
255+
return getContext(Theme);
256+
});
257+
expect(seen).toBe("light");
258+
});
259+
260+
test(`${tier}: a provided null default is returned as null`, async () => {
261+
const { createContext, getContext } = await cores[tier]();
262+
expect(getContext(createContext(null))).toBeNull();
263+
});
264+
265+
test(`${tier}: with no owner and no default, the read throws ContextNotFoundError`, async () => {
266+
const { createContext, getContext, ContextNotFoundError, NoOwnerError } = await cores[tier]();
267+
const Missing = createContext();
268+
let error: unknown;
269+
try {
270+
getContext(Missing);
271+
} catch (e) {
272+
error = e;
273+
}
274+
expect(error).toBeInstanceOf(ContextNotFoundError);
275+
expect(error).not.toBeInstanceOf(NoOwnerError);
276+
});
277+
}
278+
279+
test("dev: a bare read with no owner throws NoOwnerError", async () => {
280+
const { createContext, getContext, NoOwnerError } = await cores.dev();
281+
expect(() => getContext(createContext("light"))).toThrowError(NoOwnerError);
282+
});
283+
284+
for (const tier of Object.keys(TIERS) as (keyof typeof TIERS)[]) {
285+
test(`${tier}: under an owner, a provided value wins and the default fills the gap`, async () => {
286+
const { createContext, getContext, setContext, createRoot } = await cores[tier]();
287+
const Theme = createContext("light");
288+
const Missing = createContext();
289+
createRoot(() => {
290+
expect(getContext(Theme)).toBe("light");
291+
setContext(Theme, "dark");
292+
expect(getContext(Theme)).toBe("dark");
293+
expect(() => getContext(Missing)).toThrow();
294+
});
295+
});
296+
}
297+
});
298+
235299
describe("@solidjs/signals bundled under `sideEffects: false`", () => {
236300
// #3891: the store half of `affects()` was installed by a bare
237301
// `import "./affects.js"`, which a bundler honouring `sideEffects: false`

‎packages/signals/tests/settle-diamond-revisit.test.ts‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@
44
* re-run, a staged error's mark): sharing the walk with the other dependent
55
* walks (#4560) once skipped it, and the join re-showed its committed value
66
* between the held error's staging and its commit (an extra "false:3").
7-
* Pins `next`'s diamond output. The chain's extra "false:3" is the separate
8-
* graph-shape question (#4562), not pinned here.
7+
* Pins `next`'s diamond output.
8+
*
9+
* The chain (#4562): its join is reached once, and that single arrival used
10+
* to retire the pending state without the staged-error mark, so the view
11+
* re-showed `false:3` mid-hold. F-9: `isPending(D)` stays true while the
12+
* staged answer waits in the hold — no new answer has been committed; F-2:
13+
* the screen keeps `true:3` until the commit; D-41: the derivation of a
14+
* staged error carries it. Graph shape is not observable: the chain shows
15+
* the diamond's frames.
916
*/
1017
import { afterEach, expect, it } from "vitest";
1118
import {
@@ -66,3 +73,43 @@ it("a diamond's join revisited by the settle walk keeps the early-return branch"
6673
expect(log).toEqual(["false:3", "true:3", "E:boomA", "false:9"]);
6774
dispose();
6875
});
76+
77+
it("a chain's single arrival keeps the staged mark, as a diamond's second arrival does (#4562)", async () => {
78+
const [s, setS] = createSignal(0);
79+
const log: unknown[] = [];
80+
let release!: () => void;
81+
const dispose = createRoot(d => {
82+
const A = createMemo(async () => {
83+
const v = s();
84+
await Promise.resolve();
85+
if (v === 2) throw new Error("boomA");
86+
return v;
87+
});
88+
const B = createMemo(() => A() + 1);
89+
const C = createMemo(() => B() + 2);
90+
const D = createMemo(() => C());
91+
const view = createErrorBoundary(
92+
() => `${isPending(D)}:` + D(),
93+
e => `E:${(e() as Error).message}`
94+
);
95+
createRenderEffect(view, v => void log.push(v));
96+
return d;
97+
});
98+
await tick();
99+
setS(1);
100+
flush();
101+
action(function* () {
102+
setS(2);
103+
yield new Promise<void>(r => (release = r));
104+
})();
105+
await tick();
106+
release();
107+
await tick();
108+
await tick();
109+
setS(3);
110+
flush();
111+
await tick();
112+
await tick();
113+
expect(log).toEqual(["false:3", "true:3", "E:boomA", "false:6"]);
114+
dispose();
115+
});

0 commit comments

Comments
 (0)