Skip to content

Svelte 5: Destructuring Assignment to a Signal Produces a Compiled Output that Interferes with Await Keyword #9686

@levibassey

Description

@levibassey

Describe the bug

Below is some svelte 5 code and it's compiled output:

//Svelte 5 code
let user = $state(null)
const load_user = async () => {
		[user] = await fetch_user()
}

//Compiler Output
const load_user = async (_, user, fetch_user) => {
	(() => {
		const tmp = await fetch_user(); //Cannot use keyword await outside async function

		($.set(user, tmp[0]));
		return tmp;
	})();
};

As you can see the compiled code won't work because the wrapping function is not async. This only seems to happen you destructure the result of an await expression.

The compiler should probably add async to the wrapping function

Reproduction

https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAA2VQQU7DMBD8yspCqiNFaS9cQlrBlQMgwa2pquBuwMWxI3vTEkX-O3aCmiIulmZn1jszA6ulQsfy7cB01SDL2UPbspRR30bgTqgIA3amsyJOCiesbGlT6pJk0xpLMNRI4nPfObQeamsaWGTLeZYd3SKqFRJEDGu4cVQRct0pldwFLrDCaEegTHXY_4oq12sBPIH1BoYoKWkbqV3kzpUkmG_wJAp8qYvl7E8X7x2R0WB0LpQUX-vh8r8fA7xYE_PHtUkapsVBnkZyeHx9fsocWak_ZN3zuJaMJ6IidNKYg6wlHlhOtkOfXiqcfc1NHt11i_g9NjeFnuUh2VVei9RZDRrPEIw20iHnFp1RJ7wuxSG9yQZNR_xPWfGDUcy3QzSWQ8mOaG1fshSwqaS6TO4jyoRpSuZ3ybTuU7hdraZew-v_B975H_U64Ro_AgAA

Logs

No response

System Info

System:
    OS: Linux 5.15 Linux Mint 21.2 (Victoria)
    CPU: (4) x64 Intel(R) Core(TM) i5-2520M CPU @ 2.50GHz
    Memory: 3.71 GB / 7.63 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node
    npm: 9.5.1 - ~/.nvm/versions/node/v18.16.0/bin/npm
  npmPackages:
    svelte: ^5.0.0-next.13 => 5.0.0-next.13

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcompilerChanges relating to the compiler

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions