Switch to ESM modules #6603
Closed
lotas wants to merge 59 commits into
Closed
Conversation
1f04210 to
2744031
Compare
1569572 to
3d673b1
Compare
aad6218 to
2bd04f8
Compare
2bd04f8 to
165573c
Compare
165573c to
7503cce
Compare
lotas
commented
Oct 17, 2023
| @@ -0,0 +1,6 @@ | |||
| audience: developers | |||
| level: major | |||
Contributor
Author
There was a problem hiding this comment.
although clients are mostly untouched, and are still CJS, worth making a major
lotas
commented
Oct 17, 2023
| }; | ||
|
|
||
| const ROOT_DIR = path.resolve(__dirname, '../../..'); | ||
| const suiteName = () => { |
Contributor
Author
There was a problem hiding this comment.
this is copied from lib/testing to avoid having to implicitly depend on package from outside.
lotas
commented
Oct 17, 2023
| message: 'Internal error of sorts', | ||
| }); | ||
| }); | ||
| // suite(testing.suiteName(), function() { |
Contributor
Author
There was a problem hiding this comment.
as this whole test depends on "internal" libraries, it shouldn't be here.
figuring out where to put it
lotas
commented
Oct 17, 2023
| @@ -0,0 +1,35 @@ | |||
| { | |||
Contributor
Author
There was a problem hiding this comment.
this was added to keep clients/client clean, and separate tests that used internal libraries from the client itself.
698de97 to
960d829
Compare
To reduce footprint of this refactoring let's keep helper as it was before. Helper.js defines and exports a `helper` object the same way that it was doing before, but instead of `exports` it is using `helper` object. This allows to tests remain untouched (besides imports)
Import type assertions are still experimental and they disappoint eslint Reverting to loading files with fs.readFile instead eslint/eslint#15305
node client was using implicit dependencies, which makes it hard to keep CJS because those dependencies are ESM already. Some of those dependencies can be easily rewritten, but `retry_test.js` heavily relies on the application logic, so doesn't belong there.
Retry logic tests from node client relied on implicit internal dependencies that broken CJS/ESM interop. clients/client remain CJS and can be used in both CJS and ESM packages. Therefore we extract those tests into separate package
cleaned up test-coverage.js
befe0a4 to
63cf97b
Compare
Contributor
Author
|
closed in favor of #6627 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
In node.js ESM (
"type": "module") package can import both CommonJS packages, and ESM packages. However, commonjs package cannot import newer ESM packages.As more and more dependencies switch to ESM-only distribution, making upgrades (especially security upgrades) become hard.
Taskcluster platform consists of multiple utility (library) packages, services themselves, and clients.
Library packages:
db,api,app,config,iterate,loader,monitor,postgres,pulse,references,testing,validateServices:
auth,built-in-workers,github,hooks,index,notify,object,purge-cache,queue,secrets,web-server,worker-managerClients that are published:
taskcluster-client,taskcluster-client-web.It is not possible to only convert services packages to ESM, as they depend on library packages, which also consume 3rd-party dependencies.
Clients, however, do not depend on those, so could possibly remain in CommonJS format to avoid shipping both ESM/CJS builds.
taskcluster-clientis a published standalone package, but uses unlisted dependencies for testing:taskcluster-lib-*,taskcluster-testing. Those are not listed in package dependencies, as those packages are not installed, but are internal.This was refactored, and implicit dependencies were removed, and tests that depends on those were moved into separate
client/client-testfolderimporting
ESM requires that files would be imported with extensions.
For the same reason (I believe) running
node services/auth/src/main serverwill not work, butnode services/auth/src/main.js serverwill work__dirname,__filenameCan now be used with the help of
import.meta.url:module.parentno longer availableNot possible to do:
This will work:
Testing
Since ESM require exports to be statically analyzable, test helper functions are being rewritten:
instead of
exports.xxand dynamic injections of helper functions,helper.jsnow exposes singlehelper = {}object that is being modified by utility functions likewithDb(),withPulse()Exports
Taskcluster internal libraries expose everything as named export and as default to allow mixed usage:
Can be used both ways:
Import assertions
Node.js allows to specify import assertions since v17:
However, it is still experimental and comes with:
Also eslint doesn't implement it yet, waiting for it to be analysed.
Alternatives:
Files were migrated in dumb way
npx cjs-to-es6 path/to/filesand does not result in a working code, there are still many steps to follow:TODO
db/infrastructure/toolinglibraries/apilibraries/applibraries/configlibraries/iteratelibraries/loaderlibraries/monitorlibraries/postgreslibraries/pulselibraries/referenceslibraries/testinglibraries/validateservices/authservices/built-in-workersservices/githubservices/hooksservices/indexservices/notifyservices/objectservices/purge-cacheservices/queueservices/secretsservices/web-serverservices/worker-managerFixes #4260