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: widen multi-env vars types in wrangler types #5086

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dario-piotrowicz
Copy link
Member

@dario-piotrowicz dario-piotrowicz commented Feb 24, 2024

Fixes #5082

What this PR solves / how to test:

Currently types for variable generate string literal, those are appropriate when
a single environment has been specified in the config file but if multiple environments
are specified this however wrongly restricts the typing, the changes here fix such
incorrect behavior.

For example, given a wrangler.toml containing the following:

[vars]
MY_VAR = "dev value"
[env.production]
[env.production.vars]
MY_VAR = "prod value"

running wrangler types would generate:

interface Env {
	MY_VAR: "dev value";
}

making typescript incorrectly assume that MY_VAR is always going to be "dev value"

after these changes, the generated interface would instead be:

interface Env {
	MY_VAR: "dev value" | "prod value";
}

Author has addressed the following:

  • Tests
    • Included
    • Not necessary because:
  • Changeset (Changeset guidelines)
    • Included
    • Not necessary because:
  • Associated docs
    • Issue(s)/PR(s):
    • Not necessary because: this is a quality of life improvement, not requiring documentation

Note for PR author:

We want to celebrate and highlight awesome PR review! If you think this PR received a particularly high-caliber review, please assign it the label highlight pr review so future reviewers can take inspiration and learn from it.

@dario-piotrowicz dario-piotrowicz requested a review from a team as a code owner February 24, 2024 17:29
Copy link

changeset-bot bot commented Feb 24, 2024

🦋 Changeset detected

Latest commit: df55b66

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

Copy link
Contributor

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/8032131415/npm-package-wrangler-5086

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

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

Or you can use npx with this latest build directly:

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

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


wrangler@3.29.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20240208.0
workerd 1.20240208.0 1.20240208.0
workerd --version 1.20240208.0 2024-02-08

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

Copy link

codecov bot commented Feb 24, 2024

Codecov Report

Attention: Patch coverage is 97.36842% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 70.42%. Comparing base (cab7e1c) to head (df55b66).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #5086      +/-   ##
==========================================
+ Coverage   70.33%   70.42%   +0.08%     
==========================================
  Files         298      298              
  Lines       15515    15536      +21     
  Branches     3987     3990       +3     
==========================================
+ Hits        10913    10941      +28     
+ Misses       4602     4595       -7     
Files Coverage Δ
packages/wrangler/src/type-generation.ts 99.29% <100.00%> (+0.09%) ⬆️
packages/wrangler/src/config/index.ts 90.44% <91.66%> (+0.27%) ⬆️

... and 6 files with indirect coverage changes

@dario-piotrowicz
Copy link
Member Author

I've converted this PR to draft as it's been decided by the team that we should go with generic string, etc... types instead of union of string literals etc...

@DaniFoldi
Copy link
Contributor

Hi 👋,

Thanks @dario-piotrowicz for the PR, this is something we had to fix manually after running wrangler types. I believe your implementation would improve correctness, and I was looking forward to seeing it land. Widening the type to string is a weird change in my opinion - updating a vars variable is no different to adding/deleting a KV namespace, it causes the same desync between the config and the type declaration file. Some frameworks auto-generate their equivalent of wrangler types (eg. astro sync, nuxi prepare) before building or starting a dev server, although I admit that's a slightly different case as those files would generally be in .gitignore. Still, I don't think a union would cause any more harm than string.

@jasperdunn
Copy link

I'd be keen to know what the reasoning was for using a generic string type instead of string literals. Surely having more robust types would benefit those of us using typescript? My solution atm is to override the type.

@jfsiii
Copy link

jfsiii commented Oct 31, 2024

I've converted this PR to draft as it's been decided by the team that we should go with generic string, etc... types instead of union of string literals etc...

@dario-piotrowicz Is there a place to track that decision/implementation?

I'd also prefer a union but I'm mostly looking to avoid this bug

@dario-piotrowicz
Copy link
Member Author

I've converted this PR to draft as it's been decided by the team that we should go with generic string, etc... types instead of union of string literals etc...

@dario-piotrowicz Is there a place to track that decision/implementation?

I'd also prefer a union but I'm mostly looking to avoid this bug

Hi @jfsiii 👋

Sorry, honestly this has fallen off my radar and I haven't pushed for a resolution, it's been quite a long time since this PR has been opened... let me ping the team and prompt them to make a decision, then I can update and merge this PR 🙂👍

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

Successfully merging this pull request may close these issues.

🚀 Feature Request: wrangler types generates wider types for environment variables
4 participants