cloudflare / cloudflare/workers-sdk

Wrangler D1 Migrations Path Bug (Vite Plugin)

Open
#15,484 1 comment 0 reactions 0 assignees View on GitHub
feature:auto-provisioning package:vite-plugin package:wrangler
Dominant language
TypeScript
Stars
4.5k
Forks
1.5k
Avg merge
3d 8h
Merged PRs (30d)
186

Description

### 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:
```bash
npm install
```

2. Build the project:
```bash
npx vite build
```

3. Deploy the first time (the subsequent deployments won't trigger the auto-provisioning):
```bash
npx wrangler deploy
```

## What Happens

1. `@cloudflare/vite-plugin` generates `dist/my-app/wrangler.json` with:
```json
"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//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_

Contributor guide

Open the contributing guide

Research direction

Read the Vite plugin's output-config.ts and Wrangler's experimental_patchConfig path, then reproduce the behavior with the linked project using npm install, npx vite build, and npx wrangler deploy. Compare the generated dist configuration with the original wrangler.jsonc; done means auto-provisioning adds database_id without rewriting the original migrations_dir path.

Written by the indexing model from the issue text.

Assessment

Tech stack
nodejs, typescript, vite
Domain
cli, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.