Code Monkey home page Code Monkey logo

Comments (9)

Rmannn avatar Rmannn commented on June 2, 2024 1

I tried by syncing the node version. No luck, no bug.

By stepping through the call stack, I notice the error is due to the this.listeners array does not contain the schema:sync command in node_modules/commander/index.js:891. It then throws an error which goes to the catch block in console.ts and the process rightfully exits.

The this.listeners can not contain the schema:sync command. This command is not registered on commander and had nothing to do with it. Only the entry point cli.js (from typeorm) can understand schema:sync command.
Note that the nestjs-console module is not doing anything to your app instance outside the context of the bootstrap console.ts/js file.

Does nestjs-console check/lift/sync yargs based commands as well? It seems to hijack the entrypoint for all commands and not only Commander based commands.

I searched and it seems commander is not using yargs.
nestjs-console is not invoked when you call the typeorm command. It is initialized and working only if you use the console.ts/js file as entry point. The init and boot are done in this file only.

So the first thing to check is WHY the stack contains code from the nestjs-console module when you call the cli of typeorm.
Is really the nestjs-console module that is throwing the error ? If yes, why ? It shouldn't be in the stack.
it's a different context, like a different application.

Maybe you can try to investigate here, this is the only place where the console module is throwing a command not found error.

// listen for not found on child
command.on('command:*', (args: string[]) => {
throw new Error(`"${args[0]}" command not found`);
});

Maybe you have a global node module that is interacting on your computer ?
Could you try on a fresh node install ?

thx.

from nestjs-console.

Rmannn avatar Rmannn commented on June 2, 2024

Be sure to have commander 4.1.1 and not v5.0.0
Commander has published a new major version and the console module is not yet ready for it.

from nestjs-console.

MichaelHindley avatar MichaelHindley commented on June 2, 2024

This was indeed the case! tyvm

from nestjs-console.

MichaelHindley avatar MichaelHindley commented on June 2, 2024

I may have been a bit fast, turns out I was standing in a branch where the console was not added. This behaviour is observed on [email protected] as well.

I have setup a repo here:
https://github.com/MichaelHindley/nestjs-console-repro

Run npm i && npm build to setup.
Then, running npm run typeorm schema:sync results in:

"schema:sync" command not found

removing the ConsoleModule from app.module.ts fixes it.

from nestjs-console.

Rmannn avatar Rmannn commented on June 2, 2024

Thanks for the repo, let me have a look and come back to you.

from nestjs-console.

Rmannn avatar Rmannn commented on June 2, 2024

After clone, i had to add the import of the ConsoleModule in the AppModule.
It was not present.

Then i ran the command to be sure the console was working

ts-node -r tsconfig-paths/register src/console.ts "--help"

As expected the result was

Usage: console.ts [options] [command]

Options:
  -h, --help        output usage information

Commands:
  list <directory>  List content of a directory

Then i ran the typeorm command you gave me (after installing and starting a postgres sql server)
I had to remove the debugger flag (--inspect-brk) cause i do not have a debugger ready.

npm run typeorm schema:sync

The result was

query: START TRANSACTION
query: SELECT * FROM "information_schema"."tables" WHERE "table_schema" = current_schema() AND "table_name" = 'typeorm_metadata'
query: COMMIT
Schema syncronization finished successfully.

Everything worked well.
I am sorry, I was not able to reproduce the bug.

from nestjs-console.

MichaelHindley avatar MichaelHindley commented on June 2, 2024

(I pushed the changes missing in the repro)

Super interesting, my output for ts-node -r tsconfig-paths/register src/console.ts "--help"is the same as yours, but here is my output for npm run typeorm schema:sync

image

Removing ConsoleModule gives me the same output for npm run typeorm schema:sync as you.

By stepping through the call stack, I notice the error is due to the this.listeners array does not contain the schema:sync command in node_modules/commander/index.js:891. It then throws an error which goes to the catch block in console.ts and the process rightfully exits.

If I remove the ConsoleModule, the commander breakpoints are not even hit, this is because typeorm uses the yargs package for defining commands and not Commander.

If I remove the process.exit(1) line, I still get the error from Commander that the command is not found, but then it goes to yargs and runs the command successfully, since it does exist in that context.

Does nestjs-console check/lift/sync yargs based commands as well? It seems to hijack the entrypoint for all commands and not only Commander based commands.

I do wonder how come in your case, the error is not thrown, since the code path would result in an empty this.listeners in Commander no matter how I look at it, and thus throw an error which would lead to the process.exit(1) block every time. Could you perhaps print the members of your this.listeners array out?

node: 12.9.1
os: MacOS Catalina 0.15.4

from nestjs-console.

MichaelHindley avatar MichaelHindley commented on June 2, 2024

I can confirm that the code in console.ts was executed because of typeORM doing a require call to all files matching the glob for entities in ormconfig.json, in this particular case it was adding all .js files in the dist directory, where console.js was as a result of npm run build.

The require call was then executing the inline bootstrap code.

So that was the root cause! :)

from nestjs-console.

Rmannn avatar Rmannn commented on June 2, 2024

Nice catch ! I was testing using sources, this is why the bug was not present. The dist folder is not existing on my side.

from nestjs-console.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.