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: When the environment variable is empty, bindings from another environment are loaded. [resolved] #6130

Closed
ogadra opened this issue Jun 23, 2024 · 4 comments
Labels
bug Something that isn't working

Comments

@ogadra
Copy link

ogadra commented Jun 23, 2024

Which Cloudflare product(s) does this pertain to?

Wrangler core

What version(s) of the tool(s) are you using?

3.61.0

What version of Node are you using?

v22.1.0

What operating system and version are you using?

Sonoma 14.4.1

Describe the Bug

Observed behavior

I want to use different D1 for the validation environment and the production environment. To do so, I set up wrangler.toml as follows:

[env.stg]
d1_databases = [
  { binding = "DB", database_name = "db-stg", database_id = <stg-db-id> },
]

[env]
d1_databases = [
  { binding = "DB", database_name = "db", database_id = <prd-db-id> },
]

[[d1_databases]]
binding = "DB"
database_name = "db-stg"
database_id = <stg-db-id>

[[d1_databases]]
binding = "DB"
database_name = "db"
database_id = <prd-db-id>

Then deploy to Cloudflare Workers it with the wrangler deploy command.

However, this configuration will cause the deployment to fail with the following error:

Your worker has access to the following bindings:
- D1 Databases:
  - DB: db-stg (<stg-db-id>)
  - DB: db (<prd-db-id>)

✘ [ERROR] A request to the Cloudflare API (/accounts/<accountId>/workers/scripts/<productName>) failed.

  the binding name 'DB' cannot be used more than once [code: 10053]

  If you think this is a bug, please open an issue at:
  https://github.com/cloudflare/workers-sdk/issues/new/choose

Expected behavior

This is probably due to the fact that the value of env.stg is being read even though the environment variable has not been set.

If I set the environment variable to stg, this works fine.

However, I don't want the production app name to have -prd attached to it, so I want to deploy it with empty environment variables.

Thanks for reading.

Please provide a link to a minimal reproduction

https://github.com/ogadra/empty-variable-in-wrangler

Please provide any relevant error logs

No response

@ogadra ogadra added the bug Something that isn't working label Jun 23, 2024
@petebacondarwin
Copy link
Contributor

This is because your wrangler.toml is not valid. If you converted it to JSON it would look like this:

{
  "compatibility_date": "2024-06-20",
  "compatibility_flags": [
    "nodejs_compat"
  ],
  "main": "src/index.ts",
  "name": "empty-variable-in-wrangler",
  "d1_databases": [
    {
      "binding": "DB",
      "database_id": "<stg-db-id>",
      "database_name": "db-stg"
    },
    {
      "binding": "DB",
      "database_id": "<prd-db-id>",
      "database_name": "db"
    }
  ],
  "env": {
    "d1_databases": [
      {
        "binding": "DB",
        "database_id": "<prd-db-id>",
        "database_name": "db"
      }
    ],
    "stg": {
      "d1_databases": [
        {
          "binding": "DB",
          "database_id": "<stg-db-id>",
          "database_name": "db-stg"
        }
      ]
    }
  }
}

You can see that you have added two DB bindings to the top-level - amongst other issues.

We have a PR to provide better errors in this case: #5737

@petebacondarwin petebacondarwin closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2024
@ogadra
Copy link
Author

ogadra commented Jul 4, 2024

@petebacondarwin So, I can't switch the database binding by the stage name, can I? Thanks.

@petebacondarwin
Copy link
Contributor

petebacondarwin commented Jul 4, 2024

I'm not quite sure what you mean but I think what you want in your wrangler.toml is something like:

d1_databases = [
  { binding = "DB", database_name = "db", database_id = "<prd-db-id>" },
]

[env.stg]
d1_databases = [
  { binding = "DB", database_name = "db-stg", database_id = "<stg-db-id>" },
]

What this does is set the prod-db-id as the top level environment and the std-db-id for the stg environment.

@ogadra
Copy link
Author

ogadra commented Jul 6, 2024

@petebacondarwin I did it!! Thanks for your advice!

@ogadra ogadra changed the title 🐛 BUG: When the environment variable is empty, bindings from another environment are loaded. 🐛 BUG: When the environment variable is empty, bindings from another environment are loaded. [resolved] Jul 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that isn't working
Projects
Archived in project
Development

No branches or pull requests

2 participants