kit icon indicating copy to clipboard operation
kit copied to clipboard

Build error after upgrading SvelteKit from #405 to #406: Cannot access 'x' before initialization

Open raduab opened this issue 2 years ago • 3 comments

Describe the bug

This appears to be a circular dependency, but it isn't (at least not in the source code). However, while building the app after upgrading from #405 to #406, a circular dependency occurs. After many hours of debugging and pulling out my hair, I realized that this occurs only when a hook imports one of the variables—removing the import from the hook causes the project to build successfully. As the reproduced example below shows, the variables involved were not interdependent.

This is the error I get:

file://.../.svelte-kit/output/server/chunks/hooks.server.js:1
foo + "bar";
^

ReferenceError: Cannot access 'foo' before initialization

Reproduction

https://github.com/raduab/sveltekit-issue/commit/8117e14cea893dc08e88d75a9fcb0e5ce816c438

Logs

No response

System Info

System:
    OS: Linux 5.15
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 18.10.0 - /usr/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 8.19.2 - /usr/bin/npm
  npmPackages:
    @sveltejs/adapter-cloudflare: 1.0.0-next.38 => 1.0.0-next.38 
    @sveltejs/adapter-static: 1.0.0-next.44 => 1.0.0-next.44 
    @sveltejs/kit: 1.0.0-next.512 => 1.0.0-next.512 
    svelte: ^3.51.0 => 3.51.0

Severity

blocking an upgrade

Additional Information

This prevents us from upgrading SvelteKit from build #405 to the most recent one, which is #512 at the time of writing.

raduab avatar Oct 11 '22 15:10 raduab

Just to be sure, have you gone through the migration guide for upgrading past next.405?

whalderman avatar Oct 12 '22 02:10 whalderman

Yes and I successfully upgraded our project to the newest version which was next.512. I found a workaround in our case by refactoring how the imports were done. However, as you can see above, I could reproduce this on a fresh install of next.512

raduab avatar Oct 13 '22 16:10 raduab

This looks like either a Vite or ESBuild bug not being able to properly resolve the circular file dependency.

dummdidumm avatar Oct 19 '22 08:10 dummdidumm

This seems to have been fixed with the latest versions of the packages. :muscle:

raduab avatar Mar 08 '23 12:03 raduab