drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too đŸ˜…
https://orm.drizzle.team
Apache License 2.0
24.33k stars 633 forks source link

[BUG]: running migrations fails on mac due to esbuild issues #1348

Closed ar7casper closed 1 year ago

ar7casper commented 1 year ago

What version of drizzle-orm are you using?

0.28.6

What version of drizzle-kit are you using?

0.19.13

Describe the Bug

I'm using bun 1.0.3 as my everything (runner, package manager, etc.) I'm using a MacBook Pro M1 Max.

I have a drizzle.config.ts file with the following -

import type { Config } from "drizzle-kit";

export default {
  schema: "./src/db/schema/*",
  driver: "sqlite",
  out: "./drizzle",
} satisfies Config;

the command I'm running is - drizzle-kit generate:sqlite

In response, I get an error -

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

Error:
You installed esbuild for another platform than the one you're currently using.
This won't work because esbuild is written with native code and needs to
install a platform-specific binary executable.

Specifically the "@esbuild/darwin-arm64" package is present but this platform
needs the "@esbuild/darwin-x64" package instead. People often get into this
situation by installing esbuild with npm running inside of Rosetta 2 and then
trying to use it with node running outside of Rosetta 2, or vice versa (Rosetta
2 is Apple's on-the-fly x86_64-to-arm64 translation service).

If you are installing with npm, you can try ensuring that both npm and node are
not running under Rosetta 2 and then reinstalling esbuild. This likely involves
changing how you installed npm and/or node. For example, installing node with
the universal installer here should work: https://nodejs.org/en/download/. Or
you could consider using yarn instead of npm which has built-in support for
installing a package on multiple platforms simultaneously.

If you are installing with yarn, you can try listing both "arm64" and "x64"
in your ".yarnrc.yml" file using the "supportedArchitectures" feature:
https://yarnpkg.com/configuration/yarnrc/#supportedArchitectures
Keep in mind that this means multiple copies of esbuild will be present.

Another alternative is to use the "esbuild-wasm" package instead, which works
the same way on all platforms. But it comes with a heavy performance cost and
can sometimes be 10x slower than the "esbuild" package, so you may also not
want to do that.

    at generateBinPath (/Users/alex/dev/sashka/clients/invoices/node_modules/esbuild/lib/main.js:1888:17)
    at esbuildCommandAndArgs (/Users/alex/dev/sashka/clients/invoices/node_modules/esbuild/lib/main.js:1969:33)
    at ensureServiceIsRunning (/Users/alex/dev/sashka/clients/invoices/node_modules/esbuild/lib/main.js:2133:25)
    at startSyncServiceWorker (/Users/alex/dev/sashka/clients/invoices/node_modules/esbuild/lib/main.js:2336:19)
    at Object.<anonymous> (/Users/alex/dev/sashka/clients/invoices/node_modules/esbuild/lib/main.js:2377:3)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

Node.js v18.15.0
error: script "generate" exited with code 1 (SIGHUP)

The weird part is that I can't understand why node is there.

Another thing I checked is what arch I'm using. The output of -

if (process.arch === 'x64') {
  console.log("You're running on Rosetta.");
} else if (process.arch === 'arm64') {
  console.log("You're running natively on Apple Silicon.");
} else {
  console.log("You're running on an architecture other than x64 or arm64.");
}

is "You're running natively on Aplle Silicon".

BUT, in the CLI, when I run node -p "process.arch" the response is x64, meaning Roestta.

Helpful links

https://blog.hao.dev/fixing-esbuild-related-cpu-architecture-error-on-apple-silicon-macs

Expected behavior

For stuff to work.

Environment & setup

Mac Pro M1 max.

ar7casper commented 1 year ago

Found a solution, it has nothing to do with drizzle, but since it happened while working with you, I think it might be a good idea for future devs -

So it's a matter of several moving parts. Involving ZSH, node versions and only then esbulid.

So, first thing I've done was running - node -p "process.arch", the answer was x64. Then, I did nvm uninstall 18 and nvm install 18. Same result. Then I found that it might be a ZSH thing. I ran - arch and the answer was x64. So I did - env /usr/bin/arch -arm64 /bin/zsh --login (https://vineethbharadwaj.medium.com/m1-mac-switching-terminal-between-x86-64-and-arm64-e45f324184d9)

and now arch returned arm64. Next, reinstalled node again nvm uninstall 18 && nvm install 18 and the answer of node -p "process.arch" was arm64. Then I ran my code and the migration worked.

The only thing that's weird is that I'm using bun, not node. I looked in the installed esbuild module, they use node there so that might be the reason, but I'm not sure and I don't have the time to dive deeper.

Hope it will help future devs.

kylekirkby commented 3 weeks ago

Found a solution, it has nothing to do with drizzle, but since it happened while working with you, I think it might be a good idea for future devs -

So it's a matter of several moving parts. Involving ZSH, node versions and only then esbulid.

So, first thing I've done was running - node -p "process.arch", the answer was x64. Then, I did nvm uninstall 18 and nvm install 18. Same result. Then I found that it might be a ZSH thing. I ran - arch and the answer was x64. So I did - env /usr/bin/arch -arm64 /bin/zsh --login (https://vineethbharadwaj.medium.com/m1-mac-switching-terminal-between-x86-64-and-arm64-e45f324184d9)

and now arch returned arm64. Next, reinstalled node again nvm uninstall 18 && nvm install 18 and the answer of node -p "process.arch" was arm64. Then I ran my code and the migration worked.

The only thing that's weird is that I'm using bun, not node. I looked in the installed esbuild module, they use node there so that might be the reason, but I'm not sure and I don't have the time to dive deeper.

Hope it will help future devs.

This worked for me too. Thanks!