Skip to content

Wrangler D1 Migrations Path Bug (Vite Plugin) #15484

Description

@Karsten-Zhou

What versions & operating system are you using?

System:
OS: Windows 11 10.0.26200
CPU: (12) x64 12th Gen Intel(R) Core(TM) i5-12400F
Memory: 5.38 GB / 15.85 GB
Binaries:
Node: 24.13.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.19 - C:\Users\wesli\AppData\Roaming\npm\yarn.CMD
npm: 10.4.0 - C:\Program Files\nodejs\npm.CMD
bun: 1.3.7 - C:\Users\wesli.bun\bin\bun.EXE
npmPackages:
@cloudflare/vite-plugin: ^1.54.3 => 1.54.3
wrangler: ^4.128.0 => 4.128.0

Please provide a link to a minimal reproduction

https://github.com/Karsten-Zhou/wrangler-migrations-bug-repro

Describe the Bug

Issue

When using @cloudflare/vite-plugin + wrangler deploy with auto-provisioning, the original wrangler.jsonc gets its migrations_dir path incorrectly rewritten.

Project Layout

project/
├── wrangler.jsonc      # Original config
├── vite.config.ts      # @cloudflare/vite-plugin
├── package.json
├── tsconfig.json
├── migrations/
│   └── 0001_init.sql
└── server/
    └── index.ts

Steps to Reproduce

  1. Install dependencies:

    npm install
  2. Build the project:

    npx vite build
  3. Deploy the first time (the subsequent deployments won't trigger the auto-provisioning):

    npx wrangler deploy

What Happens

  1. @cloudflare/vite-plugin generates dist/my-app/wrangler.json with:

    "migrations_dir": "../../migrations"

    This is correct relative to dist/my-app/.

  2. Wrangler auto-provisions the D1 database (since database_id is missing).

  3. Wrangler writes the database_id back to the config.

  4. BUG: It also writes "migrations_dir": "../../migrations" into the original wrangler.jsonc.

  5. The original config now has an incorrect path. Subsequent database migrations fail because ../../migrations is wrong relative to the project root.

Root Cause

The bug is in the interaction between two Wrangler behaviors:

  1. Vite plugin (output-config.ts) correctly rewrites migrations_dir for the generated dist/<name>/wrangler.json.
  2. Wrangler auto-provisioning (experimental_patchConfig) writes the entire binding object (including the rewritten migrations_dir) back to the original config file, instead of only updating the database_id or preserving the original migrations_dir value.

Expected Fix

When experimental_patchConfig patches the original wrangler.jsonc, it should either:

  • Only patch the database_id field, leaving migrations_dir untouched
  • Rebase migrations_dir back to the original config's directory before writing

Please provide any relevant error logs

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature:auto-provisioningAutomatic provisioning of Worker resourcespackage:vite-pluginRelating to the `@cloudflare/vite-plugin` packagepackage:wranglerRelating to the `wrangler` package

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions