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

update node version (v18, v20, v22) #833

Merged
merged 31 commits into from
Dec 20, 2024
Merged

Conversation

doc-han
Copy link
Collaborator

@doc-han doc-han commented Nov 29, 2024

Short Description

This PR ensures that Openfn/kit tooling works on several node versions. (v18, v20 & v22)

Fixes

#542
#617

Implementation Details

1. chore: move from ts-node to swc-loader (f857884)

Removes ts-node from the codebase and adds @swc-node/register as the new loader for running ava tests.

2. ci: update CI to run tests for v18, v20 & v22 (33f777d)

Updates the CI to run unit_tests and integration_tests under the node versions 18,20, 22.

3. fix: several tests & mocks (1222c9c)

This commit fixes several mocks that seem to function in v18 but not in other node versions. Some of them have to do with functionalities that don't exist across versions and so on.

  1. Change implementation of mockSocket to use a class instead of a function.
  2. Update the import of package.json from import with assert to use fs
  3. Update wording of expected error cannot read properties of undefined to consider cannot destructure property
  4. update test that waits for workerprocess to be killed to actually be killed. Because workerProcess.kill('SIGTERM') is async

4. fix: resolve mock-fs (fc2e5e1)

  1. readFileSync with utf-8 was broken by an optimization in node. An update to [email protected] resolved that. (readFileSync with 'utf-8' option broken by node v20.5.0 tschaub/mock-fs#377)
  2. dynamic import from a mocked filesystem stopped working with mock-fs. No good issue around that. Using the real filesystem resolved that. (Can't require files with mocked filesystem tschaub/mock-fs#130)

5. test: collection tests & flaky test (4cfd339)

  1. A test issue introduced by tests accompanying collections.
  2. skipping the test "allow a job to complete after receiving a sigterm". This is because it's flaky and .kill('SIGTERM') is async.

QA Notes

List any considerations/cases/advice for testing/QA here.

AI Usage

Please disclose how you've used AI in this work (it's cool, we just want to know!):

  • Code generation (copilot but not intellisense)
  • Learning or fact checking
  • Strategy / design
  • Optimisation / refactoring
  • Translation / spellchecking / doc gen
  • Other
  • I have not used AI

You can read more details in our Responsible AI Policy

@doc-han doc-han force-pushed the farhan/update-node-version branch 4 times, most recently from 17ffdaf to 0676931 Compare November 29, 2024 08:55
@doc-han
Copy link
Collaborator Author

doc-han commented Nov 29, 2024

@josephjclark
We have some progress here.

  1. Tests run on node20 when we're using the swc loader.
  2. Hence, we currently have 2 separate CI tests for v18 and v20
  3. Most of the issues at the moment are code specific. some things that work in v18 but seem troublesome in v20. Mostly some mocks that need to be updated.

Work in progress!

@josephjclark
Copy link
Collaborator

Thank you for the update @doc-han ! Appreciate it

If anything in the core code is seriously breaking please let me know as soon as possible

@doc-han
Copy link
Collaborator Author

doc-han commented Nov 30, 2024

Thank you for the update @doc-han ! Appreciate it

If anything in the core code is seriously breaking please let me know as soon as possible

Currently, the only thing not working is mock-fs.
It had some issues due to optimizations introduced to the fs.readfilesync function.

In node 20. mock-fs fails to provide a mock when the mocked fs path is imported instead of read with fs.

This is because. with custom adaptor paths. even tho we mock the adaptor path. the runtime doesn't use fs to read the path. we rather dynamically import he index.js file at that adaptor path.

Been on it for a while now.

@josephjclark
Copy link
Collaborator

josephjclark commented Nov 30, 2024

I see. So we either need to change how the runtime imports modules (not keen on that!) or we need to find a better way to mock modules on the file system?

Or we need to work out a different way to test. We could use the actual file system to load modules, although the mock is very convenient 🤔

Di you have a link for the mock-fs and node 20 issue? Does this fix itself it node 22?

@josephjclark
Copy link
Collaborator

Hey @doc-han I think this is going to get complicated, can we divide-and-conquer a bit?

  • raise an issue for each isolated node 20 problem (ie node loaders, mock fs)
  • create an epic branch to hold all the fixes ready for release
  • create a fix branch for each specific issue, targeting the epic branch

This will make code review much easier and help me understand what's going on.

I can help tidy up the issues on Monday, just so long as I have a clear list of the problems

@doc-han
Copy link
Collaborator Author

doc-han commented Dec 1, 2024

I see. So we either need to change how the runtime imports modules (not keen on that!) or we need to find a better way to mock modules on the file system?

Or we need to work out a different way to test. We could use the actual file system to load modules, although the mock is very convenient 🤔

Di you have a link for the mock-fs and node 20 issue? Does this fix itself it node 22?

  1. readFileSync Issue: readFileSync with 'utf-8' option broken by node v20.5.0 tschaub/mock-fs#377 (fixed via a version bump to 5)

  2. don't see a good issue around mock-fs not working with dynamic import in node v20. closest issue for mock-fs@v3Can't require files with mocked filesystem tschaub/mock-fs#130

  3. Node has an issue where loader arguments are not passed down to workers. Hence. other loaders like tsx/esm actually work with ava but due to this you'll have to not spawn workers for ava test when using them. ESM loaders cannot be defined via Worker option execArgv in v20 nodejs/node#47747

// ava.config.js
...
  workerThreads: false,
...

Our current headache

when mock-fs mocks /modules/

mock({
[expressionPath]: job,
[statePath]: state,
[outputPath]: '{}',
[pnpm]: mock.load(pnpm, {}),
// enable us to load test modules through the mock
'/modules/': mock.load(path.resolve('test/__modules__/'), {}),
'/repo/': mock.load(path.resolve('test/__repo__/'), {}),
'/monorepo/': mock.load(path.resolve('test/__monorepo__/'), {}),
//'node_modules': mock.load(path.resolve('node_modules/'), {}),
[pkgPath]: mock.load(pkgPath),
...(options.mockfs ?? {}),
});

we later want to import from this mocked filesystem.
const result = import(`${prefix}${path}`);

Works in v18 but not in v20

@doc-han
Copy link
Collaborator Author

doc-han commented Dec 1, 2024

Hence, our only failing tests are in packages/cli/test/commands.test.ts

@doc-han
Copy link
Collaborator Author

doc-han commented Dec 1, 2024

Hey @doc-han I think this is going to get complicated, can we divide-and-conquer a bit?

  • raise an issue for each isolated node 20 problem (ie node loaders, mock fs)
  • create an epic branch to hold all the fixes ready for release
  • create a fix branch for each specific issue, targeting the epic branch

This will make code review much easier and help me understand what's going on.

I can help tidy up the issues on Monday, just so long as I have a clear list of the problems

Yeah we can do that.
My plan was to get things working and then with rebase, rearrange and squash some of the commits into meaningful bits.

@josephjclark
Copy link
Collaborator

Ok cool: so let's make this PR solely about #542, and any other node 20 issues we'll handle in different PRs

ava.config.js Outdated Show resolved Hide resolved
@doc-han doc-han force-pushed the farhan/update-node-version branch 3 times, most recently from d67f97b to 48285ba Compare December 4, 2024 02:35
@doc-han
Copy link
Collaborator Author

doc-han commented Dec 4, 2024

@josephjclark
The last commit 48285ba is cherry-pick of #836
will be removed after #836 gets merged and this branch will be rebased.

Resolves our test issues and as you can see. tests are passing on v18 & v20. hurray 🎉 (bed-time)

@josephjclark
Copy link
Collaborator

Node has an issue where loader arguments are not passed down to workers. Hence. other loaders like tsx/esm actually work with ava but due to this you'll have to not spawn workers for ava test when using them

Are we saying there's no fix to this? We straight up can't use ava + typescript + workers in node 20+? That would be a huge disappointment - are more people talking about this somewhere?

I'd much prefer to use worker threads, they're way less intensive. Running the tests with child processes makes my whole system burn up. I know, I've tried it!

@doc-han
Copy link
Collaborator Author

doc-han commented Dec 4, 2024

Are we saying there's no fix to this? We straight up can't use ava + typescript + workers in node 20+? That would be a huge disappointment - are more people talking about this somewhere?

We currently don't have a problem with that. SWC seems to work fine. That issue was happening with other loaders like tsx.
You'll notice that flag isn't set in ava.config.js

@doc-han
Copy link
Collaborator Author

doc-han commented Dec 4, 2024

are more people talking about this somewhere?

will get you references.

@josephjclark
Copy link
Collaborator

Ok @doc-han , thanks. So long as swc is loading that's cool, I'm happy to lean into that.

No need for references, happy to take you at your word :) Thanks

