cloudflare / workers-sdk

⛅️ Home to Wrangler, the CLI for Cloudflare Workers®
https://developers.cloudflare.com/workers/
Apache License 2.0
2.41k stars 590 forks source link

🐛 BUG: When the environment variable is empty, bindings from another environment are loaded. #6130

Open ogadra opened 1 week ago

ogadra commented 1 week ago

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