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

fix: ensure that non-inherited fields are not removed when using an inferred named environment #7478

Merged
merged 1 commit into from
Dec 12, 2024

Conversation

petebacondarwin
Copy link
Contributor

Fixes #0000

It is an error for the the user to provide an environment name that doesn't match any of the named environments in the Wrangler configuration. But if there are no named environments defined at all in the Wrangler configuration, we special case the top-level environment as though it was a named environment. Previously, when this happens, we would remove all the nonInheritable fields from the configuration (essentially all the bindings) leaving an incorrect configuration. Now we correctly generate a flattened named environment that has the nonInheritable fields, plus correctly applies any transformFn on inheritable fields.


  • Tests
    • TODO (before merge)
    • Tests included
    • Tests not necessary because:
  • E2E Tests CI Job required? (Use "e2e" label or ask maintainer to run separately)
    • I don't know
    • Required
    • Not required because:
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Documentation not necessary because: bug fix

@petebacondarwin petebacondarwin added the e2e Run e2e tests on a PR label Dec 6, 2024
@petebacondarwin petebacondarwin requested a review from a team as a code owner December 6, 2024 13:08
Copy link

changeset-bot bot commented Dec 6, 2024

🦋 Changeset detected

Latest commit: 50f5b2e

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

This PR includes changesets to release 2 packages
Name Type
wrangler Patch
@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

@petebacondarwin
Copy link
Contributor Author

I can confirm that the updated tests fail without this fix.

env: {
DEV: {
account_id: "some_account_id",
it("should error if named environment contains a `account_id` field, even if there is no top-level name", () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This whole section has just been moved inside the "non-legacy" describe block as that is what they are related to. There is no code change here.

Copy link
Contributor

github-actions bot commented Dec 6, 2024

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12282151899/npm-package-wrangler-7478

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/7478/npm-package-wrangler-7478

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12282151899/npm-package-wrangler-7478 dev path/to/script.js
Additional artifacts:
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12282151899/npm-package-create-cloudflare-7478 --no-auto-update
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12282151899/npm-package-cloudflare-kv-asset-handler-7478
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12282151899/npm-package-miniflare-7478
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12282151899/npm-package-cloudflare-pages-shared-7478
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12282151899/npm-package-cloudflare-vitest-pool-workers-7478
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12282151899/npm-package-cloudflare-workers-editor-shared-7478
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12282151899/npm-package-cloudflare-workers-shared-7478
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12282151899/npm-package-cloudflare-workflows-shared-7478

Note that these links will no longer work once the GitHub Actions artifact expires.


wrangler@3.95.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20241205.0
workerd 1.20241205.0 1.20241205.0
workerd --version 1.20241205.0 2024-12-05

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

Comment on lines +72 to +76
// `rawEnv === topLevelEnv` is a special case where the user has provided an environment name
// but that named environment is not actually defined in the configuration.
// In that case we have reused the topLevelEnv as the rawEnv,
// and so we need to process the `transformFn()` anyway rather than just using the field in the `rawEnv`.
(rawEnv !== topLevelEnv ? (rawEnv[field] as Environment[K]) : undefined) ??
Copy link
Contributor

Choose a reason for hiding this comment

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

Trying to understand the need for the extra logic here. What would happen if it was left as it was?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this the inherited field transform does not run. For example the name field gets transformed when it is inherited from 'my-worker' to 'my-worker-env'.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it. Thanks!

@petebacondarwin petebacondarwin force-pushed the pbd/fix-inferred-named-environment branch 3 times, most recently from 80c4fc2 to 0d6a2f5 Compare December 11, 2024 12:09
…nferred named environment

It is an error for the the user to provide an environment name that doesn't match any of the named environments in the Wrangler configuration.
But if there are no named environments defined at all in the Wrangler configuration, we special case the top-level environment as though it was a named environment.
Previously, when this happens, we would remove all the nonInheritable fields from the configuration (essentially all the bindings) leaving an incorrect configuration.
Now we correctly generate a flattened named environment that has the nonInheritable fields, plus correctly applies any transformFn on inheritable fields.
@petebacondarwin petebacondarwin force-pushed the pbd/fix-inferred-named-environment branch from 0d6a2f5 to 50f5b2e Compare December 11, 2024 17:57
@petebacondarwin petebacondarwin merged commit 2e90efc into main Dec 12, 2024
31 checks passed
@petebacondarwin petebacondarwin deleted the pbd/fix-inferred-named-environment branch December 12, 2024 11:07
penalosa pushed a commit that referenced this pull request Jan 10, 2025
…nferred named environment (#7478)

It is an error for the the user to provide an environment name that doesn't match any of the named environments in the Wrangler configuration.
But if there are no named environments defined at all in the Wrangler configuration, we special case the top-level environment as though it was a named environment.
Previously, when this happens, we would remove all the nonInheritable fields from the configuration (essentially all the bindings) leaving an incorrect configuration.
Now we correctly generate a flattened named environment that has the nonInheritable fields, plus correctly applies any transformFn on inheritable fields.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e2e Run e2e tests on a PR
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants