Skip to content

Commit df2ba6c

Browse files
ryansolidclaude
andauthored
fix(solid,signals): warn about duplicate Solid copies only when they meet (#4370)
* fix(solid): dev duplicate-instance check keyed by major, off v1's Solid$$ Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0117BoWMMJ6aTSA3MvtgJWG2 * fix(solid): warn about duplicate Solid copies only when they meet The dev warning used to fire as soon as a second copy loaded, so isolated copies (a devtools panel in its own root, a Solid 1 copy on the page) warned with nothing wrong (#2549). Copies now share one Solid$$running slot that names the copy whose component is running; a component or useContext() from another copy that runs ownerless inside it warns once. v1's Solid$$ flag is no longer set. Dev only; prod builds carry none of it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0117BoWMMJ6aTSA3MvtgJWG2 * fix(signals): catch reads across duplicate Solid copies The interaction check only saw components and useContext. The common duplicate is a library's precompiled JSX (or a runtime that bundled its own solid-js) whose render effects read the app's props or signals from another copy: the read finds no listener and silently does not track. The duplicate-copy slot moves into @solidjs/signals. In dev every computation run marks its copy, and an untracked signal or store read that finds another copy running warns once, unless both copies render through roots of their own (an app calling into an isolated copy's API). Prod builds carry none of it. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0117BoWMMJ6aTSA3MvtgJWG2 * perf(signals): same-copy nested runs only read the duplicate-copy slot Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_0117BoWMMJ6aTSA3MvtgJWG2 * perf(signals): duplicate-copy checks cost nothing while one copy is loaded Copies now count themselves into one shared record, `Solid$$copies` ({ copies, running }), and skip the running slot and every read check while the count is below 2. The record's shape is a fixed contract that every later version of Solid keeps, so copies of any 2.x+ version still catch each other. Prod builds turn the exported helpers into no-ops. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018LY7F9czoRqdMEvk2QwPr1 * test(solid): reset the copies record through a helper so test-types don't narrow it to never Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_018LY7F9czoRqdMEvk2QwPr1 --------- Co-authored-by: Claude <[email protected]>
1 parent 4d8195d commit df2ba6c

10 files changed

Lines changed: 353 additions & 16 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
The dev "multiple instances of Solid" warning now fires when two copies actually meet (a component or `useContext()` from one copy runs inside a component rendered by another, with no owner) instead of whenever a second copy loads. Isolated copies, such as a devtools panel rendering into its own root or a Solid 1 copy on the same page, no longer warn (#2549). Solid 2 no longer sets v1's `globalThis.Solid$$` flag.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* Dev: duplicate-copy detection.
3+
*
4+
* Two copies of Solid on a page are only a bug when they meet: one copy's
5+
* code runs inside the other's computation or component and finds no owner
6+
* or listener of its own (each copy keeps them in its own module state), so
7+
* it neither reacts nor sees the other's providers. That is the shape of
8+
* every duplicate report on record: a library's precompiled JSX on copy B
9+
* reading the app's props or signals from copy A, a runtime that bundled its
10+
* own solid-js. Copies that never meet (a devtools panel rendering into its
11+
* own root, an isolated widget) are fine at any version, so nothing is
12+
* checked when a copy loads.
13+
*
14+
* Every copy from 2.0 on shares one global record, `Solid$$copies`. It is a
15+
* contract between all future versions of Solid, so its shape never changes:
16+
*
17+
* - `copies`: each copy adds 1 when it loads.
18+
* - `running`: while `copies > 1`, each copy names itself here (an object
19+
* with a `rooted` boolean, compared by identity) around its computations
20+
* and components, and restores the previous value after.
21+
*
22+
* A copy that sees `copies < 2` skips the slot entirely, so a page with one
23+
* copy pays one property read per check. v1 keeps its own load-time `Solid$$`
24+
* check, which this never sets, so neither side warns about the other.
25+
*/
26+
interface SolidCopy {
27+
/** This copy has created a root of its own (it renders by itself). */
28+
rooted: boolean;
29+
}
30+
31+
interface SolidCopies {
32+
copies: number;
33+
running: SolidCopy | undefined;
34+
}
35+
36+
declare global {
37+
var Solid$$copies: SolidCopies | undefined;
38+
}
39+
40+
const COPY: SolidCopy = { rooted: false };
41+
let warned = false;
42+
let shared: SolidCopies;
43+
44+
if (__DEV__) {
45+
shared = globalThis.Solid$$copies ||= { copies: 0, running: undefined };
46+
shared.copies++;
47+
}
48+
49+
function warnOnce(detail: string): void {
50+
if (warned) return;
51+
warned = true;
52+
console.warn(
53+
`You appear to have multiple instances of Solid: ${detail} Make sure every package resolves the same solid-js (e.g. \`resolve.dedupe\` in Vite) and that libraries keep solid-js external.`
54+
);
55+
}
56+
57+
export function markCopyRooted(): void {
58+
COPY.rooted = true;
59+
}
60+
61+
/**
62+
* Marks this copy as the one running; returns the slot's previous value. A
63+
* nested run of the same copy (most runs inside a flush) only reads the slot.
64+
*/
65+
export function enterCopy(): SolidCopy | undefined {
66+
if (!__DEV__ || shared.copies < 2) return COPY;
67+
const prev = shared.running;
68+
if (prev !== COPY) shared.running = COPY;
69+
return prev;
70+
}
71+
72+
export function exitCopy(prev: SolidCopy | undefined): void {
73+
if (prev !== COPY) shared.running = prev;
74+
}
75+
76+
/** @internal Runs `fn` with this copy marked as the one running. */
77+
export function runAsSolidCopy<T>(fn: () => T): T {
78+
if (!__DEV__ || shared.copies < 2) return fn();
79+
const prev = enterCopy();
80+
try {
81+
return fn();
82+
} finally {
83+
exitCopy(prev);
84+
}
85+
}
86+
87+
/** @internal An ownerless component or context read: warns if another copy is running. */
88+
export function checkForeignOwnerless(): void {
89+
if (!__DEV__ || shared.copies < 2) return;
90+
const running = shared.running;
91+
if (running && running !== COPY)
92+
warnOnce(
93+
"a component or useContext() from one copy ran inside a component rendered by another, so it has no owner and will not react or find its providers."
94+
);
95+
}
96+
97+
/**
98+
* An untracked read: warns if another copy's computation is running, unless
99+
* both copies render through roots of their own (an app calling into an
100+
* isolated copy's API, which reads its own state untracked by design).
101+
*/
102+
export function checkForeignRead(): void {
103+
if (!__DEV__ || shared.copies < 2) return;
104+
const running = shared.running;
105+
if (running && running !== COPY && !(running.rooted && COPY.rooted))
106+
warnOnce(
107+
"a signal or store from one copy was read inside a computation of another, so the read is not tracked and will not update."
108+
);
109+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { checkForeignRead, enterCopy, exitCopy } from "./copies.js";
12
import {
23
clearStatus,
34
handleAsync,
@@ -510,6 +511,7 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
510511
prevStrictRead = strictRead;
511512
strictRead = false;
512513
}
514+
const prevCopy = __DEV__ ? enterCopy() : undefined;
513515
tracking = true;
514516
const isStaleEffect = isEffect && isEffect !== EFFECT_USER;
515517
const prevStale = stale;
@@ -579,7 +581,10 @@ export function recompute(el: Computed<any>, create: boolean = false): void {
579581
} finally {
580582
tracking = prevTracking;
581583
if (laneStageHook) laneRoot = prevMount;
582-
if (__DEV__) strictRead = prevStrictRead;
584+
if (__DEV__) {
585+
strictRead = prevStrictRead;
586+
exitCopy(prevCopy);
587+
}
583588
if (isStaleEffect) stale = prevStale;
584589
// Consume the missed-wake latch (#3037, set by insertSubs): a dep write
585590
// landed beneath this pass on a link it had already validated. The wipe
@@ -1829,6 +1834,7 @@ export function pullComputed(owner: Computed<any>, c: Computed<any>): void {
18291834

18301835
export function read<T>(el: Signal<T> | Computed<T>): T {
18311836
if (__DEV__) DEV.hooks.onRead?.(el);
1837+
if (__DEV__ && !tracking) checkForeignRead();
18321838
if (__DEV__ && asyncTailFlights !== 0 && !tracking && untrackDepth === 0)
18331839
checkPostAwaitRead(
18341840
el,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { markCopyRooted } from "./copies.js";
12
import {
23
CONFIG_AUTO_DISPOSE,
34
CONFIG_CHILDREN_FORBIDDEN,
@@ -483,6 +484,7 @@ export function createRoot<T>(
483484
options?: { id?: string; transparent?: boolean }
484485
): T {
485486
const owner = createOwner(options);
487+
if (__DEV__) markCopyRooted();
486488
// A bound function, not an arrow: an arrow would share this scope, which
487489
// holds `init`, so keeping `dispose` would keep `init` and all it closes
488490
// over alive (#4095).

‎packages/signals/src/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export {
4040
export { setConsoleFooter } from "./core/index.js";
4141
/** @internal The flush round counter (scheduler.ts): hydration's served pass tells its own round from a later one. */
4242
export { clock } from "./core/index.js";
43+
/** @internal Dev duplicate-copy check: solid-js marks components and context reads. */
44+
export { runAsSolidCopy, checkForeignOwnerless } from "./core/copies.js";
4345
/** @internal The pre-write hook (`setWriteHook` in core/core.ts); the server-components client's deferred hydration. */
4446
/** @internal Plain-signal identities and the dev read watch (dependency edges; the server-components client). */
4547
export { setWriteHook, setSignalIdentities, setReadWatch, signalName } from "./core/index.js";

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import {
8080
import { signalHook } from "../core/identities.js";
8181
import { setSlotUnobserved } from "../core/graph.js";
8282
import { getObserver, getOwner } from "../core/owner.js";
83+
import { checkForeignRead } from "../core/copies.js";
8384
import { insertIntoHeap } from "../core/heap.js";
8485
import {
8586
dirtyQueue,
@@ -1995,6 +1996,7 @@ function warnWriteOutsideSetter(
19951996

19961997
const traps: ProxyHandler<StoreTarget> = {
19971998
get(target, key, receiver) {
1999+
if (__DEV__ && getObserver() === null) checkForeignRead();
19982000
// One typeof gates every brand-symbol compare off the hot string path.
19992001
if (typeof key !== "string") {
20002002
if (key === $TARGET) return target;

‎packages/solid/src/client/component.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
import { untrack, createMemo } from "@solidjs/signals";
2-
import { IS_DEV, IS_OBSERVE, markFlight, observedComponent } from "../client/core.js";
1+
import { untrack, createMemo, runAsSolidCopy } from "@solidjs/signals";
2+
import {
3+
IS_DEV,
4+
IS_OBSERVE,
5+
checkForeignCopy,
6+
markFlight,
7+
observedComponent
8+
} from "../client/core.js";
39
import { _lazyHydrationLookup, sharedConfig, tracing } from "./hydration.js";
410
import type { Element as SolidElement } from "../types.js";
511

@@ -89,6 +95,10 @@ export function createComponent<T extends Record<string, any>>(
8995
name?: string,
9096
loc?: string
9197
): SolidElement {
98+
if (IS_DEV) {
99+
checkForeignCopy();
100+
return runAsSolidCopy(() => observedComponent(Comp, props || ({} as T), name, loc));
101+
}
92102
if (IS_OBSERVE) return observedComponent(Comp, props || ({} as T), name, loc);
93103
return untrack(() => Comp(props || ({} as T)));
94104
}

‎packages/solid/src/client/core.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
createMemo,
33
createRoot,
4+
checkForeignOwnerless,
45
getOwner,
56
untrack,
67
setContext,
@@ -18,6 +19,11 @@ import { FlowComponent, FlowProps } from "./component.js";
1819
// attribution). Dev builds set both; observe builds set only IS_OBSERVE;
1920
// prod neither. IS_DEV implies IS_OBSERVE.
2021
export const IS_DEV = "_SOLID_DEV_" as string | boolean;
22+
23+
/** @internal Dev: an ownerless component or context read while another copy of Solid is running (see @solidjs/signals core/copies.ts). */
24+
export function checkForeignCopy(): void {
25+
if (!getOwner()) checkForeignOwnerless();
26+
}
2127
export const IS_OBSERVE = "_SOLID_OBSERVE_" as string | boolean;
2228
/**
2329
* Brand symbol marking dev-built components for `solid-devtools` /
@@ -172,6 +178,7 @@ export function createContext<T>(defaultValue?: T, options?: EffectOptions): Con
172178
* @description https://v2.solidjs.com/reference/solid-js/components-context/use-context
173179
*/
174180
export function useContext<T>(context: Context<T>): T {
181+
if (IS_DEV) checkForeignCopy();
175182
return getContext(context);
176183
}
177184

‎packages/solid/src/index.ts‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,19 +300,6 @@ export type {
300300
export type { RecoveryEvent, RecoveryLive, RecoveryListener } from "./recovery.js";
301301
export type { ChangeOrigin } from "@solidjs/signals/attribution";
302302

303-
// handle multiple instance check
304-
declare global {
305-
var Solid$$: boolean;
306-
}
307-
308-
if (IS_DEV && globalThis) {
309-
if (!globalThis.Solid$$) globalThis.Solid$$ = true;
310-
else
311-
console.warn(
312-
"You appear to have multiple instances of Solid. This can lead to unexpected behavior."
313-
);
314-
}
315-
316303
// Point-of-pain discovery: the first console report of each diagnostic code
317304
// gains a footer naming the repair skill shipped with this package — see
318305
// console-footer.ts (shared with the server entry).

0 commit comments

Comments
 (0)