drizzle-team / drizzle-kit-mirror

Docs and issues repository for drizzle-kit
288 stars 16 forks source link

Issues with ESBuild Register #429

Open mothershipper opened 1 month ago

mothershipper commented 1 month ago

There's an issue with esbuild-register that's impacting our ability to use drizzle-kit, the TL;DR; is esbuild-register doesn't seem to resolve the project's tsconfig file correctly.

Did some instrumenting and observed that it fails to bubble up looking for a tsconfig.json for the project when running drizzle-kit:

Reading config file '/Users/jack/dev/marketplace/backend/libs/database/drizzle.config.ts'
loading /Users/jack/dev/marketplace/backend/libs/database/tsconfig.json
loading /Users/jack/dev/marketplace/backend/configs/tsconfig.json
loading /Users/jack/dev/marketplace/backend/configs/tsconfig.json
loading /Users/jack/dev/marketplace/backend/configs/tsconfig.json
loading /Users/jack/dev/marketplace/backend/configs/tsconfig.json
loading /Users/jack/dev/marketplace/backend/configs/tsconfig.json
loading /Users/jack/dev/marketplace/backend/configs/application/tsconfig.json
Error: Transform failed with 4 errors:
 ... ERROR: Transforming JavaScript decorators to the configured target environment ("es2021") is not supported yet

It seems like esbuild-register searches the local directory of the file under compilation for a tsconfig.json, and if not found, uses the default tsconfig settings. Our problem is that backend is a nest project that makes heavy use of decorators. experimentalDecorators is not a default tsconfig option and hence esbuild fails to compile the drizzle config file.

The original esbuild-register issue suggested this can be worked around by doing the tsconfig resolution yourself and passing that as an option to esbuild, bypassing its resolution of the tsconfig.json.

mothershipper commented 1 month ago

I think misread the proposed solution on the downstream issue. It seems like they were suggesting parsing the complete tsconfig file yourself to pass individual options to esbuild, which is roughly what was proposed in a different tsconfig handling issue: https://github.com/egoist/esbuild-register/issues/99

Not sure if this is fixable without fixing upstream or forking esbuild-register :/

mothershipper commented 1 month ago

For a bit more color, esbuild-register uses transformSync from esbuild. According to the docs, the transform API does not have access to the file-system

Seems like you'd have to load the tsconfig.json, handle any extends and pass a merged tsconfig.json down to esbuild for the transformSync call.

AlexBlokh commented 1 month ago

@mothershipper thanks for reporting, can you please prepare a simple minimal repro example, we'll gladly have a look

mothershipper commented 1 month ago

@AlexBlokh https://github.com/mothership/drizzle-kit-esbuild-bug

LynchEntrostat commented 3 weeks ago

Hi, I am also experiencing the same issue. My project uses class-transformer decorators on some classes that aren't even imported in my drizzle entity files, but I do get this error when trying to generate a migration.

peterphan1996 commented 2 weeks ago

I also have this issue using NestJS

LynchEntrostat commented 2 weeks ago

Hi, I am also experiencing the same issue. My project uses class-transformer decorators on some classes that aren't even imported in my drizzle entity files, but I do get this error when trying to generate a migration.

My team and I temporarily solved this on my NestJS project using a patch

drizzle-kit@0.22.1.patch

diff --git a/bin.cjs b/bin.cjs
index e5e7558dc577c4fba1ca4fe508a5851b4e8f4c91..ddb3a33904dd375bfbb39be5dcc5cfc376226faa 100755
--- a/bin.cjs
+++ b/bin.cjs
@@ -1,5 +1,7 @@
 #!/usr/bin/env node
 "use strict";
+require('ts-node/register');
+require('reflect-metadata');
 var __create = Object.create;
 var __defProp = Object.defineProperty;
 var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
LynchEntrostat commented 2 weeks ago

Hi, I am also experiencing the same issue. My project uses class-transformer decorators on some classes that aren't even imported in my drizzle entity files, but I do get this error when trying to generate a migration.

My team and I temporarily solved this on my NestJS project using a patch

drizzle-kit@0.22.1.patch

diff --git a/bin.cjs b/bin.cjs
index e5e7558dc577c4fba1ca4fe508a5851b4e8f4c91..ddb3a33904dd375bfbb39be5dcc5cfc376226faa 100755
--- a/bin.cjs
+++ b/bin.cjs
@@ -1,5 +1,7 @@
 #!/usr/bin/env node
 "use strict";
+require('ts-node/register');
+require('reflect-metadata');
 var __create = Object.create;
 var __defProp = Object.defineProperty;
 var __getOwnPropDesc = Object.getOwnPropertyDescriptor;

@peterphan1996 Hope this helps you too.