Skip to content

fix: untrack $inspect.with and add check for unsafe mutation #16209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-dodos-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: untrack `$inspect.with` and add check for unsafe mutation
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Cannot set prototype of `$state` object
### state_unsafe_mutation

```
Updating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without `$state`
Updating state inside `$derived(...)`, `$inspect(...)` or a template expression is forbidden. If the value should not be reactive, declare it without `$state`
```

This error occurs when state is updated while evaluating a `$derived`. You might encounter it while trying to 'derive' two pieces of state in one go:
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/messages/client-errors/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ See the [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-long

## state_unsafe_mutation

> Updating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without `$state`
> Updating state inside `$derived(...)`, `$inspect(...)` or a template expression is forbidden. If the value should not be reactive, declare it without `$state`

This error occurs when state is updated while evaluating a `$derived`. You might encounter it while trying to 'derive' two pieces of state in one go:

Expand Down
6 changes: 5 additions & 1 deletion packages/svelte/src/internal/client/dev/inspect.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UNINITIALIZED } from '../../../constants.js';
import { snapshot } from '../../shared/clone.js';
import { inspect_effect, validate_effect } from '../reactivity/effects.js';
import { untrack } from '../runtime.js';

/**
* @param {() => any[]} get_value
Expand Down Expand Up @@ -28,7 +29,10 @@ export function inspect(get_value, inspector = console.log) {
}

if (value !== UNINITIALIZED) {
inspector(initial ? 'init' : 'update', ...snapshot(value, true));
var snap = snapshot(value, true);
untrack(() => {
inspector(initial ? 'init' : 'update', ...snap);
});
}

initial = false;
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,12 @@ export function state_prototype_fixed() {
}

/**
* Updating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without `$state`
* Updating state inside `$derived(...)`, `$inspect(...)` or a template expression is forbidden. If the value should not be reactive, declare it without `$state`
* @returns {never}
*/
export function state_unsafe_mutation() {
if (DEV) {
const error = new Error(`state_unsafe_mutation\nUpdating state inside a derived or a template expression is forbidden. If the value should not be reactive, declare it without \`$state\`\nhttps://svelte.dev/e/state_unsafe_mutation`);
const error = new Error(`state_unsafe_mutation\nUpdating state inside \`$derived(...)\`, \`$inspect(...)\` or a template expression is forbidden. If the value should not be reactive, declare it without \`$state\`\nhttps://svelte.dev/e/state_unsafe_mutation`);

error.name = 'Svelte error';
throw error;
Expand Down
6 changes: 4 additions & 2 deletions packages/svelte/src/internal/client/reactivity/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ export function mutate(source, value) {
export function set(source, value, should_proxy = false) {
if (
active_reaction !== null &&
!untracking &&
// since we are untracking the function inside `$inspect.with` we need to add this check
// to ensure we error if state is set inside an inspect effect
(!untracking || (active_reaction.f & INSPECT_EFFECT) !== 0) &&
is_runes() &&
(active_reaction.f & (DERIVED | BLOCK_EFFECT)) !== 0 &&
(active_reaction.f & (DERIVED | BLOCK_EFFECT | INSPECT_EFFECT)) !== 0 &&
!(reaction_sources?.[1].includes(source) && reaction_sources[0] === active_reaction)
) {
e.state_unsafe_mutation();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
},
error: 'state_unsafe_mutation'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
let a = $state(0);

let b = $state(0);

$inspect(a).with((...args)=>{
console.log(...args);
b++;
});
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { flushSync } from 'svelte';
import { test } from '../../test';

export default test({
compileOptions: {
dev: true
},
async test({ assert, target, logs }) {
const [a, b] = target.querySelectorAll('button');
assert.deepEqual(logs, ['init', 0]);
flushSync(() => {
b?.click();
});
assert.deepEqual(logs, ['init', 0]);
flushSync(() => {
a?.click();
});
assert.deepEqual(logs, ['init', 0, 'update', 1]);
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
let a = $state(0);

let b = $state(0);

$inspect(a).with((...args)=>{
console.log(...args);
b;
});
</script>

<button onclick={()=>a++}></button>
<button onclick={()=>b++}></button>