arcanis / clipanion

Type-safe CLI library / framework with no runtime dependencies
https://mael.dev/clipanion/
1.1k stars 61 forks source link

Error when expecting number type in run return value #158

Closed ktroach closed 6 months ago

ktroach commented 6 months ago

I was getting this error in my own project, which uses a library called contentlayer, which has a dependency on clipanion. See Stack Trace in the error below:

Warning: Contentlayer might not work as expected on Windows Generated 5 documents in .contentlayer TypeError: The "code" argument must be of type number. Received an instance of Object at process.set [as exitCode] (node:internal/bootstrap/node:124:9) at Cli.runExit (C:\Users\wayof\Developments\next-contentlayer\node_modules\clipanion\lib\advanced\Cli.js:232:26)
at run (file:///C:/Users/wayof/Developments/next-contentlayer/node_modules/@contentlayer/cli/src/index.ts:39:3)
at main (C:\Users\wayof\Developments\next-contentlayer\node_modules\contentlayer\bin\cli.cjs:5:3) { code: 'ERR_INVALID_ARG_TYPE' }

At this line of code in clipanion:

   node_modules\clipanion\lib\advanced\Cli.js:232:26)  

Error being thrown in runExit():

    async runExit(input, context) {
        process.exitCode = await this.run(input, context);
    }

It is expecting a number type, but actually getting back an object from the Promise. I changed it locally and here is how I confirmed it:

    async runExit(input, context) {
        const result = await this.run(input, context);
        console.log(">>>> result >>> ", result);
        process.exitCode = 0;
    }

Here is the actual output from the change I made locally:

Warning: Contentlayer might not work as expected on Windows Generated 5 documents in .contentlayer

result >>> { documentCount: 5 }

Instead of returning a number, clipanion is returning an object. This does not affect the run behavior, which seems to work fine, only the runExit() step. But I think this might be affecting several things that depend on this library. Just wanted to bring it to your awareness. Thanks!

arcanis commented 6 months ago

Clipanion itself doesn't create an object similar to {documentCount: 5}, so I suspect contentlayer is returning an invalid object from one of their commands. The execute function is only supposed to return an exit code, so if that's not the case it's expected that things don't work somewhere down the line.