drizzle-team / drizzle-team/drizzle-orm

[BUG]: drizzle-kit add "./" prefix on `generate` when given absolute path (resolve and __dirname)

Open
#3,807 8 comments 12 reactions 0 assignees View on GitHub
bug drizzle/kit priority
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Report hasn't been filed before.

- [X] I have verified that the bug I'm about to report hasn't been filed before.

### What version of `drizzle-orm` are you using?

0.38.2

### What version of `drizzle-kit` are you using?

0.30.1

### Other packages

_No response_

### Describe the Bug

When doing `drizzle-kit generate` it will give out an `ENOENT` error, which is actually correct but it is correct because drizzle-kit somehow prepend a migration meta file path with `./`

here's the output of the error:
```sh
user@host lib-db % bun --bun --env-file=../../.env.local run drizzle-kit --config=./src/config.ts generate
[bun] Warning: async_hooks.createHook is not implemented in Bun. Hooks can still be created but will never be called.
Reading config file '/Users/user/workspace/project/src/lib-db/src/config.ts'
7672 | };
7673 | validateWithReport = (snapshots, dialect6) => {
7674 | const { validator: validator2, version: version3 } = validatorForDialect(dialect6);
7675 | const result = snapshots.reduce(
7676 | (accum, it) => {
7677 | const raw2 = JSON.parse((0, import_fs.readFileSync)(`./${it}`).toString());
^
ENOENT: No such file or directory
errno: -2
syscall: "open"
path: ".//Users/user/workspace/project/src/lib-db/src/migration/meta/1734596738_snapshot.json"

at /Users/user/workspace/project/node_modules/drizzle-kit/bin.cjs:7677:49
at reduce (1:11)
at prepareMigrationFolder (/Users/user/workspace/project/node_modules/drizzle-kit/bin.cjs:7717:22)
at /Users/user/workspace/project/node_modules/drizzle-kit/bin.cjs:51667:40
at prepareAndMigratePg (/Users/user/workspace/project/node_modules/drizzle-kit/bin.cjs:51661:34)
at /Users/user/workspace/project/node_modules/drizzle-kit/bin.cjs:91970:13
```

as you can see the `path` is actually "correct" and pointing to the actual existing `1734596738_snapshot.json` file, however the whole path is prepended by `./` which makes it invalid and in turn causing drizzle-kit or rather OS unable to find the file

Happened on `macOS 15.1.1 (24B91) Apple M1` will also test on my Linux machine later

The content of `config.ts`:
```ts
import { defineConfig } from "drizzle-kit";
import { env } from "lib-env";
import { resolve } from "node:path";

const basePath = __dirname; //dirname --> /Users/user/workspace/project/src/lib-db/src

export default defineConfig({
dbCredentials: {
url: env.DATABASE_URL,
},
dialect: "postgresql",
casing: "snake_case",
migrations: {
prefix: "unix",
},
out: resolve(basePath, "migration"),
schema: resolve(basePath, "schema/index.ts"),
strict: true,
verbose: true,
});
```

Update:

~~I think more specifically, it is caused when the path to migration path is given as an absolute path, either raw `__dirname` / string concatination or using `resolve` path function, however when using a workaround on the comment below, which is using `relative` path function, it works as expected~~

This actually messed up some migration generation

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.