workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🚀 Feature Request: Add a command to check that the generated types are in sync

Open stof opened this issue 1 year ago • 4 comments

Describe the solution

In my CI job, I'd like to have a check that the worker-configuration.d.ts file is actually in sync with the worker configuration (i.e. that devs changing the configuration don't forget to run npm run cf-typegen) so that typescript type checking can be relied on.

I attempted to create a GHA job running npm run cf-typegen to regenerate the file and then using https://github.com/marketplace/actions/verify-changed-files to make the job fail if the file is modified. However, this fails because the output of wrangler types is not reproducible. The output contains a comment with the timestamp of the generation run so the file would be modified all the time in my CI job.

I see 2 ways to solve that:

  • provide a dedicated check mode in wrangler types (for instance wrangler types --check) which would compare the existing file content with the generated content with special logic to ignore changes in the timestamp, and report differences by a failure exit code
  • provide a way to make the wrangler types output reproducible (either always or with a dedicated flag) by removing the timestamp, which would allow to use existing tooling checking for modified files after running it again.

stof avatar Aug 23 '24 16:08 stof

This sounds like it would be caught by just running tsc. If your Worker code does not match your generated types, then tsc will error. Perhaps you can try running that in your CI job?

We did talk about a wrangler types --check command recently, but we reasoned that all it would be doing is:

  1. Run wrangler types.
  2. Run tsc.

andyjessop avatar Aug 26 '24 13:08 andyjessop

Regenerating the wrangler types before doing typechecking in CI could work to ensure that tsc uses the right type declarations in CI, but this would still create confusion for other devs in the team which would see typescript errors locally.

I already have a CI job running tsc in CI. However, my goal is to enforce that the version-controlled worker-configuration.d.ts matches the version-controlled wrangler.toml

Note that my request for wrangler types --check is not for it to run type checking on the whole project. It is to check whether the generated worker-configuration.d.ts matches what wrangler types would generate based on the current wrangler.toml. And if the type generation were reproducible, I would already be able to check that by running wrangler types followed by git status (or a similar git-based detection). My blockage is that wrangler types is not reproducible due to the generation timestamp included in the comment.

stof avatar Aug 26 '24 14:08 stof

@stof this should be fixed by https://github.com/cloudflare/workers-sdk/pull/6329, which will make wrangler types reproducible

penalosa avatar Aug 27 '24 13:08 penalosa

great news. This would indeed solve my need.

stof avatar Aug 27 '24 15:08 stof