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

widen multi-env vars types in wrangler types and add --strict-vars option to the command #5086

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

dario-piotrowicz
Copy link
Member

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

Fixes #5082

This PR does two things:

  • one is widening the vars types that wrangler types generates when the same variables are defined in different environments (basically instead of incorrectly generate something like MY_VAR: "dev value"; now we could correctly generate MY_VAR: "dev value" | "prod value"; etc...)
  • two is adding a new option (currently called --strict-vars) that allows the generated types not to be literals/unions but to be loose/generic ones (e.g. instead of MY_VAR: "dev value" | "prod value"; it allows the generation of MY_VAR: string;), this was added after various feedback and back and forth from the team (the concern being that literal values/unions can be annoying for people if they change their var values often)

For more details please check the changeset files in the PR 🙂


  • 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: this is tested in unit tests
  • Public documentation

@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: 5405cd8

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

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

github-actions bot commented Feb 24, 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/12679174554/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/12679174554/npm-package-wrangler-5086 dev path/to/script.js
Additional artifacts:
wget https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12679174554/npm-package-cloudflare-workers-bindings-extension-5086 -O ./cloudflare-workers-bindings-extension.0.0.0-v764a02dfa.vsix && code --install-extension ./cloudflare-workers-bindings-extension.0.0.0-v764a02dfa.vsix
npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12679174554/npm-package-create-cloudflare-5086 --no-auto-update
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12679174554/npm-package-cloudflare-kv-asset-handler-5086
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12679174554/npm-package-miniflare-5086
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12679174554/npm-package-cloudflare-pages-shared-5086
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12679174554/npm-package-cloudflare-unenv-preset-5086
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12679174554/npm-package-cloudflare-vitest-pool-workers-5086
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12679174554/npm-package-cloudflare-workers-editor-shared-5086
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12679174554/npm-package-cloudflare-workers-shared-5086
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/12679174554/npm-package-cloudflare-workflows-shared-5086

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


wrangler@3.100.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20241230.0
workerd 1.20241230.0 1.20241230.0
workerd --version 1.20241230.0 2024-12-30

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 🙂👍

@kansson
Copy link

kansson commented Dec 29, 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

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 🙂👍

Hey just checking in on the progress. Thanks 😄

@dario-piotrowicz dario-piotrowicz force-pushed the wrangler-types-string-literals branch from df55b66 to b4ac0d9 Compare January 1, 2025 15:41
@dario-piotrowicz dario-piotrowicz added skip-pr-description-validation Skip validation of the required PR description format and removed skip-pr-description-validation Skip validation of the required PR description format labels Jan 1, 2025
@dario-piotrowicz
Copy link
Member Author

Hey just checking in on the progress. Thanks 😄

@kansson thanks for the ping 🙂 , I checked with the team a few days ago and it was suggested that I add a flag to allow having the loosen/generic types alongside the literal/union ones, so that we can have both the stricter/robust types alongside loosen/generic ones if needed (to try to satisfy everyone)

That's what I did with the new --loose-vars flag (the name can still change).

I've also rebased the PR, it's ready to be re-reviewer and hopefully merged soon 🙂

@jfsiii so sorry for the late action here 😓 (not to mention @DaniFoldi 😅 🙇)

@dario-piotrowicz dario-piotrowicz changed the title fix: widen multi-env vars types in wrangler types widen multi-env vars types in wrangler types and add --loose-vars option to the command Jan 1, 2025
packages/wrangler/src/type-generation/index.ts Outdated Show resolved Hide resolved
packages/wrangler/src/type-generation/index.ts Outdated Show resolved Hide resolved
@@ -603,6 +603,35 @@ describe("generateTypes()", () => {
`);
});

it("should respect the loose-vars option", async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

I was considering adding a test for vars generation without loose-vars, but since that's the default behavior (and given the "vars present in multiple environments" describe block) that felt a bit overkill here 😕 I'm happy to do it if someone thinks that that would be good

Copy link
Member Author

Choose a reason for hiding this comment

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

actually given the "vars present in multiple environments" describe block, this test might be a bit overkill... I'm also happy to remove it entirely 🙂

@dario-piotrowicz dario-piotrowicz marked this pull request as ready for review January 1, 2025 16:42
@dario-piotrowicz
Copy link
Member Author

@petebacondarwin, @andyjessop please have a look when you get the chance 🙏

@dario-piotrowicz dario-piotrowicz changed the title widen multi-env vars types in wrangler types and add --loose-vars option to the command widen multi-env vars types in wrangler types and add --strict-vars option to the command Jan 2, 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.

In general, could you add some more comments explaining the logic? There's some fairly complicated code here

.changeset/unlucky-timers-sort.md Outdated Show resolved Hide resolved
packages/wrangler/src/type-generation/index.ts Outdated Show resolved Hide resolved
packages/wrangler/src/type-generation/index.ts Outdated Show resolved Hide resolved
@dario-piotrowicz
Copy link
Member Author

dario-piotrowicz commented Jan 7, 2025

In general, could you add some more comments explaining the logic? There's some fairly complicated code here

All the types gen logic here seems to me fairly complicated but without any comments, so I was just trying to adhere to the code style in these files, if code consistency is not a concern I'll add some comments to the new logic 👍

Amend: 👆 sorry I misremembered, that's not actually true, there are comments in some places but not in others

.changeset/proud-forks-build.md Outdated Show resolved Hide resolved
.changeset/proud-forks-build.md Outdated Show resolved Hide resolved
.changeset/unlucky-timers-sort.md Outdated Show resolved Hide resolved
.changeset/unlucky-timers-sort.md Outdated Show resolved Hide resolved
@dario-piotrowicz
Copy link
Member Author

@penalosa I've simplified the logic I had (alongside the var values I was also needlessly collecting their environment name, I did so as it might come in handy in the future, but I think that it's just better to keep things simple and add it later if the extra complexity becomes necessary) and I added a few comments, hopefully the code should be simpler to follow now, please have another look and let me know 🙏

@@ -262,11 +247,14 @@ async function generateTypes(
);
}

const envTypeStructure: string[] = [];
const envTypeStructure: [string, string][] = [];
Copy link
Member Author

Choose a reason for hiding this comment

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

I think it would be nicer/clearer to have this as

const envTypeStructure: {
 key: string,
 type: string,
}[] = [];

but it would make all the .push calls below much more verbose, and the current code should be clear enough

but I am happy with either solution, if anyone prefers the more verbose version (or has any other idea) I'm happy to change this

@dario-piotrowicz
Copy link
Member Author

@petebacondarwin sorry I had to change the code significantly after your approval 😓 🙇 , if you would please have another look (PS: anyways the functionality didn't change, I just cleaned up the code and fixes something I broke)

@dario-piotrowicz dario-piotrowicz force-pushed the wrangler-types-string-literals branch from e366aed to e4d2892 Compare January 8, 2025 16:16
@dario-piotrowicz dario-piotrowicz force-pushed the wrangler-types-string-literals branch from e4d2892 to 5405cd8 Compare January 8, 2025 21:16
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.

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