Repository: sveltejs/svelte (JavaScript)

Issue:
Svelte 5: Store auto-subscriptions cause inconsistent variable shadowing.
### Describe the bug

This is a bit of a corner case:
If there is a variable shadowing a (top-level) store, this will either result in an error or not depending on whether an auto-subscription has been created elsewhere.

### Reproduction

```svelte
<script>
	import { writable } from 'svelte/store';
	const user = writable(0);
	const users = [];
	$: selected = users.find(user => user.id == $user);

	// Remove to get error:
	$: console.log($user);
</script>
```
[Svelte 5](https://svelte-5-preview.vercel.app/#H4sIAAAAAAAACl2PzWrDMAzHX0WIQjMIyc5eXegr7Nr00MVKMTiWkZWOEvLuxcnIYTfp__ETmnHwgTKa64zxPhIavKSENeorlSU_KShhjZkn6Ytyyr34pOcudurHxKIww694vf8EggUG4RGOW6_NykLHr5LtOWaFKZOA3fPV58c_M4OF620VDwYyBeqVHNjNbAYfXbVBzqvUeAfWwqHMKyt22rbwTSM_CZThQQokwmL-mOUWB2oCP6q9dmr3t7DGkZ0fPDk0KhMtt-UNTcpB4ycBAAA=)
[Svelte 4](https://svelte.dev/repl/457d5764ff024091864c0f78da1d9f46?version=4.2.13) - Always errors as I would have expected

### Logs

_No response_

### System Info

```shell
REPL - Svelte v5.0.0-next.100
```


### Severity

annoyance


Discussion:

Patch:
diff --git a/.changeset/afraid-kids-hide.md b/.changeset/afraid-kids-hide.md
new file mode 100644
index 000000000000..890ea86af2ea
--- /dev/null
+++ b/.changeset/afraid-kids-hide.md
@@ -0,0 +1,5 @@
+---
+"svelte": patch
+---
+
+fix: throw error when auto-subscribed store variable shadow by local variable
diff --git a/packages/svelte/src/compiler/phases/2-analyze/index.js b/packages/svelte/src/compiler/phases/2-analyze/index.js
index d9c6dfe64adc..c1ed378ca3da 100644
--- a/packages/svelte/src/compiler/phases/2-analyze/index.js
+++ b/packages/svelte/src/compiler/phases/2-analyze/index.js
@@ -302,8 +302,8 @@ export function analyze_component(root, source, options) {
 					declaration.initial.source.value === 'svelte/store'
 				))
 		) {
-			let is_nested_store_subscription = false;
-			for (const reference of references) {
+			let is_nested_store_subscription_node = undefined;
+			search: for (const reference of references) {
 				for (let i = reference.path.length - 1; i >= 0; i--) {
 					const scope =
 						scopes.get(reference.path[i]) ||
@@ -311,14 +311,17 @@ export function analyze_component(root, source, options) {
 						instance.scopes.get(reference.path[i]);
 					if (scope) {
 						const owner = scope?.owner(store_name);
-						is_nested_store_subscription =
-							!!owner && owner !== module.scope && owner !== instance.scope;
+						if (!!owner && owner !== module.scope && owner !== instance.scope) {
+							is_nested_store_subscription_node = reference.node;
+							break search;
+						}
 						break;
 					}
 				}
 			}
-			if (is_nested_store_subscription) {
-				error(references[0].node, 'illegal-store-subscription');
+
+			if (is_nested_store_subscription_node) {
+				error(is_nested_store_subscription_node, 'illegal-store-subscription');
 			}
 
 			if (options.runes !== false) {


Test patch:
diff --git a/packages/svelte/tests/compiler-errors/samples/store-shadow-scope-2/_config.js b/packages/svelte/tests/compiler-errors/samples/store-shadow-scope-2/_config.js
new file mode 100644
index 000000000000..f320abac21c1
--- /dev/null
+++ b/packages/svelte/tests/compiler-errors/samples/store-shadow-scope-2/_config.js
@@ -0,0 +1,9 @@
+import { test } from '../../test';
+
+export default test({
+	error: {
+		code: 'illegal-store-subscription',
+		message: 'Cannot subscribe to stores that are not declared at the top level of the component',
+		position: [142, 147]
+	}
+});
diff --git a/packages/svelte/tests/compiler-errors/samples/store-shadow-scope-2/main.svelte b/packages/svelte/tests/compiler-errors/samples/store-shadow-scope-2/main.svelte
new file mode 100644
index 000000000000..d7ef9306c634
--- /dev/null
+++ b/packages/svelte/tests/compiler-errors/samples/store-shadow-scope-2/main.svelte
@@ -0,0 +1,8 @@
+<script>
+	import { writable } from 'svelte/store';
+	const user = writable(0);
+	const users = [];
+	$: selected = users.find(user => user.id == $user);
+
+	$: console.log($user);
+</script>
diff --git a/packages/svelte/tests/compiler-errors/samples/store-shadow-scope-3/_config.js b/packages/svelte/tests/compiler-errors/samples/store-shadow-scope-3/_config.js
new file mode 100644
index 000000000000..74e25f3bf176
--- /dev/null
+++ b/packages/svelte/tests/compiler-errors/samples/store-shadow-scope-3/_config.js
@@ -0,0 +1,9 @@
+import { test } from '../../test';
+
+export default test({
+	error: {
+		code: 'illegal-store-subscription',
+		message: 'Cannot subscribe to stores that are not declared at the top level of the component',
+		position: [167, 172]
+	}
+});
diff --git a/packages/svelte/tests/compiler-errors/samples/store-shadow-scope-3/main.svelte b/packages/svelte/tests/compiler-errors/samples/store-shadow-scope-3/main.svelte
new file mode 100644
index 000000000000..4988835dd72a
--- /dev/null
+++ b/packages/svelte/tests/compiler-errors/samples/store-shadow-scope-3/main.svelte
@@ -0,0 +1,8 @@
+<script>
+	import { writable } from 'svelte/store';
+	const user = writable(0);
+	const users = [];
+
+	$: console.log($user);
+	$: selected = users.find(user => user.id == $user);
+</script>