Skip to content

Top level await breaks stores as of 5.38.3 #16682

@dangodai

Description

@dangodai

Describe the bug

Using a top level await causes any stores created in the same script to be undefined when accessed in the template. This seems to have been introduced with v5.38.3.

await inside of the template works fine. The order of code in the script does not matter (i.e. awaiting before or after creating the store doesn't matter).

Reproduction

REPL: https://svelte.dev/playground/72abfb8ea2a7418ab7454ef9fee42569?version=5.38.6

Run the following component, and observe the error.

<script>
	import { writable } from 'svelte/store';

	await Promise.resolve();
	const count = writable(0);
</script>

{$count}

Logs

Log taken from loading the above REPL

Uncaught (in promise) ReferenceError: count is not defined

  in <unknown>
  in App.svelte
  in __wrapper.svelte
    $count playground:output:4903
    App playground:output:4918
    template_effect playground:output:3262
    update_reaction playground:output:3756
    update_effect playground:output:3936
    create_effect playground:output:3140
    template_effect playground:output:3262
    flatten playground:output:1533
    template_effect playground:output:3261
    App playground:output:4918
    async_body playground:output:1622
    App playground:output:4907
    __wrapper playground:output:4944
    add_svelte_meta playground:output:602
    __wrapper playground:output:4944
    <anonymous> playground:output:1011
    update_reaction playground:output:3756
    update_effect playground:output:3936
    create_effect playground:output:3140
    branch playground:output:3283
    <anonymous> playground:output:1011
    update_reaction playground:output:3756
    update_effect playground:output:3936
    create_effect playground:output:3140
    block playground:output:3271
    Boundary playground:output:1004
    boundary playground:output:922
    __wrapper playground:output:4943
    unmount playground:output:4470
    update_reaction playground:output:3756
    update_effect playground:output:3936
    create_effect playground:output:3140
    branch playground:output:3283
    unmount playground:output:4458
    update_reaction playground:output:3756
    update_effect playground:output:3936
    create_effect playground:output:3140
    component_root playground:output:3222
    _mount playground:output:4455
    mount playground:output:4402
    <anonymous> playground:output:4987
    <anonymous> about:srcdoc:45
    EventListener.handleEvent* about:srcdoc:30
    <anonymous> about:srcdoc:276

System Info

System:
    OS: Linux 6.15 Arch Linux
    CPU: (12) x64 AMD Ryzen 5 2600 Six-Core Processor
    Memory: 7.74 GB / 15.54 GB
    Container: Yes
    Shell: 5.3.3 - /bin/bash
  Binaries:
    Node: 22.17.0 - ~/.nvm/versions/node/v22.17.0/bin/node
    Yarn: 1.22.22 - /usr/bin/yarn
    npm: 10.9.2 - ~/.nvm/versions/node/v22.17.0/bin/npm
    pnpm: 10.13.1 - ~/.local/share/pnpm/pnpm
  Browsers:
    Chromium: 139.0.7258.66
  npmPackages:
    svelte: ^5.38.6 => 5.38.6

Severity

blocking an upgrade

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions