OpenAPITools / OpenAPITools/openapi-generator

[BUG] typescript-axios(API Clients) could not build because of --typeRoots option(TypeScriptCompiler) when using yarn workspace

Open
#7,104 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

The issue is the build failure of API Clients - axios-typescript. The reason why this issue is problem for me is that I cannnot publish api client used in our project.

openapi-generator version

I use the latest version of openapi-generator (becasue it happend soon after I execute yarn add @openapitools/openapi-generator-cli).

OpenAPI declaration file content or url

The issue is not related to api specification.

Command line used for generation

I use npm.

openapi-generator generate --input-spec <specPath> --generator-name typescript-axios --output <dist> --config api.json
Steps to reproduce
  • directory structure
.
├─node_modules
├─packages
│  ├─spec
│  │  ├─api.json
│  │  ├─package.json
│  │  └─openapi.yaml
│  └─client
│    └─...autoGeneratedFiles
├─package.json
└─yarn.lock

  • ./package.json

{
  "scripts": {
    "generate:client": "yarn workspace <workspaceName-spec> generate:client",
    "client:build": "yarn workspace <workspaceName-client> build"
  },
  "dependencies": {
    "axios": "^0.18.0"
  },
  "devDependencies": {
    "@types/node": "^12.11.5",
    "lerna": "^3.22.1",
    "typescript": "^3.6.4"
  },
  "workspaces": {
    "packages": {
      "packages/*"
    }
  }
}

  • ./packages/docs/package.json
{
  "scripts": {
    "generate:client": "openapi-generator generate --input-spec ./openapi.yaml --generator-name typescript-axios ../client --config api.json"
  },
  "devDependencies": {
    "@openapitools/openapi-generator-cli": "^1.0.15-4.3.1"
  }
}

Execute yarn client:build and you can see errors below.

yarn run v1.22.4
$ yarn workspace <workspaceName> build
$ tsc --outDir dist/
api.ts:15:34 - error TS2307: Cannot find module 'url' or its corresponding type declarations.
15 import * as globalImportUrl from 'url';
                                    ~~~~~
Found 1 error.
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 2
Command: /Users/<Username>/.nodenv/versions/12.16.2/bin/node
Arguments: /Users/<Username>/Applications/homebrew/Cellar/yarn/1.22.4/libexec/lib/cli.js build
Directory: <ProjectPath>/packages/client
Output:
info Visit https://yarnpkg.com/en/docs/cli/workspace for documentation about this command.
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Related issues/PRs

In order to resolve #2394, --typeRoots was introduced.

Suggest a fix

I manually changed the generated tsconfig.json and then build succeeded.

diff is here

-    ],
-    "typeRoots": [
-      "node_modules/@types"
-    ]
+    ]

https://github.com/OpenAPITools/openapi-generator/blob/28ddad44e57109989b6fd17fffe0fa2403046102/modules/openapi-generator/src/main/resources/typescript-axios/tsconfig.mustache#L15-L17

or how about introducing options that disables the --typeRoots or enables the --typeRoots?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/typescript-axios/tsconfig.mustache, especially the linked typeRoots section, and reproduce the failure with the provided yarn workspace layout and client:build command. Compare the generated tsconfig.json with the issue's manual diff and verify that the generated client builds successfully without that manual change.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, build-system
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.