Repository: sveltejs/svelte (2025-08-25T08:05:21Z)

Issue:
BUG: Variable not defined when awaited and rendered inside a snippet
### Describe the bug

I’m using the experimental `async` flag.

When rendering an awaited variable inside a snippet — without explicitly declaring it in the snippet parameters — the console throws the following error:

```
Uncaught (in promise) ReferenceError: value is not defined
```

I’m currently on **v5.38.3**. This was working correctly in **v5.38.2**.


### Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-ry9pp9kr?file=src%2Froutes%2F%2Bpage.svelte

### Logs

```shell

```

### System Info

```shell
System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 20.19.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    svelte: 5.38.3 => 5.38.3
```

### Severity

blocking an upgrade


Discussion:

Patch:
diff --git a/.changeset/cool-garlics-fail.md b/.changeset/cool-garlics-fail.md
new file mode 100644
index 000000000000..cabff1840d22
--- /dev/null
+++ b/.changeset/cool-garlics-fail.md
@@ -0,0 +1,5 @@
+---
+'svelte': patch
+---
+
+fix: place instance-level snippets inside async body
diff --git a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js
index 940d6a9e004d..bdd7eb3f1799 100644
--- a/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js
+++ b/packages/svelte/src/compiler/phases/3-transform/client/transform-client.js
@@ -362,12 +362,12 @@ export function client_component(analysis, options) {
 		store_init,
 		...store_setup,
 		...legacy_reactive_declarations,
-		...group_binding_declarations,
-		...state.instance_level_snippets
+		...group_binding_declarations
 	]);
 
 	if (analysis.instance.has_await) {
 		const body = b.block([
+			...state.instance_level_snippets,
 			.../** @type {ESTree.Statement[]} */ (instance.body),
 			b.if(b.call('$.aborted'), b.return()),
 			.../** @type {ESTree.Statement[]} */ (template.body)
@@ -375,7 +375,10 @@ export function client_component(analysis, options) {
 
 		component_block.body.push(b.stmt(b.call(`$.async_body`, b.arrow([], body, true))));
 	} else {
-		component_block.body.push(.../** @type {ESTree.Statement[]} */ (instance.body));
+		component_block.body.push(
+			...state.instance_level_snippets,
+			.../** @type {ESTree.Statement[]} */ (instance.body)
+		);
 
 		if (!analysis.runes && analysis.needs_context) {
 			component_block.body.push(b.stmt(b.call('$.init', analysis.immutable ? b.true : undefined)));


Test patch:
diff --git a/packages/svelte/tests/runtime-runes/samples/async-reference-in-snippet/_config.js b/packages/svelte/tests/runtime-runes/samples/async-reference-in-snippet/_config.js
new file mode 100644
index 000000000000..c6903c3eed32
--- /dev/null
+++ b/packages/svelte/tests/runtime-runes/samples/async-reference-in-snippet/_config.js
@@ -0,0 +1,9 @@
+import { tick } from 'svelte';
+import { test } from '../../test';
+
+export default test({
+	async test({ assert, target }) {
+		await tick();
+		assert.htmlEqual(target.innerHTML, 'value');
+	}
+});
diff --git a/packages/svelte/tests/runtime-runes/samples/async-reference-in-snippet/app.svelte b/packages/svelte/tests/runtime-runes/samples/async-reference-in-snippet/app.svelte
new file mode 100644
index 000000000000..27b29cfe5039
--- /dev/null
+++ b/packages/svelte/tests/runtime-runes/samples/async-reference-in-snippet/app.svelte
@@ -0,0 +1,9 @@
+<script>
+	const value = await 'value';
+</script>
+
+{#snippet valueSnippet()}
+	{value}
+{/snippet}
+
+{@render valueSnippet()}
\ No newline at end of file
diff --git a/packages/svelte/tests/runtime-runes/samples/async-reference-in-snippet/main.svelte b/packages/svelte/tests/runtime-runes/samples/async-reference-in-snippet/main.svelte
new file mode 100644
index 000000000000..c251a5645b7a
--- /dev/null
+++ b/packages/svelte/tests/runtime-runes/samples/async-reference-in-snippet/main.svelte
@@ -0,0 +1,8 @@
+<script>
+    import App from './app.svelte';
+</script>
+<svelte:boundary>
+    {#snippet pending()}
+    {/snippet}
+    <App />
+</svelte:boundary>
\ No newline at end of file