orval icon indicating copy to clipboard operation
orval copied to clipboard

Bun: Schema compilation fails when using bun as package manager

Open cassis163 opened this issue 1 year ago β€’ 22 comments

What are the steps to reproduce this issue?

  1. Use bun 1.1.13
  2. bun create [email protected] --template=react-swc-ts
  3. Install orval 6.30.2
  4. Try anything, this was my config file:
import { defineConfig } from "orval";

export default defineConfig({
  "backend-api": {
    output: {
      mode: "single",
      target: "src/generated/api.ts",
      schemas: "src/generated/model",
      client: "swr",
      // mock: true,
      override: {
        mutator: {
          path: "src/api/axios-instance.ts",
          name: "customInstance",
          default: true,
        },
      },
    },
    input: {
      target: "http://localhost:8080/api/v3/api-docs",
    },
  },
});

What happens?

Syntax error emerges when the program tries to compile a schema after calling it.

What were you expecting to happen?

I was expecting to Orval generate some types.

Any logs, error output, etc?

➜ bun run gen:api  
$ orval --config orval.config.ts
Error compiling schema, function code: const schema16 = scope.schema[10];return function validate14(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;if((!(data && typeof data == "object" && !Array.isArray(data))) && (data !== null)){const err0 = {instancePath,schemaPath:"#/type",keyword:"type",params:{type: schema16.type},message:"must be object,null"};if(vErrors === null){vErrors = [err0];}else {vErrors.push(err0);}errors++;}if(data && typeof data == "object" && !Array.isArray(data)){for(const key0 in data){if(!(key0 === "keyedBy")){const err1 = {instancePath,schemaPath:"#/additionalProperties",keyword:"additionalProperties",params:{additionalProperty: key0},message:"must NOT have additional properties"};if(vErrors === null){vErrors = [err1];}else {vErrors.push(err1);}errors++;}}if(data.keyedBy !== undefined){if(typeof data.keyedBy !== "string"){const err2 = {instancePath:instancePath+"/keyedBy",schemaPath:"#/properties/keyedBy/type",keyword:"type",params:{type: "string"},message:"must be string"};if(vErrors === null){vErrors = [err2];}else {vErrors.push(err2);}errors++;}}}if(errors > 0){const emErrors0 = {"type":[]};for(const err3 of vErrors){if(((((({"str":"err3"}.keyword !== "errorMessage") && (!{"str":"err3"}.emUsed)) && ({"str":"err3"}.instancePath === instancePath)) && ({"str":"err3"}.keyword in {"str":"emErrors0"})) && ({"str":"err3"}.schemaPath.indexOf("#") === 0)) && (/^\/[^\/]*$/.test({"str":"err3"}.schemaPath.slice(1)))){{"str":"emErrors0"}[{"str":"err3"}.keyword].push({"str":"err3"});{"str":"err3"}.emUsed = true;}}for(const key1 in emErrors0){if({"str":"emErrors0"}[{"str":"key1"}].length){if(vErrors === null){vErrors = [{"str":"err4"}];}else {vErrors.push({"str":"err4"});}errors++;}}const emErrs0 = [];for(const err5 of vErrors){if(!{"str":"err5"}.emUsed){{"str":"emErrs0"}.push({"str":"err5"});}}vErrors = emErrs0;errors = {"str":"emErrs0"}.length;}validate14.errors = vErrors;return errors === 0;}
/Users/somedude/Projects/project-top-secret/frontend/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/compile/index.js:120
        throw e;
        ^

SyntaxError: Unexpected token ':'
    at new Function (<anonymous>)
    at Ajv.compileSchema (node_modules/@stoplight/spectral-core/node_modules/ajv/dist/compile/index.js:89:30)
    at Ajv._compileSchemaEnv (node_modules/@stoplight/spectral-core/node_modules/ajv/dist/core.js:473:37)
    at Ajv.compile (/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/core.js:160:38)
    at createRulesetFunction (node_modules/@stoplight/spectral-core/dist/ruleset/function.js:68:80)
    at Object.<anonymous> (node_modules/@stoplight/spectral-functions/dist/alphabetical.js:30:61)
    at Module._compile (node:internal/modules/cjs/loader:1434:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1518:10)
    at Module.load (node:internal/modules/cjs/loader:1249:32)
    at Module._load (node:internal/modules/cjs/loader:1065:12)

