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: ensure Vitest Pool Workers doesn't error with nodejs_compat_v2 flag #7278

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

Conversation

andyjessop
Copy link
Contributor

Fixes #7156.

In Vitest Pool Workers, we assert the presence of the nodejs_compat flag, but it is also possible to use the nodejs_compat_v2 flag, so we have to account for this possibility when asserting the validity of the config file.

This PR creates a new class to handle the validation, simplifying the logic as well as the testing. Some small improvements alongside adding the nodejs_compat_v2 functionality:

  • you can now add console logs to the code and see the logs in the unit tests
  • the tests now run faster
  • the code does not assume where the options path is located (i.e. miniflare)
  • it is now clear how to add extra functionality should it be required - i.e. just add an extra assertion
  • each test setup has a separate test block to allow for simpler debugging.

  • 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:
  • Public documentation
    • TODO (before merge)
    • Cloudflare docs PR(s):
    • Documentation not necessary because: Bug fix

@andyjessop andyjessop added the e2e Run e2e tests on a PR label Nov 18, 2024
@andyjessop andyjessop requested a review from a team as a code owner November 18, 2024 11:43
Copy link

changeset-bot bot commented Nov 18, 2024

🦋 Changeset detected

Latest commit: 648b797

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

This PR includes changesets to release 1 package
Name Type
@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 Nov 18, 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/11894006724/npm-package-wrangler-7278

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

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

Or you can use npx with this latest build directly:

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

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:* 3.20241106.0
workerd 1.20241106.1 1.20241106.1
workerd --version 1.20241106.1 2024-11-06

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I think flag-assertions is too generic. Can it be called compatibility-flag-assertions etc?

Comment on lines 351 to 375
const assertions = [
() =>
flagAssertions.assertDisableFlagNotPresent("export_commonjs_namespace"),
() =>
flagAssertions.assertEnableFlagOrCompatibilityDate(
"export_commonjs_default",
{
compatibilityDate: runnerWorker.compatibilityDate,
defaultOnDate: "2022-10-31",
}
),
() =>
flagAssertions.assertUnionOfEnableFlags([
"nodejs_compat",
"nodejs_compat_v2",
]),
];

for (const assertion of assertions) {
const result = assertion();
if (!result.isValid) {
throw new Error(result.errorMessage);
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Miniflare already exposes a function for working out what the current node_compat mode we are in (it is not trivial due to the negation flags).

See

I think that isFlagPresent() is actually too simple to cover the cases.

I would argue that we should just use the getNodeCompat() function from Miniflare, except that I see that we also need this other export_commonjs_namespace compat flag, which potentially also has complexity due to compat date default and negation.

I propose that if we are going to have this logic here, we just create a function called isEnabled(enableFlag: string, disableFlag: string, defaultOnDate?: string), then you could just do:

if (!compatFlagAssertions.isEnabled("export_commonjs_default", "export_commonjs_namespace", "2022-10-31")) {
  throw new Error("export_commandjs_default must be enabled - either explicitly or by compat date being after 2022-10-31");
}
if (
  !compatFlagAssertions.isEnabled("nodejs_compat", "no_nodejs_compat") &&
  !compatFlagAssertions.isEnabled("nodejs_compat_v2", "no_nodejs_compat_v2")
  ) {
  throw new Error("Node.js compatibility must be enabled - by adding either `nodejs_compat` or `nodejs_compat_v2` compat flag");
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At the moment, we also have messaging depending on the options path, the wrangler path, and the relative project path. Are you suggesting we just drop that for a simpler set of messages? Most of the code in this class is dealing with messaging, so being liberated from that would simplify things significantly.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK looking at the code again, I think we could just reuse most of the logic in assertCompatibiltyFlagEnabled() but update it so that enableFlag: string becomes enableFlags: string[] and similarly with disableFlag: string becoming disableFlags: string[].

The logic just needs a bit of tweaking to ensure that none of the disableFlags are in there and either at least one of the enableFlags is there or the compatibility date is new enough.

So then still throwing inside that function.

Copy link
Contributor

Choose a reason for hiding this comment

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

And if you still want to move it out into a class that is cool too!

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've updated this to consolidate the assertDisableFlagNotPresent and assertEnableFlagOrCompatibilityDate methods, but will wait for discussion on the messaging before proceeding further with the refactor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry Pete, I didn't see your comment before pushing my latest update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e2e Run e2e tests on a PR
Projects
Status: In Review
2 participants