nitrojs / nitrojs/nitro

Dulicated bindings in auto-generated wrangler.json file with Nuxt 3 Layers architecture

Open
#3,316 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug nuxt v2
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

Environment

Any Node and Bun versions. The nitro package has the latest version.

Reproduction

Related repo:
https://github.com/serhii-chernenko/nuxt-layers-cloudflare-issue-v2

Clone repo, install packages, and build the app
git clone git@github.com:serhii-chernenko/nuxt-layers-cloudflare-issue-v2.git
bun install
bun run build:app
Verify a generated wrangler.json file.
cat src/apps/app/dist/_worker.js/wrangler.json

Expected output:

{
  "compatibility_date": "2025-01-27",
  "kv_namespaces": [
    {
      "binding": "KV_CORE",
      "id": "SET_IN_ENV"
    },
    {
      "binding": "KV_CMS",
      "id": "SET_IN_ENV"
    },
    {
      "binding": "KV_CORE",
      "id": "SET_IN_ENV"
    }
  ],
  "d1_databases": [
    {
      "binding": "DB_CORE",
      "database_id": "SET_IN_ENV"
    },
    {
      "binding": "DB_CMS",
      "database_id": "SET_IN_ENV"
    },
    {
      "binding": "DB_CORE",
      "database_id": "SET_IN_ENV"
    }
  ],
  "name": "nuxt-layers-cloudflare-issue-v2",
  "pages_build_output_dir": "..",
  "compatibility_flags": [
    "nodejs_compat",
    "no_nodejs_compat_v2"
  ]
}

As you can see, KV_CORE and DB_CORE duplicated twice.

Describe the bug

If you run

bun run dev:app

You have to see the error

ERROR  Failed to initialize wrangler bindings proxy Processing dist/_worker.js/wrangler.json configuration:
│   - KV_CORE assigned to multiple KV Namespaces bindings.
│   - DB_CORE assigned to multiple D1 Databases bindings.
│   - Bindings must have unique names, so that they can all be referenced in the worker.
│     Please change your bindings to have unique names.
Additional context
Explanation

The issue happens because I extend one layer in another one and then both are extended in the end application:

  • Core Admin → App
  • Core Admin → CMS Admin (because this layer needs functionality of the Core Admin layer) → App

I can avoid this issue by extending the chain this way:

  • Core Admin → CMS Admin → App

Just by removing:

  • Core Admin → App

But I want to have the explicit extending in the end app.

Suggestion

Filtering bindings in each array available for the nitro.cloudflare.wrangler object such as kv_namespaces, d1_databases, etc. by the same name.

Logs

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction repository, run bun install and bun run build:app, then inspect src/apps/app/dist/_worker.js/wrangler.json. Trace how the Core Admin and CMS Admin layers contribute kv_namespaces and d1_databases to the nitro.cloudflare.wrangler configuration. Done means generated bindings are unique and bun run dev:app no longer reports duplicate bindings.

Written by the indexing model from the issue text.

Assessment

Tech stack
bun, nuxt, typescript
Domain
backend, build-system, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.