Skip to content

Conversation

@penalosa
Copy link
Contributor

@penalosa penalosa commented Mar 13, 2025

Fixes #8454 and fixes #7757

There are two parts to this:

  • import { env } from "cloudflare:workers" has a type of Cloudflare.Env, where Cloudflare is a namespace defined by @cloudflare/workers-types. This PR changes wrangler types to augment this namespace, and define Env in terms of Cloudflare.Env. This means that existing workers relying on an Env type being defined will continue to work, and import { env } from "cloudflare:workers" will also be typed correctly.
  • We also augment the NodeJS namespace when the nodejs_compat_populate_process_env compatibility flag is turned on. process.env types are derived from a Pick of the text/json/secret bindings available, and always have a string-compatible value type (i.e. if --no-strict-vars is provided, every value will have a type of string, otherwise text bindings will have a literal string type)

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

@penalosa penalosa requested a review from a team as a code owner March 13, 2025 16:32
@changeset-bot
Copy link

changeset-bot bot commented Mar 13, 2025

🦋 Changeset detected

Latest commit: 142f65e

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

@workers-devprod workers-devprod added the e2e Run wrangler + vite-plugin e2e tests on a PR label Mar 13, 2025
@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Mar 13, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Mar 13, 2025

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/13846240203/npm-package-wrangler-8478

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

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

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13846240203/npm-package-wrangler-8478 dev path/to/script.js
Additional artifacts:

cloudflare-workers-bindings-extension:

wget https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13846240203/npm-package-cloudflare-workers-bindings-extension-8478 -O ./cloudflare-workers-bindings-extension.0.0.0-v32a125aae.vsix && code --install-extension ./cloudflare-workers-bindings-extension.0.0.0-v32a125aae.vsix

create-cloudflare:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13846240203/npm-package-create-cloudflare-8478 --no-auto-update

@cloudflare/kv-asset-handler:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13846240203/npm-package-cloudflare-kv-asset-handler-8478

miniflare:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13846240203/npm-package-miniflare-8478

@cloudflare/pages-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13846240203/npm-package-cloudflare-pages-shared-8478

@cloudflare/unenv-preset:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13846240203/npm-package-cloudflare-unenv-preset-8478

@cloudflare/vite-plugin:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13846240203/npm-package-cloudflare-vite-plugin-8478

@cloudflare/vitest-pool-workers:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13846240203/npm-package-cloudflare-vitest-pool-workers-8478

@cloudflare/workers-editor-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13846240203/npm-package-cloudflare-workers-editor-shared-8478

@cloudflare/workers-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13846240203/npm-package-cloudflare-workers-shared-8478

@cloudflare/workflows-shared:

npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/13846240203/npm-package-cloudflare-workflows-shared-8478

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


[email protected] includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 4.20250310.0
workerd 1.20250310.0 1.20250310.0
workerd --version 1.20250310.0 2025-03-10

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

@@ -0,0 +1,30 @@
import { UserError } from "./errors";

export function isProcessEnvPopulated(
Copy link
Contributor

Choose a reason for hiding this comment

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

error if nodejs_compat_populate_process_env && !nodejs_compat?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so, since that's not really an error case, it's just bad configuration

@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Mar 13, 2025
@penalosa penalosa force-pushed the penalosa/type-all-the-things branch from 3013113 to a5926c5 Compare March 13, 2025 18:29
"wrangler": patch
---

Add `wrangler types` support for importable env and `process.env`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Arguably this is a minor, but I think it's a fine line. As such, I've made this a patch to test out backporting to v3.

@penalosa penalosa force-pushed the penalosa/type-all-the-things branch from ae5488e to 142f65e Compare March 13, 2025 23:02
@penalosa penalosa merged commit 931b53d into main Mar 14, 2025
20 of 21 checks passed
@penalosa penalosa deleted the penalosa/type-all-the-things branch March 14, 2025 10:45
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Mar 14, 2025
@emily-shen emily-shen mentioned this pull request Mar 14, 2025
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

e2e Run wrangler + vite-plugin e2e tests on a PR

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

🚀 Feature Request: Support env types with importable env 🚀 Feature Request: generate process.env types

3 participants