sveltejs / kit

web development, streamlined
https://svelte.dev/docs/kit
MIT License
18.77k stars 1.96k forks source link

Builds break after upgrading to SvelteKit 2 #11434

Open jhechtf opened 11 months ago

jhechtf commented 11 months ago

Describe the bug

I have a project using svelte kit with Drizzle ORM and Supabase. The whole project is open source and can be found here. I recently updated everything to sveltekit 2, and since then my builds have been broken.

Reproduction

https://github.com/jhechtf/vercel-adapter-breaking - only using PNPM

https://github.com/sungmanito/mono/commit/5cc6e7326c28ab95ae363e4aad59415077adafce is where pnpm build stops working, giving these errors

> Using @sveltejs/adapter-vercel
X [ERROR] Top-level await is not available in the configured target environment ("es2020")

    .svelte-kit/output/server/chunks/client.js:161:0:
      161 │ await pool.connect();
          ╵ ~~~~~

X [ERROR] Could not resolve "net"

    node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/connection.js:3:18:
      3 │ var net = require('net')
        ╵                   ~~~~~

  The package "net" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "events"

    node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/client.js:3:27:
      3 │ var EventEmitter = require('events').EventEmitter
        ╵                            ~~~~~~~~

  The package "events" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "events"

    node_modules/.pnpm/pg-pool@3.6.1_pg@8.11.3/node_modules/pg-pool/index.js:2:29:
      2 │ const EventEmitter = require('events').EventEmitter
        ╵                              ~~~~~~~~

  The package "events" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "events"

    node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/native/client.js:12:27:
      12 │ var EventEmitter = require('events').EventEmitter
         ╵                            ~~~~~~~~

  The package "events" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "events"

    node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/connection.js:4:27:
      4 │ var EventEmitter = require('events').EventEmitter
        ╵                            ~~~~~~~~

  The package "events" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "assert"

    node_modules/.pnpm/pg-protocol@1.6.0/node_modules/pg-protocol/dist/parser.js:9:41:
      9 │ const assert_1 = __importDefault(require("assert"));
        ╵                                          ~~~~~~~~

  The package "assert" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "util"

    node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/native/client.js:13:19:
      13 │ var util = require('util')
         ╵                    ~~~~~~

  The package "util" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "net"

    node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/stream.js:6:22:
      6 │   const net = require('net')
        ╵                       ~~~~~

  The package "net" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "tls"

    node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/stream.js:21:20:
      21 │   var tls = require('tls')
         ╵                     ~~~~~

  The package "tls" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "path"

    node_modules/.pnpm/pgpass@1.0.5/node_modules/pgpass/lib/index.js:3:19:
      3 │ var path = require('path')
        ╵                    ~~~~~~

  The package "path" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "dns"

    node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/connection-parameters.js:3:18:
      3 │ var dns = require('dns')
        ╵                   ~~~~~

  The package "dns" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "events"

    node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/native/query.js:3:27:
      3 │ var EventEmitter = require('events').EventEmitter
        ╵                            ~~~~~~~~

  The package "events" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "events"

    node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/query.js:3:33:
      3 │ const { EventEmitter } = require('events')
        ╵                                  ~~~~~~~~

  The package "events" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "crypto"

    node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/crypto/utils-legacy.js:5:27:
      5 │ const nodeCrypto = require('crypto')
        ╵                            ~~~~~~~~

  The package "crypto" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "crypto"

    node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/crypto/utils-webcrypto.js:1:27:
      1 │ const nodeCrypto = require('crypto')
        ╵                            ~~~~~~~~

  The package "crypto" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "fs"

    node_modules/.pnpm/pgpass@1.0.5/node_modules/pgpass/lib/index.js:4:17:
      4 │   , fs = require('fs')
        ╵                  ~~~~

  The package "fs" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "fs"

    node_modules/.pnpm/pg-connection-string@2.6.2/node_modules/pg-connection-string/index.js:76:77:
      76 │   const fs = config.sslcert || config.sslkey || config.sslrootcert ? require('fs') : null
         ╵                                                                              ~~~~

  The package "fs" wasn't found on the file system but is built into node. Are you trying to bundle
  for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "util"

    node_modules/.pnpm/pg@8.11.3/node_modules/pg/lib/native/query.js:4:19:
      4 │ var util = require('util')
        ╵                    ~~~~~~

  The package "util" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "path"

    node_modules/.pnpm/pgpass@1.0.5/node_modules/pgpass/lib/helper.js:3:19:
      3 │ var path = require('path')
        ╵                    ~~~~~~

  The package "path" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "stream"

    node_modules/.pnpm/pgpass@1.0.5/node_modules/pgpass/lib/helper.js:4:21:
      4 │   , Stream = require('stream').Stream
        ╵                      ~~~~~~~~

  The package "stream" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "util"

    node_modules/.pnpm/pgpass@1.0.5/node_modules/pgpass/lib/helper.js:6:19:
      6 │   , util = require('util')
        ╵                    ~~~~~~

  The package "util" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "stream"

    node_modules/.pnpm/split2@4.2.0/node_modules/split2/index.js:19:30:
      19 │ const { Transform } = require('stream')
         ╵                               ~~~~~~~~

  The package "stream" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

X [ERROR] Could not resolve "buffer"

    node_modules/.pnpm/safe-buffer@5.2.1/node_modules/safe-buffer/index.js:3:21:
      3 │ var buffer = require('buffer')
        ╵                      ~~~~~~~~

  The package "buffer" wasn't found on the file system but is built into node. Are you trying to
  bundle for node? You can use "platform: 'node'" to do that, which will remove this error.

Logs

N/A, this occurs during build time. `pnpm dev` works just fine.

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor
    Memory: 14.56 GB / 31.93 GB
  Binaries:
    Node: 20.10.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.5 - C:\Program Files (x86)\Yarn\bin\yarn.CMD      
    npm: 10.2.3 - C:\Program Files\nodejs\npm.CMD
    pnpm: 8.12.1 - ~\AppData\Roaming\npm\pnpm.CMD
  Browsers:
    Chrome: 120.0.6099.110
    Edge: Chromium (120.0.2210.77)
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.0 => 3.0.0
    @sveltejs/adapter-vercel: ^3.1.0 => 3.1.0
    @sveltejs/kit: ^2.0.3 => 2.0.3
    @sveltejs/vite-plugin-svelte: ^3.0.1 => 3.0.1
    svelte: ^4.2.8 => 4.2.8
    vite: ^5.0.10 => 5.0.10


### Severity

blocking all usage of SvelteKit

### Additional Information

On my local I've moved from using the `postgres` package to the `pg` package for connecting Drizzle to my database. Pg requires the `.connect()` to work.  In a PR (https://github.com/sungmanito/mono/pull/19) I reverted the commit that updated to sveltekit 2 and the vercel deployment works correctly, even with the `await client.connect();`

![Working Vercel Deployment with ](https://github.com/sveltejs/kit/assets/8152471/d06c4c3e-017c-47af-983d-09179d2056a1)

Preview build was tested and works as expected.
alex-way commented 11 months ago

You need to provide a confined reproducible repl, you can't just provide your entire app and expect the svelte team to troubleshoot for you 😢

You've even said yourself the issues look related to the pg package? Have you tried to migrate back to the postgres package and see if that works?

jhechtf commented 11 months ago

what caused me to try the move to pg was the fact that it was happening with the postgres package.

Also stated is that reverting the upgrade to sveltekit2, even with the pg package, deploys to vercel successfully, as linked in the issue.

jhechtf commented 11 months ago

Went to make a more refined down example repo, and ran into something different but possibly still connected, documenting here.

  1. Since this is a build-time issue, using the stackblitz or whatever it's called ends up hanging after calling npm run build. Like it says it transforms some modules, but hangs the terminal after that, no idea why as nothing ever gets displayed.
  2. Because of (1) I decided to make a quick repo to test it out locally. Same setup i had: drizzle-orm, pg, and some generic queries to a postgres db to make the npm run dev command happy, and to actually use the db client in case that provided me a false positive. Installed @sveltejs/adapter-vercel, set it up on the svelte.config.js and ran npm run build and it worked perfectly
  3. (2) was confusing, so i deleted the node modules and instead installed using pnpm (which is what i normally use) and ran pnpm build. This failed with the following errors:
> Using @sveltejs/adapter-vercel
  Warning: The following modules failed to locate dependencies that may (or may not) be required for your app to work:
  node_modules\.pnpm\pg@8.11.3\node_modules\pg\lib\native\client.js
    - pg-native
error during build:
Error: EPERM: operation not permitted, symlink '.pnpm\pg@8.11.3\node_modules\pg' -> 'C:\Users\Jim\projects\vercel-adapter-breaking\.vercel\output\functions\fn.func\node_modules\pg'
    at Object.symlinkSync (node:fs:1801:11)
    at create_function_bundle (file:///C:/Users/Jim/projects/vercel-adapter-breaking/node_modules/.pnpm/@sveltejs+adapter-vercel@4.0.3_@sveltejs+kit@2.0.6/node_modules/@sveltejs/adapter-vercel/index.js:524:7)
    at async generate_serverless_function (file:///C:/Users/Jim/projects/vercel-adapter-breaking/node_modules/.pnpm/@sveltejs+adapter-vercel@4.0.3_@sveltejs+kit@2.0.6/node_modules/@sveltejs/adapter-vercel/index.js:80:5)
    at async adapt (file:///C:/Users/Jim/projects/vercel-adapter-breaking/node_modules/.pnpm/@sveltejs+adapter-vercel@4.0.3_@sveltejs+kit@2.0.6/node_modules/@sveltejs/adapter-vercel/index.js:246:5)
    at async adapt (file:///C:/Users/Jim/projects/vercel-adapter-breaking/node_modules/.pnpm/@sveltejs+kit@2.0.6_@sveltejs+vite-plugin-svelte@3.0.1_svelte@4.2.8_vite@5.0.10/node_modules/@sveltejs/kit/src/core/adapt/index.js:37:2)
    at async finalise (file:///C:/Users/Jim/projects/vercel-adapter-breaking/node_modules/.pnpm/@sveltejs+kit@2.0.6_@sveltejs+vite-plugin-svelte@3.0.1_svelte@4.2.8_vite@5.0.10/node_modules/@sveltejs/kit/src/exports/vite/index.js:825:7)
    at async Object.handler (file:///C:/Users/Jim/projects/vercel-adapter-breaking/node_modules/.pnpm/@sveltejs+kit@2.0.6_@sveltejs+vite-plugin-svelte@3.0.1_svelte@4.2.8_vite@5.0.10/node_modules/@sveltejs/kit/src/exports/vite/index.js:855:5)
    at async PluginDriver.hookParallel (file:///C:/Users/Jim/projects/vercel-adapter-breaking/node_modules/.pnpm/rollup@4.9.1/node_modules/rollup/dist/es/shared/node-entry.js:18561:17)
    at async Object.close (file:///C:/Users/Jim/projects/vercel-adapter-breaking/node_modules/.pnpm/rollup@4.9.1/node_modules/rollup/dist/es/shared/node-entry.js:19491:13)
    at async build (file:///C:/Users/Jim/projects/vercel-adapter-breaking/node_modules/.pnpm/vite@5.0.10/node_modules/vite/dist/node/chunks/dep-R0I0XnyH.js:66825:13)
 ELIFECYCLE  Command failed with exit code 1.
  1. So being a man of science I decided to try removing my current node modules in the project I see the original errors in and install using npm. Tried running both the version ^3.0.0 of the @sveltejs/adapter-vercel and 4.0.3, both fail with the same errors.
solanacryptodev commented 11 months ago

Were you able to resolve this? I'm running into a similar issue with some of the same packages in a SvelteKit app for a crypto application. Wanting to use Vercel, but running into build failures. My project actually begins the build process and gets far along until it hits @sveltejs/adapter-vercel then it fails. I'm going to open a separate Issue.

jhechtf commented 11 months ago

@solanacryptodev i was not. Are you also using pnpm?

eltigerchino commented 11 months ago

Please provide a minimal reproduction in the form of a code repository.

jhechtf commented 11 months ago

@eltigerchino For the second set of logs, https://github.com/jhechtf/vercel-adapter-breaking only breaks when using Pnpm. However, this is not the original set of errors I posted in regards to seeing the issue.

eltigerchino commented 1 week ago

I can't reproduce the issue given the reproduction. Please provide another minimal reproduction.

~/github/vercel-adapter-breaking main 7s
❯ npm run build 

> vercel-adapter-breaking@0.0.1 build
> vite build

vite v5.0.10 building SSR bundle for production...
✓ 76 modules transformed.
"DB_URL" is not exported by "virtual:$env/static/private", imported by "src/lib/server/client.ts".
file: /home/chewteeming/github/vercel-adapter-breaking/src/lib/server/client.ts:1:9
1: import { DB_URL } from '$env/static/private';
            ^
2: import { drizzle } from 'drizzle-orm/node-postgres';
3: import * as schema from "./schema";
error during build:
RollupError: "DB_URL" is not exported by "virtual:$env/static/private", imported by "src/lib/server/client.ts".
    at error (file:///home/chewteeming/github/vercel-adapter-breaking/node_modules/rollup/dist/es/shared/parseAst.js:337:30)
    at Module.error (file:///home/chewteeming/github/vercel-adapter-breaking/node_modules/rollup/dist/es/shared/node-entry.js:12755:16)
    at Module.traceVariable (file:///home/chewteeming/github/vercel-adapter-breaking/node_modules/rollup/dist/es/shared/node-entry.js:13192:29)
    at ModuleScope.findVariable (file:///home/chewteeming/github/vercel-adapter-breaking/node_modules/rollup/dist/es/shared/node-entry.js:11609:39)
    at Identifier.bind (file:///home/chewteeming/github/vercel-adapter-breaking/node_modules/rollup/dist/es/shared/node-entry.js:7193:40)
    at Property.bind (file:///home/chewteeming/github/vercel-adapter-breaking/node_modules/rollup/dist/es/shared/node-entry.js:4639:23)
    at ObjectExpression.bind (file:///home/chewteeming/github/vercel-adapter-breaking/node_modules/rollup/dist/es/shared/node-entry.js:4635:28)
    at NewExpression.bind (file:///home/chewteeming/github/vercel-adapter-breaking/node_modules/rollup/dist/es/shared/node-entry.js:4635:28)
    at VariableDeclarator.bind (file:///home/chewteeming/github/vercel-adapter-breaking/node_modules/rollup/dist/es/shared/node-entry.js:4639:23)
    at VariableDeclaration.bind (file:///home/chewteeming/github/vercel-adapter-breaking/node_modules/rollup/dist/es/shared/node-entry.js:4635:28)

After supplying a random value to .env DB_URL:

~/github/vercel-adapter-breaking main
❯ npm run build

> vercel-adapter-breaking@0.0.1 build
> vite build

vite v5.0.10 building SSR bundle for production...
✓ 76 modules transformed.

node:internal/event_target:1016
  process.nextTick(() => { throw err; });
                           ^
Error: getaddrinfo EAI_AGAIN base
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:108:26)
Emitted 'error' event on Worker instance at:
    at [kOnErrorMessage] (node:internal/worker:300:10)
    at [kOnMessage] (node:internal/worker:311:37)
    at MessagePort.<anonymous> (node:internal/worker:212:57)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:741:20)
    at exports.emitMessage (node:internal/per_context/messageport:23:28) {
  errno: -3001,
  code: 'EAI_AGAIN',
  syscall: 'getaddrinfo',
  hostname: 'base'
}

Node.js v18.17.1