Node.js v22.2.0
error: script "gen:api" exited with code 1

Any other comments?

No

What versions are you using?

Operating System: MacOS Package Version: 6.30.2

cassis163 avatar Jun 13 '24 19:06 cassis163

Is possibly related to:

  • https://github.com/anymaniax/orval/issues/326
  • https://github.com/anymaniax/orval/issues/320

Installing ajv as a dev dependency did not resolve the error. Although, I did not call the bun equivalent of npm dedupe after retrieving the package.

cassis163 avatar Jun 14 '24 18:06 cassis163

yeah @cassis163 i think its definitely related to those.

melloware avatar Jun 14 '24 19:06 melloware

Hi, @cassis163 Hmm, what happens if you explicitly install "ajv"?

bun add -D ajv
bun run gen:api

soartec-lab avatar Jun 16 '24 04:06 soartec-lab

Hello @soartec-lab , still the same exact issue after bun add --dev ajv.

➜ bun run gen:api
$ orval --config orval.config.ts
Error compiling schema, function code: const schema16 = scope.schema[10];return function validate14(data, {instancePath="", parentData, parentDataProperty, rootData=data}={}){let vErrors = null;let errors = 0;if((!(data && typeof data == "object" && !Array.isArray(data))) && (data !== null)){const err0 = {instancePath,schemaPath:"#/type",keyword:"type",params:{type: schema16.type},message:"must be object,null"};if(vErrors === null){vErrors = [err0];}else {vErrors.push(err0);}errors++;}if(data && typeof data == "object" && !Array.isArray(data)){for(const key0 in data){if(!(key0 === "keyedBy")){const err1 = {instancePath,schemaPath:"#/additionalProperties",keyword:"additionalProperties",params:{additionalProperty: key0},message:"must NOT have additional properties"};if(vErrors === null){vErrors = [err1];}else {vErrors.push(err1);}errors++;}}if(data.keyedBy !== undefined){if(typeof data.keyedBy !== "string"){const err2 = {instancePath:instancePath+"/keyedBy",schemaPath:"#/properties/keyedBy/type",keyword:"type",params:{type: "string"},message:"must be string"};if(vErrors === null){vErrors = [err2];}else {vErrors.push(err2);}errors++;}}}if(errors > 0){const emErrors0 = {"type":[]};for(const err3 of vErrors){if(((((({"str":"err3"}.keyword !== "errorMessage") && (!{"str":"err3"}.emUsed)) && ({"str":"err3"}.instancePath === instancePath)) && ({"str":"err3"}.keyword in {"str":"emErrors0"})) && ({"str":"err3"}.schemaPath.indexOf("#") === 0)) && (/^\/[^\/]*$/.test({"str":"err3"}.schemaPath.slice(1)))){{"str":"emErrors0"}[{"str":"err3"}.keyword].push({"str":"err3"});{"str":"err3"}.emUsed = true;}}for(const key1 in emErrors0){if({"str":"emErrors0"}[{"str":"key1"}].length){if(vErrors === null){vErrors = [{"str":"err4"}];}else {vErrors.push({"str":"err4"});}errors++;}}const emErrs0 = [];for(const err5 of vErrors){if(!{"str":"err5"}.emUsed){{"str":"emErrs0"}.push({"str":"err5"});}}vErrors = emErrs0;errors = {"str":"emErrs0"}.length;}validate14.errors = vErrors;return errors === 0;}
/Users/me/Projects/project-x/frontend/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/compile/index.js:120
        throw e;
        ^

SyntaxError: Unexpected token ':'
    at new Function (<anonymous>)
    at Ajv.compileSchema (/Users/project/frontend/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/compile/index.js:89:30)
    at Ajv._compileSchemaEnv (/frontend/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/core.js:473:37)
    at Ajv.compile (/project/frontend/node_modules/@stoplight/spectral-core/node_modules/ajv/dist/core.js:160:38)
    at createRulesetFunction (project-/node_modules/@stoplight/spectral-core/dist/ruleset/function.js:68:80)
    at Object.<anonymous> (node_modules/@stoplight/spectral-functions/dist/alphabetical.js:30:61)
    at Module._compile (node:internal/modules/cjs/loader:1434:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1518:10)
    at Module.load (node:internal/modules/cjs/loader:1249:32)
    at Module._load (node:internal/modules/cjs/loader:1065:12)

Node.js v22.2.0
error: script "gen:api" exited with code 1

cassis163 avatar Jun 16 '24 12:06 cassis163

@cassis163

Oh...thanks for trying. Actually, I had the same problem and I solved it by explicitly installing ajv. The problem has been resolved since v1.1.11.

https://bun.sh/blog/bun-v1.1.11#fixed-bun-install-peer-dependencies-resolution

I haven't reproduced it in my project using Bun v1.1.13 and orval 6.30.2.

soartec-lab avatar Jun 17 '24 00:06 soartec-lab

Experiencing a similar issue - but with yarn as package manager. Oddly it works when I install orval 6.30.2 as the only package in an isolated environment.

In my working environment however where we have quite many dependencies (angular, cdk, etc.) orval refuses to start - even npx orval --help fails with the same error as above. We also have ajv as a dependency in our package.json and I tried to update this to the latest version but without any luck.

Can confirm that it worked quite well five days ago.

The stack trace refers to the @stoplight/spectral-core module. When I do a yarn why on this module it shows me some dependency to @ibm-cloud:

info Reasons this module exists
   - "orval#@orval#core#@ibm-cloud#openapi-ruleset#@stoplight#spectral-functions" depends on it
   - Hoisted from "orval#@orval#core#@ibm-cloud#openapi-ruleset#@stoplight#spectral-functions#@stoplight#spectral-core"
   - Hoisted from "orval#@orval#core#@ibm-cloud#openapi-ruleset#@stoplight#spectral-formats#@stoplight#spectral-core"
   - Hoisted from "orval#@orval#core#@ibm-cloud#openapi-ruleset#@stoplight#spectral-rulesets#@stoplight#spectral-core"

not sure if this helps.

mmakulla-ci avatar Jun 17 '24 12:06 mmakulla-ci

Investigated a little further and was able to reproduce the issue with the following Dockerfile

FROM node:20

RUN echo '{\
	"dependencies": {\
		"@middy/validator": "5.4.0",\
		"orval": "6.30.2"\
	}\
}' > package.json

RUN yarn install

CMD ["npx", "orval", "--help"]

My guess is that it has something to do with the versions of ajv. In my node_modules folder the package.json of orval states ajv as a dependency with the version ^8.12.0, while the @middy/validator package lists ajv with version 8.14.0.

The also somehow related @stoplight/spectral-core package uses ^8.6.0 for ajv.

mmakulla-ci avatar Jun 18 '24 09:06 mmakulla-ci

