Skip to content

Commit 9034647

Browse files
ryansolidclaudebrenelz
authored
fix(signals): prod getContext with no owner reads the context default (#4586)
In the production build, a component created outside any owner (module-scope JSX, a timer, after an await) threw NoOwnerError from useContext even when the context had a default; dev and observe returned the default because their createComponent wraps each component in a root. Fix it at the read instead of adding a root to prod createComponent: with no owner, prod getContext now reads as if under an empty root (the default, else ContextNotFoundError), matching what dev gives that component. Dev keeps NoOwnerError for a bare no-owner read. createComponent is unchanged, the owned read does the same work, and the minified getContext is a few bytes smaller. Tests port brenelz's no-owner context-default cases (#4565): per-tier cases against the signals prod/observe/dev artifacts, and the observe/dev and prod-under-owner cases in solid's client artifact spec. Claude-Session: https://claude.ai/code/session_0117BoWNSSgtemsSYsaX2hXE Co-authored-by: Claude <[email protected]> Co-authored-by: Brenley Dueck <[email protected]>
1 parent fda83a3 commit 9034647

4 files changed

Lines changed: 111 additions & 7 deletions

File tree

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.

‎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/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/solid/test/dist-client-artifact.spec.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,38 @@ describe("solid-js client artifacts", () => {
8181
expect(read("solid.js")).not.toContain("._name = ");
8282
});
8383

84+
// The prod artifact is left out of the first case on purpose: this file
85+
// resolves @solidjs/signals to its dev build, whose bare no-owner read is a
86+
// `NoOwnerError`. Prod's no-owner context read lives in prod signals and is
87+
// covered against that artifact in signals' tests/dist-artifacts.test.ts.
88+
test.each([
89+
["observe", observe],
90+
["dev", dev]
91+
])("%s: a component created outside any owner reads a context default", (_, artifact) => {
92+
const Theme = artifact.createContext("light");
93+
let seen: unknown;
94+
function Badge() {
95+
seen = artifact.useContext(Theme);
96+
return null;
97+
}
98+
expect(getOwner()).toBeNull();
99+
artifact.createComponent(Badge, {});
100+
expect(seen).toBe("light");
101+
});
102+
103+
test("prod: a component created under an owner runs in that owner, with no root of its own", () => {
104+
let outer: unknown;
105+
let inner: unknown;
106+
prod.createRoot(() => {
107+
outer = getOwner();
108+
prod.createComponent(() => {
109+
inner = getOwner();
110+
return null;
111+
}, {});
112+
});
113+
expect(inner).toBe(outer);
114+
});
115+
84116
test("the three artifacts export the same surface", () => {
85117
expect(Object.keys(observe).sort()).toEqual(Object.keys(prod).sort());
86118
expect(Object.keys(dev).sort()).toEqual(Object.keys(prod).sort());

0 commit comments

Comments
 (0)