generator icon indicating copy to clipboard operation
generator copied to clipboard

Cannot use async-api generator in projects that use ts-node

Open aimed opened this issue 3 years ago • 9 comments

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.

aimed avatar Mar 24 '22 15:03 aimed

I'd be happy to open a PR that removes support for source maps as well as typescript :)

aimed avatar Mar 24 '22 15:03 aimed

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.

github-actions[bot] avatar Mar 24 '22 16:03 github-actions[bot]

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.

magicmatatjahu avatar Mar 29 '22 14:03 magicmatatjahu

Fix https://github.com/asyncapi/generator/pull/764

@aimed I also tested it on your repo and everything works as expected :)

magicmatatjahu avatar Mar 29 '22 15:03 magicmatatjahu

:tada: This issue has been resolved in version 1.9.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

asyncapi-bot avatar Apr 05 '22 13:04 asyncapi-bot

Awesome, thanks so much for this @magicmatatjahu 🚀

aimed avatar Apr 29 '22 12:04 aimed

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 avatar Sep 05 '22 23:09 nodify-at

@nodify-at Alternative with env is better. Do you wanna create PR for that? :)

magicmatatjahu avatar Sep 06 '22 06:09 magicmatatjahu

@nodify-at Alternative with env is better. Do you wanna create PR for that? :)

@magicmatatjahu yes :) I will create a PR today, thanks for your feedback.

nodify-at avatar Sep 06 '22 11:09 nodify-at

fixed with https://github.com/asyncapi/generator/pull/826

derberg avatar Oct 11 '22 12:10 derberg

@derberg Great :) thanks!

nodify-at avatar Oct 11 '22 13:10 nodify-at