Had the same issue using node 20.17.0 and npm 10.8.2 after upgrading some unrelated dependencies. Running npm i -D ajv "fixed" it for me, though npm i -D [email protected] (the version set in orval's dependencies) did not.

Wasn't able to reporoduce it in a minimal (not) working example unfortunately.

1nVitr0 avatar Sep 18 '24 09:09 1nVitr0

For me running the orval command with npx fixed the issue, without having to install ajv or what not

emanuelet avatar Sep 24 '24 07:09 emanuelet

For me the issue remains; both when running with bun and when running with npx. I've also tried to install ajv (both ajv@latest as well as [email protected]). Both solutions don't work. Any quick fix for that?

EDIT: After installing orval@latest, ajv@latest, deleting my lockfile and node_modules/ and reinstalling dependencies orval seems to be working again.

loicnestler avatar Oct 02 '24 15:10 loicnestler

I just ran into the same issue. And fllowing the steps listed in EDIT of @loicnestler's message I got it to work again 🀷

isoroka-plana avatar Dec 17 '24 16:12 isoroka-plana

I gonna update ajv to latest in orval later.

soartec-lab avatar Dec 23 '24 12:12 soartec-lab

I try update ajv to latest version in #1771. If the problem persists, please reopen this issue.

soartec-lab avatar Dec 27 '24 01:12 soartec-lab

As per today I face the same issue.

Bun 1.2.7 and latest orval

mrctrifork avatar Mar 30 '25 20:03 mrctrifork

@mrctrifork i reopened.

melloware avatar Mar 30 '25 20:03 melloware

Sorry for my last comment; i got the contents of the files mixed. It appears to be working on node but not on bun as this issue already claims πŸ™πŸ»

mrctrifork avatar Mar 30 '25 22:03 mrctrifork

After running some tests:

Using npm i orval --save-dev instead of bun i --dev makes it work. This is likely to not be a problem with the lib itself but rather something related to bun and the way it handles postinstall or something alike

mrctrifork avatar Mar 30 '25 22:03 mrctrifork

I tried bun add -D ajv, then deleted node_modules and reinstalled the dependencies, and it worked!

bun version: v1.2.9 orval version: v7.8.0 ajv version: v8.17.1

OiAnthony avatar Apr 11 '25 07:04 OiAnthony

bun version?

mrctrifork avatar Apr 11 '25 08:04 mrctrifork

bun version?

bun version: v1.2.9 orval version: v7.8.0 ajv version: v8.17.1

OiAnthony avatar Apr 11 '25 08:04 OiAnthony

For me it still "doesn't work".

I've tried in a "separate" project and it worked. I installed orval globally and it worked. The only place where it doesn't work is in a monorepo setup.

The "separate" project is a repository where I have a regular react application with vite. And it just worked.

The project where i am having issues is a monorepo setup in the following way:

Image

If I install orval globally bun add orval -g and then I just do orval it just works.

which orval
{my-home}/.bun/bin/orval

Whenever I install it in the apps/frontend as follows it just doesn't work:

bun add orval --dev
# bun add ajv --dev # tried with and without this one
bunx orval # crashes
bunx --bun orval # crashes
orval # globally available, works

mrctrifork avatar Apr 11 '25 09:04 mrctrifork

I tried bun add -D ajv, then deleted node_modules and reinstalled the dependencies, and it worked!

bun version: v1.2.9 orval version: v7.8.0 ajv version: v8.17.1

It solved the same issue for me, thx.

bun version: v1.2.10 orval version: v7.9.0 ajv version: v8.17.1

sd44 avatar Jun 05 '25 11:06 sd44

We just released 8.0.0-rc.0 which is fully ESM if you would like to retest please and report back.

melloware avatar Oct 28 '25 01:10 melloware

Hey, I've gotten around trying this and it worked. Thanks a ton @melloware

I ran:

bunx --bun [email protected]

mrctrifork avatar Nov 16 '25 16:11 mrctrifork

Awesome!

melloware avatar Nov 16 '25 17:11 melloware

Hey @melloware , I think this is partially fixed but no luck when using a local orval config here.

For example, this seems to work fine:

bunx --bun [email protected] --input https://cddapitrial.azurewebsites.net/swagger/v4/swagger.json --output ./src/services/scope/client.generated.ts

But when running

bunx --bun [email protected] --config ./orval.config.ts

Or by calling the local bin through the locally installed package (installed through bun)

"generate:scope:client": "orval --config ./orval.config.ts",

I always land in the following:

$ bunx --bun [email protected] --config ./orval.config.ts
🍻 orval v8.0.0-rc.2 - A swagger client generator for typescript
Error compiling schema, function code: const schema16 = scope.schema[10];return function validate14(data, {insta

Just in case, here's my current orval.config.ts to illustrate I'm not doing anything fancy there

import "dotenv/config";
import { defineConfig } from "orval";

export default defineConfig({
  "scope-api": {
    input: {
      target: "https://cddapitrial.azurewebsites.net/swagger/v4/swagger.json",
    },
    output: {
      baseUrl: process.env.SCOPE_API_URL,
      client: "fetch",
      target: "./src/services/scope/client/client.generated.ts",
    },
  },
});

And I'm definitely on the right version locally:

Image

RobbyUitbeijerse avatar Nov 24 '25 16:11 RobbyUitbeijerse