.circleci/config.yml Outdated Show resolved Hide resolved
ava.config.js Outdated Show resolved Hide resolved
@doc-han doc-han force-pushed the farhan/update-node-version branch from 48285ba to 89ffe57 Compare December 11, 2024 22:11
@doc-han doc-han changed the title chore: node support (v18, v20) chore: node support (v18, v20, v22) Dec 12, 2024
@doc-han doc-han force-pushed the farhan/update-node-version branch 2 times, most recently from 2bca944 to 9c16cdf Compare December 12, 2024 12:43
@josephjclark
Copy link
Collaborator

Starting to see some strange fails on particular node versions. Investigating - not sure how concerned to be yet. Also seeing some strange log output in CI whici I need to get to the bottom of.

Also, I think unit tests have been running wrongly? I think forever? They should be running inside a docker container to use the global openfn command - but they're not, they're running from a local build. This is probably something that needs to be looked at separately

@doc-han
Copy link
Collaborator Author

doc-han commented Dec 17, 2024

Starting to see some strange fails on particular node versions.

The uncaught exception error in unit-test is already documented under #844
I've updated the description to elaborate on what's making it error.

@josephjclark
Copy link
Collaborator

I've caught a couple of things:

  1. Incredibly, the runtime was missing an await on the module loader. It's clearly working fine synchronously (somehow!), but when there's an error it's not getting caught.
  2. This is resulting in some tests appearing to pass even though they're throwing errors internally.

I've fixed the runtime, fixed some tests, and skipped some low-value ones

@josephjclark josephjclark changed the title chore: node support (v18, v20, v22) update node: node support (v18, v20, v22) Dec 17, 2024
@josephjclark josephjclark changed the title update node: node support (v18, v20, v22) update node node support (v18, v20, v22) Dec 17, 2024
@josephjclark josephjclark changed the title update node node support (v18, v20, v22) update node support (v18, v20, v22) Dec 17, 2024
@josephjclark josephjclark changed the title update node support (v18, v20, v22) update node version (v18, v20, v22) Dec 17, 2024
@josephjclark
Copy link
Collaborator

Still some flaky tests in docgen which I'll have to keep skipping. But otherwise this is ready go. I'll test the build on the staging server.

I'm feeling pretty confident about this but the last two hours have got me a little worried. So I'l retest with a cool head tomorrow.

@josephjclark josephjclark merged commit 93cdaaa into main Dec 20, 2024
10 checks passed
@josephjclark josephjclark deleted the farhan/update-node-version branch December 20, 2024 11:20
@josephjclark
Copy link
Collaborator

Thank you so much for this @doc-han 🙏

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

Successfully merging this pull request may close these issues.

2 participants