Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task done
deanrih opened this issue Dec 19, 2024 · 1 comment
Labels
bug Something isn't working drizzle/kit priority Will be worked on next

Comments

@deanrih
Copy link

deanrih commented Dec 19, 2024

Report hasn't been filed before.

  • 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:

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:

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

@deanrih deanrih added the bug Something isn't working label Dec 19, 2024
@deanrih
Copy link
Author

deanrih commented Dec 19, 2024

This actually messed up some migration generation

There's at least workaround by modifying the config:

import { defineConfig } from "drizzle-kit";
import { env } from "lib-env";
import { relative } from "node:path";

const basePath = __dirname;

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

@deanrih deanrih changed the title [BUG]: drizzle-kit unecessarily add "./" prefix on generate command when there's already a migration files [BUG]: drizzle-kit unecessarily add "./" prefix on generate when given absolute path (resolve and __dirname) Dec 19, 2024
@deanrih deanrih changed the title [BUG]: drizzle-kit unecessarily add "./" prefix on generate when given absolute path (resolve and __dirname) [BUG]: drizzle-kit add "./" prefix on generate when given absolute path (resolve and __dirname) Dec 19, 2024
@L-Mario564 L-Mario564 added drizzle/kit priority Will be worked on next labels Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working drizzle/kit priority Will be worked on next
Projects
None yet
Development

No branches or pull requests

2 participants