airtasker / spot

Spot is a concise, developer-friendly way to describe your API contract.
Other
555 stars 38 forks source link

Spot doesn't work in a monorepo #2059

Open ipekt opened 1 year ago

ipekt commented 1 year ago

Describe the bug Spot outputs TS errors when spot generate command is run in an monorepo. But the same code works fine when it is outside a monorepo. This may be an issue with TS looking at parent node_modules folder

To Reproduce

  1. Create a package.json with serverless in root folder:

    {
    "dependencies": {
    "serverless": "^3.15.0"
    }
    }
  2. Create another package.json in a nested folder: (root/spot)

{
   "dependencies": {
    "@airtasker/spot": "^1.9.0"
  }
}
  1. Add api.ts file
  2. Run spot generate --contract api.ts --language yaml --generator openapi3 --out output/
  3. Exists with errors shared below

Expected behavior spot generate works in a monorepo setup

Screenshots Example command line output:

...

 ';' expected.
    Cannot find name '$'. Do you need to install type definitions for jQuery? Try `npm i @types/jquery`.
    ';' expected.
    Cannot find name 'infer'.
    ';' expected.
    Cannot find name 'T'.
    Cannot find name '$'. Do you need to install type definitions for jQuery? Try `npm i @types/jquery`.
    ';' expected.
    Cannot find name 'infer'.
    ';' expected.
    Cannot find name 'U'.
    ';' expected.
    Cannot find name '$'. Do you need to install type definitions for jQuery? Try `npm i @types/jquery`.
    ';' expected.
    Cannot find name 'T'.
    Cannot find name '$'. Do you need to install type definitions for jQuery? Try `npm i @types/jquery`.
    ';' expected.
    'PascalCase' only refers to a type, but is being used as a value here.
    Cannot find name 'U'.
    '(' expected.
    ';' expected.
    'PropertyKey' only refers to a type, but is being used as a value here.
    ';' expected.
    ';' expected.
    ';' expected.
    This expression is not callable.
    Unterminated template literal.

Desktop (please complete the following information):

Darkle commented 1 year ago

I am also running in to this issue.

dhmw commented 1 year ago

Same here.

There seems to be very few tools available for generating API specs from TS types.

This project looks very promising and I like the idea of the contract declaration to have some degree of control over what is exposed in the spec as well.

I've tried some of the others as well (ones which also integrate with serverless framework) - and they too are also all broken and/or abandoned.

It's a shame, because something like this will save a lot of time and effort in allowing 3rd parties to integrate on our APIs without having to do all the spec and client implementation work manually.

dhmw commented 1 year ago

This appears to be mostly due to the really old version of ts-morph that spot uses; I have a local copy updated to ts-morph@18.0.0 and most of these errors go away. I'm still tracking down some other incompatibilities though.

dhmw commented 1 year ago

PR to update dependencies just enough to make it work https://github.com/airtasker/spot/pull/2061

EDIT: I have been going deeper using this tool on my types, and I have to say that the definition of "work" is actually quite limited. I don't think I will be able to continue to use this tool with my project, since the types we have are quite complex and the parsing is failing in unknown ways; there's quite a bit of TS which is simply not supported/supportable here. Given that the tool likes to bail out completely rather than skip over parts it cannot handle, it makes debugging the generator and our project types almost impossible, and certainly more work than writing a spec from scratch.

❯ yarn run spot generate -c src/contracts/test.ts --out dist/spec --language yaml --generator openapi3
    Error: unknown type