that wasn't it... it turns out that the

that wasn't it... it turns out that the cloudflare vite plugin creates a new flattened wrangler.json, and you have to specify CLOUDFLARE_ENV during the vite build in order for it to build the right one
10 Replies
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
zsmooth
zsmoothOP3w ago
Does the redirected config only apply to the deploy command? And if not, does that mean i have to run a build before (for example) uploading a new secret? One improvement to the DX here might be if someone uses --env and that env doesn't exist in the redirected config, but it DOES exist in the local config, tell them what happened: "A redirected wrangler.json was built by your build process. You may need to specify CLOUDFLARE_ENV in order for the generated file to have the correct env." This could have been really bad if I had my production deploy instructions as the default (non-named env), and was trying to deploy to env.staging. It would have deployed to production without me realizing it. The only reason it was failing for me was because I have a KV binding with the id set to dev for the dev environment, and that obviously won't work with a deployed binding. I could see this biting someone in the future: * They use a template to set up a basic wrangler worker app that deploys to an environment without --env. * They build up that environment and it's like a playground * Eventually they decide it's good enough and it's prod * They realize they need a staging env, and add it to the wrangler.json Now they're in a situation where accidentally deploying to prod when they wanted to deploy to staging could happen because of the redirected wrangler.json.
James Opstad
James Opstad3w ago
Thanks for the feedback. We'll be working on the documentation for this over the next few weeks and will make improvements (it's all very new right now). With the Cloudflare Vite plugin, the environment is applied at build time and a flattened config is generated. So for changes to the config to take effect when deploying you need to generate a new build. This is great feedback!
zsmooth
zsmoothOP3w ago
Yeah, that’s a terrible DX. Will need lots of clear docs and messaging (If you’re saying i have to run a build before pushing a secret, which is what it sounds like)
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
James Opstad
James Opstad3w ago
This doesn't apply to secrets because they're not part of the config. You set them via the CLI or dashboard so they can be updated independently.
zsmooth
zsmoothOP3w ago
But when I push a secret, it reads the config, right? It has to to at least get the name of the worker. My worry was that it would read the redirected config, see that the env you specified doesn’t exist, and fail. But according to Pete it sounds like that’s not the case
James Opstad
James Opstad3w ago
Yep. It will read your input config. It's only a small subset of commands that read the redirected config. Appreciate that this all needs clear documentation and is confusing at the moment.
svend
svend2w ago
Will just steal this thread to follow up on the "deploy" command and use of cloudflare-vite plugin 🙈 It seems wrangler now: - appends to worker name on its own initiative and doing it twice, also including CLOUDFLARE_ENV? - Goes into build/server and finds wrangler.json without any reference to it in my config files? Can find it in .wrangler/deploy/config.json, but not sure where that file comes from. Vite plugin perhaps? Is this also documented somewhere? Edit: appending is mentioned here, but not the twice part: https://github.com/cloudflare/workers-sdk/tree/main/packages/vite-plugin-cloudflare#cloudflare-environments Example 1 gives worker name workername-production:

.env.production:
RUNTIME_ENV=production
CLOUDFLARE_ENV=$RUNTIME_ENV

./wrangler.json
{
"name": "workername",
"env": {
"production": {
"name": "workername",
}
}
}

.env.production:
RUNTIME_ENV=production
CLOUDFLARE_ENV=$RUNTIME_ENV

./wrangler.json
{
"name": "workername",
"env": {
"production": {
"name": "workername",
}
}
}
Example 2 gives worker name workername-production-production...:

.env.production:
RUNTIME_ENV=production
CLOUDFLARE_ENV=$RUNTIME_ENV

./wrangler.json
{
"name": "workername",
"env": {
"production": {//no name key here}
}
}

.env.production:
RUNTIME_ENV=production
CLOUDFLARE_ENV=$RUNTIME_ENV

./wrangler.json
{
"name": "workername",
"env": {
"production": {//no name key here}
}
}
Deploy command is:
vite build --mode production && wrangler deploy --env production
vite build --mode production && wrangler deploy --env production
Wrangler 4.4, @cloudflare/vite-plugin: 0.1.15 Warning during deploy:
Using redirected Wrangler configuration.
- Configuration being used: "build/server/wrangler.json"
- Original user's configuration: "wrangler.json"
- Deploy configuration file: ".wrangler/deploy/config.json"
▲ [WARNING] Processing build/server/wrangler.json configuration:

- No environment found in configuration with name "production".
Before using `--env=production` there should be an equivalent environment section in the
configuration.

Consider adding an environment configuration section to the wrangler.json file:

[env.production]
Using redirected Wrangler configuration.
- Configuration being used: "build/server/wrangler.json"
- Original user's configuration: "wrangler.json"
- Deploy configuration file: ".wrangler/deploy/config.json"
▲ [WARNING] Processing build/server/wrangler.json configuration:

- No environment found in configuration with name "production".
Before using `--env=production` there should be an equivalent environment section in the
configuration.

Consider adding an environment configuration section to the wrangler.json file:

[env.production]
Wonky Onion
Wonky Onion2w ago
you should use CLOUDFLARE_ENV with vite build to set the environment for deploy (like you're already doing), and then you don't need wrangler deploy --env. that's probably why you're getting the double appending (because vite resolves the environment once, and then wrangler tries to do it again). tldr vite build --mode production && wrangler deploy should ensure you have the right environment.

Did you find this page helpful?