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

compatibility with esbuild #1069

Closed
2 of 4 tasks
alexandervandekleutab opened this issue Aug 31, 2021 · 9 comments · Fixed by #1108
Closed
2 of 4 tasks

compatibility with esbuild #1069

alexandervandekleutab opened this issue Aug 31, 2021 · 9 comments · Fixed by #1108

Comments

@alexandervandekleutab
Copy link

alexandervandekleutab commented Aug 31, 2021

When importing stuff like Route, Body, etc from tsoa, bundling code with esbuild fails and tries to use tsoa as the cli.

Sorting

  • bug report

  • feature request

  • support request

  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit

Steps to Reproduce

  • install esbuild and tsoa
  • copy the tsoa example from the docs
  • bundle it with esbuild src/server.ts target=node --outfile=index.js
  • try running it with node index.js
  • instead of starting the server as expected, the bundled code is treated as if it were the command line tsoa function.

This can be solved by importing from @tsoa/runtime directly.

Version of the library: "tsoa": "^3.9.0"
Version of NodeJS: v14.17.0

  • Confirm you were using yarn not npm: [x]
@WoH
Copy link
Collaborator

WoH commented Sep 11, 2021

If this is not a misconfiguration, I'd suspect this would more likely be an esbuild issue.

@WoH
Copy link
Collaborator

WoH commented Sep 29, 2021

Guess I was wrong, seems like ts-node has similar issues: #1090
Marking this as a bug.

@jpmtrabbold
Copy link

Hi - a bit of more information on this bug. On the built code, there is this:

    exports2.runCLI = runCLI;
    if (!module2.parent)
      runCLI();

somehow, when built with esbuild, runCLI() is executed when the server starts, which makes the whole server process fail. Do you guys have any ideas on how to solve this? @lukeautry

@jpmtrabbold
Copy link

Also, info from here:

nodejs v15 deprecated usage of module.parent to detect if module is loaded or running as standalone:
[DEP0144] DeprecationWarning: module.parent is deprecated due to accuracy issues. Please use require.main to find program entry point instead.

From node's documentation:

"When a file is run directly from Node.js, require.main is set to its module. That means that it is possible to determine whether a file has been run directly by testing require.main === module.

For a file foo.js, this will be true if run via node foo.js, but false if run by require('./foo')."

So - would you guys be okay to try to use require.main === module instead of !module.parent?

@WoH
Copy link
Collaborator

WoH commented Oct 10, 2021

Also, info from here:

nodejs v15 deprecated usage of module.parent to detect if module is loaded or running as standalone:
[DEP0144] DeprecationWarning: module.parent is deprecated due to accuracy issues. Please use require.main to find program entry point instead.

From node's documentation:

"When a file is run directly from Node.js, require.main is set to its module. That means that it is possible to determine whether a file has been run directly by testing require.main === module.

For a file foo.js, this will be true if run via node foo.js, but false if run by require('./foo')."

So - would you guys be okay to try to use require.main === module instead of !module.parent?

TL;Dr: Yes, feel free to send a PR.

It's a better/recommended way of checking anyway and has no compatibility impact. Not sure why esbuild seems to set a parent here in the transpiled code.

@jpmtrabbold
Copy link

@WoH great! PR done, tests passed. The PR is minimal, but let me know if you have any problems with it. Thanks

@WoH WoH closed this as completed in #1108 Oct 11, 2021
@jpmtrabbold
Copy link

@WoH just so I plan accordingly - are there any releases planned soon for this fix to be available?

@WoH
Copy link
Collaborator

WoH commented Oct 16, 2021

I don't tend to give ETAs for OS stuff. But here's a release you may be interested in: v3.13.0

@jpmtrabbold
Copy link

Thanks!

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

Successfully merging a pull request may close this issue.

3 participants