ts-spec / tspec

Type-driven API Documentation library. Auto-generating REST API document based on TypeScript types.
https://ts-spec.github.io/tspec/
MIT License
108 stars 5 forks source link

Cannot import Tspec when using `"module": "NodeNext"` in tsconfig #57

Closed aayush123 closed 3 weeks ago

aayush123 commented 1 month ago

Describe the bug Our application uses "module": "NodeNext" in the tsconfig file, however, when importing Tspec as mentioned in the docs, it gives the following error: Module tspec has not exported member Tspec: image

It seems that the imports show up when using "module": "commonjs" (didn't try other options) Unfortunately, we are not able to switch to commonjs and must continue using NodeNext

To Reproduce

  1. Clone the minimal reproduction repo: https://github.com/aayush123/tspec-test
  2. Ensure node version in 20.x
  3. Run npm i
  4. Run npm run build (This runs npx tspec generate --outputPath openapi.json)
  5. See that the openapi.json contains an empty schema. Also notice the import error in index.ts (in VSCode)
  6. Change "module": "NodeNext" to "module": "CommonJS" in tsconfig.json
  7. Run npm run build again.
  8. See that openapi.json now contains expected schema. Also notice that import error is now gone in index.ts

Expected behavior We should be able to import Tspec even when module in tsconfig is set to NodeNext

Additional context I'd be happy to create a PR to fix this with minimal guidance.

aayush123 commented 3 weeks ago

@hyeonss0417 What do you think would be the appropriate fix for this? I'd be happy to submit a PR, just need a small nudge

hyeonss0417 commented 3 weeks ago

Hello, @aayush123

Thank you very much for taking the time to report the bug and for your interest in our project.

I tested it on the test repo you reported, and when I modified the dist/index.d.ts file like below, the openapi.json file seems to be generated as expected when I build it.

export * from './generator';
export * from './server';
export * from './types/tspec.d.ts';

The cause of the problem is that in tsconfig, "module": "NodeNext" in tsconfig, I think it's caused by Directory modules (index file resolution) not working properly.

I'm trying to find a solution to the problem, but I don't know the exact solution yet🤔.

hyeonss0417 commented 3 weeks ago

Hi @aayush123

Thank you so much for your interest in contributing to the project and for reporting the issue. I appreciate your willingness to work on a fix.

Upon further investigation, I realized that the bug was more complex than it initially appeared. To ensure it was resolved correctly and promptly, I went ahead and made the necessary changes myself. I’ve just submitted a PR with the fix.

However, I encourage you to take a look at the PR (#61) to see how the issue was resolved. Your feedback and any suggestions for improvements are always welcome. Additionally, there are many other areas in the project where your contributions would be invaluable, and I’d be happy to guide you through any of them.

Thank you again for your support and understanding.

Best regards

hyeonss0417 commented 3 weeks ago

I just have merged the PR (#61), and the issue is fixed!

To resolve this issue on your end, please update tspec version to latest(=v0.1.115).

Once again, thank you for your valuable feedback. Your contributions help us improve the project significantly.