Skip to content

Conversation

1000hz
Copy link
Contributor

@1000hz 1000hz commented Sep 3, 2025

This PR adds tagging behavior that applies appropriate service and environment tags to Workers with multiple environments when deploying or uploading a new version. If any environments are detected in the Wrangler config file, running wrangler deploy or wrangler version upload will apply a cf:service=${topLevelName} and (if a --env was specified) a cf:environment=${environmentName} tag. This metadata will be used by the Cloudflare Dashboard to group these Workers together in a more user-friendly way.

This PR also adds a new definedEnvironments computed field to Wrangler config that can be used to more easily determine whether a Worker uses environments.

  • Tests
    • Tests included
    • Tests not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: not a public-facing Wrangler feature
  • Wrangler V3 Backport
    • Wrangler PR:
    • Not necessary because: Not scoped for v3

@1000hz 1000hz requested a review from a team as a code owner September 3, 2025 18:39
Copy link

changeset-bot bot commented Sep 3, 2025

🦋 Changeset detected

Latest commit: f31edfc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
wrangler Minor
@cloudflare/vite-plugin Major
@cloudflare/vitest-pool-workers Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

pkg-pr-new bot commented Sep 3, 2025

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@10546

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@10546

miniflare

npm i https://pkg.pr.new/miniflare@10546

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@10546

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@10546

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@10546

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@10546

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@10546

wrangler

npm i https://pkg.pr.new/wrangler@10546

commit: f31edfc

@1000hz 1000hz force-pushed the cina/tag-services-and-environments branch from b1172e7 to 45c8035 Compare September 3, 2025 19:39
Copy link
Contributor

@jamesopstad jamesopstad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @1000hz. This doesn't account for redirected configs (as used by the Vite plugin, Nitro and possibly others), which flatten the configuration using the selected environment at build time. See the following:

if (isRedirectedConfig && definedEnvironments.length > 0) {
diagnostics.errors.push(
dedent`
Redirected configurations cannot include environments but the following have been found:\n${definedEnvironments
.map((env) => ` - ${env}`)
.join("\n")}
Such configurations are generated by tools, meaning that one of the tools
your application is using is generating the incorrect configuration.
Report this issue to the tool's author so that this can be fixed there.
`
);
}

We propose that an environmentName property is added to ComputedFields that is included in the output of readConfig. Then, for redirected configs (which are configs that have been generated by readConfig), this value can be used to provide the env to applyServiceAndEnvironmentTags. Does that sound OK?

@github-project-automation github-project-automation bot moved this from Untriaged to In Review in workers-sdk Sep 4, 2025
@petebacondarwin petebacondarwin marked this pull request as draft September 8, 2025 13:36
@1000hz 1000hz force-pushed the cina/tag-services-and-environments branch from 45c8035 to af14df0 Compare September 10, 2025 16:27
@1000hz 1000hz marked this pull request as ready for review September 10, 2025 16:27
@1000hz 1000hz requested a review from jamesopstad September 10, 2025 16:28
@1000hz 1000hz force-pushed the cina/tag-services-and-environments branch 2 times, most recently from 0f2b7ac to cd128b7 Compare September 10, 2025 19:55
…vironments is deployed or has a new version upload.
@1000hz 1000hz force-pushed the cina/tag-services-and-environments branch from cd128b7 to f31edfc Compare September 10, 2025 20:13
Copy link
Contributor

@jamesopstad jamesopstad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Thanks for doing the extra work to support the redirected config. It would be good to add some tests for that scenario but it's not blocking. I think we probably need a writeRedirectedWranglerConfig helper equivalent to writeWranglerConfig to make this more straightforward.

@github-project-automation github-project-automation bot moved this from In Review to Approved in workers-sdk Sep 10, 2025
Copy link
Contributor

@penalosa penalosa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test for redirectied config?

@@ -48,6 +50,10 @@ export interface ComputedFields {
* It can be useful to know what the top-level name was before the flattening.
*/
topLevelName: string | undefined;
/** A list of environment names declared in the raw configuration. */
definedEnvironments: string[] | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need definedEnvironments? It looks like it's only used to decide whether or not to apply environment tags, but surely the presence of a target environment is enough to decide that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

target environment alone is not sufficient. we need to apply service tags as long as there are any environments defined in the config file, even when you're targeting the top-level environment (i.e. target environment would be empty).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, but to play devils advocate for a second, did you consider just always adding the service name tag, regardless of whether the user has environments in their config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We potentially could, but there'd be some undesirable implications on other parts of the system that we'd need to handle (i.e. service grouping) and it'd be better to only apply it where necessary for now.

let requestCount = 0;
mockPatchScriptSettings(async ({ request }) => {
requestCount++;
if (requestCount === 2) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we sending three requests to this endpoint?

Copy link
Contributor Author

@1000hz 1000hz Sep 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are sending two. why not bundle setting the tags into the existing request? there's a chance updating the tags will fail if the additional tags push the worker beyond the tag limit, so I didn't want to jeopardize syncing the other script settings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no way we can exempt these specific tags from the tag limit server side? I'm not going to block on this because we already do this all over the place in Wrangler, but I'd definitely prefer we get closer to 1 API call being a deploy, rather than further away

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, now that we're qualifying these tags with a cf: prefix, we probably should exempt these two specific tags from the limit.

@1000hz
Copy link
Contributor Author

1000hz commented Sep 12, 2025

Spoke with @penalosa and decided I'll follow up this PR with another one to add additional test coverage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Approved
Development

Successfully merging this pull request may close these issues.

3 participants