Skip to content

[TypeScript] remove supportsES6 option #16187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft

Conversation

wing328
Copy link
Member

@wing328 wing328 commented Jul 26, 2023

I've filed this change to remove supportsES6 option.

If this looks good, I'll remove supportsES6 from the templates and configs. Let me know. Thanks for reviewing the change.

cc @TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01) @topce (2018/10) @akehir (2019/07) @petejohansonxo (2019/11) @amakhrov (2020/02) @davidgamero (2022/03) @mkusaka (2022/04)

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • In case you are adding a new generator, run the following additional script :
    ./bin/utils/ensure-up-to-date
    
    Commit all changed files.
  • File the PR against the correct branch: master (6.3.0) (minor release - breaking changes with fallbacks), 7.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@wing328
Copy link
Member Author

wing328 commented Jul 27, 2023

I've updated the templates to remove supportsES6 but got the build failure in CircleCI:

> [email protected] build
> tsc

../../builds/default/dist/http/http.d.ts(5,8): error TS1192: Module '"/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/builds/default/node_modules/form-data/index"' has no default export.
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (npm-build) on project TypeScriptDefaultPetstoreClientTests: Command execution failed.: Process exited with an error: 2 (Exit value: 2) -> [Help 1]
[ERROR]

https://app.circleci.com/pipelines/github/OpenAPITools/openapi-generator/24929/workflows/b16d1417-4485-4f62-805a-ca0938005456/jobs/74456

Can someone please take a look as I'm not familiar with the TypeScript client generator?

I tested that folder locally (mvn integration) and the build passed.

@amakhrov
Copy link
Contributor

@wing328 2 changes needed:

  • samples/openapi3/client/petstore/typescript/tests/default/tsconfig.json
    "compilerOptions": {
+       "esModuleInterop": true,
  • samples/openapi3/client/petstore/typescript/tests/default/test/http/isomorphic-fetch.test.ts
- import * as FormData from "form-data";
+ import FormData from "form-data";

@wing328
Copy link
Member Author

wing328 commented Jul 28, 2023

Now I got a different errors after applying the patch:

> [email protected] test
> mocha test/**/*.ts -r ts-node/register --timeout 10000

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/tests/default/node_modules/ts-petstore-client/package.json
    at new NodeError (node:internal/errors:387:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:365:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:589:7)
    at resolveExports (node:internal/modules/cjs/loader:556:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:596:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1014:27)
    at Function.Module._load (node:internal/modules/cjs/loader:873:27)
    at Module.require (node:internal/modules/cjs/loader:1100:19)
    at require (node:internal/modules/cjs/helpers:108:18)
    at Object.<anonymous> (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/tests/default/test/api/PetApi.test.ts:1:1)
    at Module._compile (node:internal/modules/cjs/loader:1198:14)
    at Module.m._compile (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/tests/default/node_modules/ts-node/src/index.ts:392:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1252:10)
    at Object.require.extensions.<computed> [as .ts] (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/tests/default/node_modules/ts-node/src/index.ts:395:12)
    at Module.load (node:internal/modules/cjs/loader:1076:32)
    at Function.Module._load (node:internal/modules/cjs/loader:911:12)
    at Module.require (node:internal/modules/cjs/loader:1100:19)
    at require (node:internal/modules/cjs/helpers:108:18)
    at /home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/tests/default/node_modules/mocha/lib/mocha.js:250:27
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/tests/default/node_modules/mocha/lib/mocha.js:247:14)
    at Mocha.run (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/tests/default/node_modules/mocha/lib/mocha.js:576:10)
    at Object.<anonymous> (/home/circleci/OpenAPITools/openapi-generator/samples/openapi3/client/petstore/typescript/tests/default/node_modules/mocha/bin/_mocha:637:18)
    at Module._compile (node:internal/modules/cjs/loader:1198:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1252:10)
    at Module.load (node:internal/modules/cjs/loader:1076:32)
    at Function.Module._load (node:internal/modules/cjs/loader:911:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:22:47
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (npm-test) on project TypeScriptDefaultPetstoreClientTests: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <args> -rf :TypeScriptDefaultPetstoreClientTests

ref: https://app.circleci.com/pipelines/github/OpenAPITools/openapi-generator/24941/workflows/f7cd6154-b216-48e3-b275-ec72078bfceb/jobs/74515

@amakhrov
Copy link
Contributor

amakhrov commented Jul 28, 2023

Ok, now it reveals a real issue. For the typescript generator, supportEs6 has an additional effect of adding "type": "module" in package.json. Now the generated code is treated as an ESM module. It then breaks the tests, because the test framework (mocha + ts-node) doesn't support ESM well. The same could also happen to a lot of real-life apps - the adoption of ESM across the ecosystem still deserves better.

@TiFu any thoughts? I think typescript is the only TS generator that emits type: module in the generated package.json file. Perhaps it needs to be driven by a separate generator option, independent on the tsconfig's target?

@wing328
Copy link
Member Author

wing328 commented Aug 9, 2023

Reverted the change in typescript templates and now I got a different errors:

> @openapitools/[email protected] prepare
> npm run build


> @openapitools/[email protected] build
> tsc && tsc -p tsconfig.esm.json

api.ts(17,70): error TS2792: Cannot find module 'axios'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
api.ts(18,25): error TS2792: Cannot find module 'axios'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
base.ts(20,25): error TS2792: Cannot find module 'axios'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
common.ts(18,51): error TS2792: Cannot find module 'axios'. Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?
npm ERR! code 2
npm ERR! path /Users/williamcheng/Code/openapi-generator7/samples/client/petstore/typescript-axios/builds/with-npm-version
npm ERR! command failed
npm ERR! command sh -c npm run build

npm ERR! A complete log of this run can be found in: /Users/williamcheng/.npm/_logs/2023-08-09T08_52_52_744Z-debug-0.log

ref: https://app.circleci.com/pipelines/github/OpenAPITools/openapi-generator/25159/workflows/fc07f9e1-4cde-46f0-a4ea-32cdd11fcd02/jobs/75597

Does anyone know what went wrong?

@amakhrov
Copy link
Contributor

amakhrov commented Aug 21, 2023

@wing328 it's a different flavor of the same root cause: in the typescript-axios generator, the supportsES6 flag drove both the language compatibility (TS target) and the module type. Previously, the CI never tested the ESM-based version - now, since it's unconditional, it reveals the issue in the setup.

I guess it can be fixed by setting "moduleResolution": "node", compiler option here:

"compilerOptions": {
"module": "esnext",
"outDir": "dist/esm"
}

I'm not sure if there is going to be more issues related to ES modules down the line

@wing328
Copy link
Member Author

wing328 commented Aug 24, 2023

@amakhrov thanks for the explanation. I'll put this PR on hold for the time being as there's not much benefit taking the risk to remove this option.

@wing328 wing328 modified the milestones: 7.0.0, 7.0.1 Aug 25, 2023
@wing328 wing328 modified the milestones: 7.0.1, 7.1.0 Sep 19, 2023
@wing328 wing328 modified the milestones: 7.1.0, 7.2.0 Nov 13, 2023
@wing328 wing328 modified the milestones: 7.2.0, 7.3.0 Dec 22, 2023
@wing328 wing328 removed this from the 7.3.0 milestone Feb 8, 2024
@wing328 wing328 added this to the 7.4.0 milestone Feb 8, 2024
@wing328 wing328 modified the milestones: 7.4.0, 7.5.0 Mar 11, 2024
@wing328 wing328 modified the milestones: 7.5.0, 7.6.0 Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants