Skip to content

Commit 8ff2af5

Browse files
fix: show filename information in legacy_recursive_reactive_block (#13764)
1 parent 41d61c7 commit 8ff2af5

File tree

7 files changed

+43
-6
lines changed

7 files changed

+43
-6
lines changed

.changeset/witty-flies-impress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"svelte": patch
3+
---
4+
5+
fix: show filename information in `legacy_recursive_reactive_block`

documentation/docs/98-reference/.generated/client-warnings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ The `render` function passed to `createRawSnippet` should return HTML for a sing
5959
### legacy_recursive_reactive_block
6060

6161
```
62-
Detected a migrated `$:` reactive block that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.
62+
Detected a migrated `$:` reactive block in `%filename%` that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.
6363
```
6464

6565
### lifecycle_double_unmount

packages/svelte/messages/client-warnings/warnings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The easiest way to log a value as it changes over time is to use the [`$inspect`
3838
3939
## legacy_recursive_reactive_block
4040

41-
> Detected a migrated `$:` reactive block that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.
41+
> Detected a migrated `$:` reactive block in `%filename%` that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.
4242
4343
## lifecycle_double_unmount
4444

packages/svelte/src/internal/client/warnings.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ export function invalid_raw_snippet_render() {
100100
}
101101

102102
/**
103-
* Detected a migrated `$:` reactive block that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.
103+
* Detected a migrated `$:` reactive block in `%filename%` that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.
104+
* @param {string} filename
104105
*/
105-
export function legacy_recursive_reactive_block() {
106+
export function legacy_recursive_reactive_block(filename) {
106107
if (DEV) {
107-
console.warn(`%c[svelte] legacy_recursive_reactive_block\n%cDetected a migrated \`$:\` reactive block that both accesses and updates the same reactive value. This may cause recursive updates when converted to an \`$effect\`.`, bold, normal);
108+
console.warn(`%c[svelte] legacy_recursive_reactive_block\n%cDetected a migrated \`$:\` reactive block in \`${filename}\` that both accesses and updates the same reactive value. This may cause recursive updates when converted to an \`$effect\`.`, bold, normal);
108109
} else {
109110
// TODO print a link to the documentation
110111
console.warn("legacy_recursive_reactive_block");

packages/svelte/src/legacy/legacy-client.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import { hydrate, mount, unmount } from '../internal/client/render.js';
66
import {
77
active_effect,
88
component_context,
9+
dev_current_component_function,
910
flush_sync,
1011
get,
1112
set_signal_status
1213
} from '../internal/client/runtime.js';
1314
import { lifecycle_outside_component } from '../internal/shared/errors.js';
1415
import { define_property, is_array } from '../internal/shared/utils.js';
1516
import * as w from '../internal/client/warnings.js';
17+
import { DEV } from 'esm-env';
18+
import { FILENAME } from '../constants.js';
1619

1720
/**
1821
* Takes the same options as a Svelte 4 component and the component function and returns a Svelte 4 compatible component.
@@ -182,7 +185,12 @@ export function run(fn) {
182185
var effect = /** @type {import('#client').Effect} */ (active_effect);
183186
// If the effect is immediately made dirty again, mark it as maybe dirty to emulate legacy behaviour
184187
if ((effect.f & DIRTY) !== 0) {
185-
w.legacy_recursive_reactive_block();
188+
let filename = "a file (we can't know which one)";
189+
if (DEV) {
190+
// @ts-ignore
191+
filename = dev_current_component_function?.[FILENAME] ?? filename;
192+
}
193+
w.legacy_recursive_reactive_block(filename);
186194
set_signal_status(effect, MAYBE_DIRTY);
187195
}
188196
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
compileOptions: {
5+
dev: true
6+
},
7+
async test({ warnings, assert }) {
8+
assert.deepEqual(warnings, [
9+
'Detected a migrated `$:` reactive block in `main.svelte` that both accesses and updates the same reactive value. This may cause recursive updates when converted to an `$effect`.'
10+
]);
11+
}
12+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { run } from 'svelte/legacy';
3+
4+
let count = $state(0);
5+
6+
run(() => {
7+
count = count+1;
8+
});
9+
</script>
10+
11+
{count}

0 commit comments

Comments
 (0)