orval icon indicating copy to clipboard operation
orval copied to clipboard

Wrong exit code on failure

Open Maxim-Mazurok opened this issue 1 year ago • 5 comments

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 avatar Jul 02 '24 03:07 Maxim-Mazurok

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 avatar Jul 02 '24 03:07 Maxim-Mazurok

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);
  }
})();

Maxim-Mazurok avatar Jul 02 '24 07:07 Maxim-Mazurok

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

melloware avatar Jul 02 '24 11:07 melloware

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.

Maxim-Mazurok avatar Jul 02 '24 11:07 Maxim-Mazurok

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

melloware avatar Jul 02 '24 11:07 melloware