Repository: sveltejs/svelte (JavaScript)

Issue:
Svelte 5: destructured $derived.by values are treated as sources
### Describe the bug

Extra `$.get(value)` is added to destructured $derived.by values.

### Reproduction

[REPL](https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAA51RzWrEIBB-lUF6yJKwbXN0k0Cfo-lh10xAmqjouLCI715_lpbSPfXgYb7fUQNb5YaO8ffA1HlHxtmbMaxjdDN5cFfcCNPstLciI4MTVhqaZjXThgRCe0UwwpOjM2Hzcjgl5s4R7iZRzQHGCZqQ4ZmkEhZ3VJTgcLe3I7yeIHZQJQXseSW7jMUSW0IDfCd0VdFDhLzBglZecTlebk1urpZ8Vq8ESa2yM7f-WaRUzGp4_rmcGi6eKHm0EpsUn2NI-liuXWbHIZT24qva6bGrtvzDWx-uKNv2kb__HZD-adeLXCUujJP1GD_iF441seniAQAA)

### Logs

_No response_

### System Info

```shell
Svelte 5.0.0-next.234
```


### Severity

annoyance


Discussion:
[simpler repro](https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAE1WOQQqDMBBFrzIMXUQolXYZY6DnqF2oGSGgNiRRDCF3byyIdPnf-5-ZiIMeySF_RZzbiZDj0xi8og9mD26l0VPO7rPYfifC9VYbL5u58SN5aKGGCBuHO6TqgF2GrIBaAosQODwgFT95FPICUi5dFFm9kmJtUZ0u_LlbF1i3a1Get2dhZNwSxJBEaWT-cPooPWhSyL1dKL3TF59DiCzcAAAA)



Patch:
diff --git a/.changeset/olive-llamas-warn.md b/.changeset/olive-llamas-warn.md
new file mode 100644
index 000000000000..4cfbf1a76368
--- /dev/null
+++ b/.changeset/olive-llamas-warn.md
@@ -0,0 +1,5 @@
+---
+'svelte': patch
+---
+
+fix: properly transform destructured `$derived.by` declarations
diff --git a/packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js b/packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js
index c05c1b60759f..319c7118551b 100644
--- a/packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js
+++ b/packages/svelte/src/compiler/phases/3-transform/client/visitors/VariableDeclaration.js
@@ -173,7 +173,7 @@ export function VariableDeclaration(node, context) {
 					let id;
 					let rhs = value;
 
-					if (init.arguments[0].type === 'Identifier') {
+					if (rune === '$derived' && init.arguments[0].type === 'Identifier') {
 						id = init.arguments[0];
 					} else {
 						id = b.id(context.state.scope.generate('$$d'));


Test patch:
diff --git a/packages/svelte/tests/runtime-runes/samples/destructure-derived-by/_config.js b/packages/svelte/tests/runtime-runes/samples/destructure-derived-by/_config.js
new file mode 100644
index 000000000000..5b090c388f39
--- /dev/null
+++ b/packages/svelte/tests/runtime-runes/samples/destructure-derived-by/_config.js
@@ -0,0 +1,13 @@
+import { flushSync } from 'svelte';
+import { test } from '../../test';
+
+export default test({
+	html: '<button>clicks: 0</button>',
+
+	test({ assert, target }) {
+		const btn = target.querySelector('button');
+
+		flushSync(() => btn?.click());
+		assert.htmlEqual(target.innerHTML, `<button>clicks: 1</button>`);
+	}
+});
diff --git a/packages/svelte/tests/runtime-runes/samples/destructure-derived-by/main.svelte b/packages/svelte/tests/runtime-runes/samples/destructure-derived-by/main.svelte
new file mode 100644
index 000000000000..2b0093590707
--- /dev/null
+++ b/packages/svelte/tests/runtime-runes/samples/destructure-derived-by/main.svelte
@@ -0,0 +1,8 @@
+<script>
+	let count = $state(0);
+	let fn = () => ({ n: count });
+
+	let { n } = $derived.by(fn);
+</script>
+
+<button onclick={() => (count += 1)}>clicks: {n}</button>