orval-labs / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
2.8k stars 311 forks source link

Wrong exit code on failure #1497

Open Maxim-Mazurok opened 2 months ago

Maxim-Mazurok commented 2 months ago

What are the steps to reproduce this issue?

  1. npx orval

What happens?

image

maxim@64QHKR3:~/BorderWiseWeb/Frontend/vue-elements$ npx orval
🍻 Start orval v6.22.1 - A swagger client generator for typescript
🛑  api - TypeError: Cannot read properties of undefined (reading 'nullable')
maxim@64QHKR3:~/BorderWiseWeb/Frontend/vue-elements$ echo $?
0

What were you expecting to happen?

Non-0 exit code

What versions are you using?

Operating System: Ubuntu Package Version: 6.22.1

Maxim-Mazurok commented 2 months ago

Workaround in package.json:

{
  "scripts": {
    "swagger:generate": "rimraf ./src/api && orval | tee /dev/tty | grep -q \"ready to use\" && exit 0 || exit 1",
  }
}
Maxim-Mazurok commented 2 months ago

Workaround script (because there's no tee/grep on Windows...)

// this script exists because https://github.com/anymaniax/orval/issues/1497

void (async () => {
  const originalConsoleLog = console.log;
  let orvalFinishedFine = false;

  const newConsoleLog: typeof console.log = (...parameters) => {
    if (typeof parameters[0] === "string" && parameters[0].includes("ready to use")) {
      // expect "🎉 api - Your OpenAPI spec has been converted into ready to use orval!"
      orvalFinishedFine = true;
    }
    if (typeof parameters[0] === "string" && parameters[0].toLowerCase().includes("error")) {
      // expect "🛑  api - TypeError: Cannot read properties of undefined (reading 'properties')"
      orvalFinishedFine = false;
    }

    originalConsoleLog.apply(console, parameters);
  };
  console.log = newConsoleLog;

  const orval = await import("orval"); // import after console.log is replaced
  await orval.generate();

  // see https://github.com/typescript-eslint/typescript-eslint/issues/9477
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  if (!orvalFinishedFine) {
    process.exit(1);
  }
})();
melloware commented 2 months ago

@Maxim-Mazurok is this something that can be fixed in Orval?

Maxim-Mazurok commented 2 months ago

Yeah, I believe so. I think I looked into it some time ago and didn't find an easy fix. The problem occurs if you have some issues in Orval config file. In my case I was trying to access some property on undefined value.

The problem was that Orval didn't return non-zero exit code, so all other scripts in CI continued to run, making it harder to find what broke the build.

melloware commented 2 months ago

Yeah I agree! Maybe there some slick place we can do a try...catch and return the exit(1)? Just thinking out loud.