generator
generator copied to clipboard
Cannot use async-api generator in projects that use ts-node
Describe the bug
When trying to run a project that uses ts-node AND async-api generator, the following error appears.
Users/x/async-api-nest/node_modules/@asyncapi/generator/node_modules/ts-node/src/index.ts:513
return new TSError(diagnosticText, diagnosticCodes)
^
TSError: ⨯ Unable to compile TypeScript:
src/app.module.ts(4,55): error TS2339: Property 'decorate' does not exist on type 'typeof Reflect'.
src/app.module.ts(4,92): error TS2339: Property 'decorate' does not exist on type 'typeof Reflect'.
src/app.module.ts(16,6): error TS2695: Left side of comma operator is unused and has no side effects.
at createTSError (/Users/x/async-api-nest/node_modules/@asyncapi/generator/node_modules/ts-node/src/index.ts:513:12)
ts-node can only be registered once, otherwise it will try to compile already compiled code, potentially leading to complication errors (depending on the compiler settings).
When using ts-node in a project that uses async-api generator, both register ts-node. The project does this when starting (node -r ts-node/register app.ts), async-api does it here: https://github.com/asyncapi/generator/blob/808783b607850d72ed88766e581c64456400e8b5/lib/utils.js#L152
How to Reproduce
Steps to reproduce the issue. Attach all resources that can help us understand the issue:
- Install async-api generator
- Install ts-node
- Import async-api generator in any file
- Run the project with
node -r ts-node/register app.ts
https://github.com/aimed/async-api-generator-ts-node-reproduction
Expected behavior
I do not expect async-api generator to modify source code at run-time AT ALL. Instead using ts-node or any other type of compilation/transpilation process should be done by the consumer application.
I'd be happy to open a PR that removes support for source maps as well as typescript :)
Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.
I admit that the current solution is bad and TS -> JS transpilation should be done on the pre-publish and not on the runtime side, but I don't agree with removing this "feature" because then we would have to do major release (because it would be breaking change). In version 2.0.0 of the generator we will remove it, and the transpilation will be done before publishing. From what I remember, ts-node patches the imports and does the transpilation before importing, so I will check if we can detect the patched imports and run registerTypeScript function or not.
Fix https://github.com/asyncapi/generator/pull/764
@aimed I also tested it on your repo and everything works as expected :)
:tada: This issue has been resolved in version 1.9.1 :tada:
The release is available on:
Your semantic-release bot :package::rocket:
Awesome, thanks so much for this @magicmatatjahu 🚀
Hi,
this issue still happens if we use ts-node-dev instead of ts-node itself. This causes to the same issue.
As a workaround I did at util.js following change to test and it worked but it is tricky :)
const { REGISTER_INSTANCE } = require('ts-node');
process[REGISTER_INSTANCE] = true;
Alternative would be to check the env. variable where ts-node sets it at star up:
const { REGISTER_INSTANCE, register } = require('ts-node');
// if the ts-node has already been registered before, do not register it again.
if (process[REGISTER_INSTANCE] || process.env.TS_NODE_DEV) {
return;
}
register();
@aimed @magicmatatjahu What do you think? Can we create a patch? It blocks us to use ts-node-dev without using the tricky code snippet :)
@nodify-at Alternative with env is better. Do you wanna create PR for that? :)
@nodify-at Alternative with
envis better. Do you wanna create PR for that? :)
@magicmatatjahu yes :) I will create a PR today, thanks for your feedback.
fixed with https://github.com/asyncapi/generator/pull/826
@derberg Great :) thanks!