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

Untangle wrangler types 🙈 #2377

Merged
merged 3 commits into from
Dec 13, 2022
Merged

Untangle wrangler types 🙈 #2377

merged 3 commits into from
Dec 13, 2022

Conversation

mrbbot
Copy link
Contributor

@mrbbot mrbbot commented Dec 9, 2022

Whilst fixing another bug, I noticed fetch was in the global typing environment for wrangler. This shouldn't be the case, as our minimum supported Node version is 16.13.0, which doesn't include global fetch. Digging deeper, I noticed lots of files were being typed with a mix of @cloudflare/workers-types, node and jest types. This meant we could happily write a Jest describe in Wrangler CLI code, and TypeScript wouldn't complain. Not good... 😅

This PR splits wrangler's directories into separate TypeScript projects, each with their own configuration, and makes sure we aren't importing files that pollute global types. This ensures files get type checked under the correct environment.

A new tsc-all.js script has been added that automatically runs tsc on each project. tsconfig-santiy.ts files have also been added with @ts-expect-error comments to make sure this doesn't happen again.

I've also fixed the type checking of fixtures, and incorporated this into our root check script.

@mrbbot mrbbot requested review from a team as code owners December 9, 2022 13:13
@changeset-bot
Copy link

changeset-bot bot commented Dec 9, 2022

🦋 Changeset detected

Latest commit: 56a15ed

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

This PR includes changesets to release 1 package
Name Type
wrangler 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

@mrbbot mrbbot marked this pull request as draft December 9, 2022 14:25
@@ -541,7 +541,7 @@ export const Handler = async ({

CLEANUP_CALLBACKS.push(stop);

waitUntilExit().then(() => {
void waitUntilExit().then(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps this should just use a .finally instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably, but we aren't really doing anything with the Promise

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm, if this throws, we probably don't want to exit with code 0? Unhandled rejections will trigger the process.on("exit") listener below too.

@github-actions
Copy link
Contributor

github-actions bot commented Dec 9, 2022

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/runs/3685450343/npm-package-wrangler-2377

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

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

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/runs/3685450343/npm-package-wrangler-2377 dev path/to/script.js
Additional artifacts:
npm install https://prerelease-registry.devprod.cloudflare.dev/runs/3685450343/npm-package-cloudflare-pages-shared-2377

@codecov
Copy link

codecov bot commented Dec 9, 2022

Codecov Report

Merging #2377 (56a15ed) into main (4bdb1f6) will increase coverage by 0.75%.
The diff coverage is 33.33%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2377      +/-   ##
==========================================
+ Coverage   70.71%   71.46%   +0.75%     
==========================================
  Files         153      153              
  Lines        9751     9644     -107     
  Branches     2550     2515      -35     
==========================================
- Hits         6895     6892       -3     
+ Misses       2856     2752     -104     
Impacted Files Coverage Δ
packages/wrangler/src/api/dev.ts 69.23% <ø> (ø)
packages/wrangler/src/dev.tsx 84.91% <ø> (ø)
packages/wrangler/src/dev/start-server.ts 62.58% <0.00%> (ø)
packages/wrangler/src/init.ts 94.56% <ø> (-0.91%) ⬇️
packages/wrangler/src/pages/dev.tsx 19.02% <0.00%> (ø)
packages/wrangler/src/dev/local.tsx 22.14% <20.00%> (-0.17%) ⬇️
packages/wrangler/src/dispatch-namespace.ts 100.00% <100.00%> (ø)
packages/wrangler/src/pages/upload.tsx 83.43% <100.00%> (ø)
.../wrangler/src/__tests__/helpers/msw/handlers/r2.ts 52.94% <0.00%> (-11.77%) ⬇️
... and 8 more

@mrbbot mrbbot force-pushed the bcoll/untangle-types branch 3 times, most recently from 46cfa63 to 7942c1d Compare December 9, 2022 17:21
@mrbbot mrbbot marked this pull request as ready for review December 9, 2022 17:49
@penalosa
Copy link
Contributor

penalosa commented Dec 9, 2022

I was joking when I said to try and change more files... 😂

.eslintrc.js Show resolved Hide resolved
.eslintrc.js Show resolved Hide resolved
fixtures/local-mode-tests/src/module.ts Show resolved Hide resolved
fixtures/node-app-pages/functions/stripe.ts Show resolved Hide resolved
fixtures/remix-pages-app/.gitignore Show resolved Hide resolved
const response = await env.ASSETS.fetch(request.url, request);
const response = await env.ASSETS.fetch(
request.url,
request as RequestInit<RequestInitCfProperties>
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this cast needed?

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 is probably a bug in @cloudflare/workers-types tbh. The cf types from the fetch handler request parameter and global fetch were incompatible.

packages/wrangler/templates/pages-template-plugin.ts Outdated Show resolved Hide resolved
packages/wrangler/templates/pages-template-worker.ts Outdated Show resolved Hide resolved
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.

Once the cf casting issue is fixed, this looks good to go!

@@ -332,7 +332,7 @@ describe("r2", () => {
"host": "api.cloudflare.com",
}
`);
response.once(
return response.once(
Copy link
Contributor

@JacobMGEvans JacobMGEvans Dec 12, 2022

Choose a reason for hiding this comment

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

ugh... ty for catching these, little disappointed the tests were passing without them returning the response properly, and I overlooked them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm, would you be able to have a look and see what's going on here?

Copy link
Contributor

Choose a reason for hiding this comment

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

After I finishing converting the other tests, for sure.

Copy link
Contributor

Choose a reason for hiding this comment

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

Likely something like this in these tests too #2377 (comment)

@@ -119,7 +119,7 @@ function* executeRequest(request: Request, relativePathname: string) {
}
}

export default function (pluginArgs) {
export default function (pluginArgs: unknown) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I am guessing this was undefined before

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was implicitly any typed, which was causing a TypeScript error:
image

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh no O_O

@mrbbot
Copy link
Contributor Author

mrbbot commented Dec 13, 2022

Once the cf casting issue is fixed, this looks good to go!

@penalosa In the interest of avoiding this PR going stale, and bearing in mind the time it will take to get a workers-types release out with this fix, would you mind if we merge this now? I've replaced the cast with a @ts-expect-error, so we'll get a type error when we fix this.

@penalosa
Copy link
Contributor

Yeah, I'm good with that—can you make an issue to fix? (I know the ts-expect-error will catch it, but just for tracking)

@mrbbot
Copy link
Contributor Author

mrbbot commented Dec 13, 2022

Updated #2390 👍

@mrbbot mrbbot merged commit 32686e4 into main Dec 13, 2022
@mrbbot mrbbot deleted the bcoll/untangle-types branch December 13, 2022 15:08
@github-actions github-actions bot mentioned this pull request Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants