Skip to content
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

refactor: use full relative paths in imports #552

Merged
merged 6 commits into from
Nov 20, 2023
Merged

Conversation

wolfy1339
Copy link
Member

This should help bring ESM compatibility in the future

@wolfy1339 wolfy1339 added the Type: Maintenance Any dependency, housekeeping, and clean up Issue or PR label Apr 26, 2021
@wolfy1339
Copy link
Member Author

This works fine with the TS compiler itself, but not Jest

@wolfy1339 wolfy1339 changed the title refactor: use full relative paths in imports 🚧 refactor: use full relative paths in imports Apr 26, 2021
@gr2m
Copy link
Contributor

gr2m commented Apr 26, 2021

This works fine with the TS compiler itself, but not Jest

If jest is the problem, I'm okay with moving away from it. I heard from others that Jest is a big pain point in the transition to ESM

@wolfy1339
Copy link
Member Author

If jest is the problem, I'm okay with moving away from it. I heard from others that Jest is a big pain point in the transition to ESM

ESM support is not in the current stable release of jest; To get ESM support, you need jest@next and ts-jest@next
The tests work fine without any modifications when run as ESM.


Here's what I have found with some testing:

  • Removing the .js from imports within the tests resolves the files correctly, however the imports in the source code fail to resolve. From what it looks like, jest is trying to resolve these imports when it should be ts-node resolving the imports
  • The "express middleware no mount path no next" test no longer works, because we cannot use require() in ESM which is used to import express without the types, because importing express with types results in the test never finishing (it times out). Commenting out the webhooks node middleware makes it work.

@gr2m
Copy link
Contributor

gr2m commented Apr 26, 2021

importing express with types results in the test never finishing (it times out)

this is the weirdest thing! Is this related to jest or do you see the same when you just import express in an ESM node script?

@G-Rath
Copy link
Member

G-Rath commented Apr 26, 2021

I'd personally prefer to stick with jest - We're planning to ship the new major of jest in a couple of months, so shouldn't be too long a wait; using next in the meantime would help weed out any bugs :)

@gr2m
Copy link
Contributor

gr2m commented Apr 26, 2021

I'd personally prefer to stick with jest - We're planning to ship the new major of jest in a couple of months, so shouldn't be too long a wait; using next in the meantime would help weed out any bugs :)

ok 👍🏼

We're planning to ship the new major of jest

You are working on jest?

@G-Rath
Copy link
Member

G-Rath commented Apr 26, 2021

You are working on jest?

I mainly maintain eslint-plugin-jest, but do contributions to jest itself too; but so I know a few of the folks there (and specifically the guy who handles the releases who also helps maintain the eslint plugin).

@wolfy1339
Copy link
Member Author

wolfy1339 commented Apr 26, 2021

This is the test in question:

https://github.com/octokit/webhooks.js/blob/master/test/integration/node-middleware.test.ts#L360-L390

It only fails in this test, and only when you import with types for express. Wether in ESM or CommonJS
The middleware should've called the next function, since it isn't the path it is using, but it doesn't seem like it did and the middleware just hangs.

EDIT: Only happens in jest@next and ts-jest@next

@wolfy1339
Copy link
Member Author

wolfy1339 commented Apr 26, 2021

The next() function from express is indeed getting called when you give an invalid route.

The test stalls right after we call the next() function.

EDIT: something to do with the versions of jest and ts-jest that is used. The current versions (in master) don't exhibit this behaviour

@wolfy1339
Copy link
Member Author

@G-Rath do you know what could be causing this?
As soon as we use jest@next withts-jest@next and install @types/express the tests stall

@G-Rath
Copy link
Member

G-Rath commented Apr 27, 2021

@wolfy1339 not off the top of my head, but could try looking into it this weekend.

However, the fact that the issue happens when installing @types/express makes me think of an issue caused by the type of Request looks like that code was removed here.

@wolfy1339 wolfy1339 added this to the v10 milestone Apr 27, 2021
@wolfy1339 wolfy1339 force-pushed the use-full-relative-paths branch from a50213f to a2c4203 Compare April 27, 2021 21:59
@wolfy1339 wolfy1339 force-pushed the use-full-relative-paths branch from a2c4203 to 09f89bb Compare May 6, 2021 02:22
@wolfy1339
Copy link
Member Author

I think we are running into this issue: kulshekhar/ts-jest#2010

@wolfy1339
Copy link
Member Author

I have tried forcing ts-jest to output ESM, but that doesn't help either.

@G-Rath Can you help out here to figure out a way to go?

@wolfy1339 wolfy1339 force-pushed the use-full-relative-paths branch from f3e4216 to 2c3cf2f Compare May 9, 2021 21:23
@wolfy1339
Copy link
Member Author

I did some more testing, found a fix.

  • This problem only manifests itself when using TypeScript and you are using the types for express (ie not using require)
  • The problem arises when the middleware encounters a path it doesn't know about
  • Specifying the middleware first before any other fixes this problem

It seems the reverse of #534 but only when paths are unknown to the middleware

@oscard0m
Copy link
Member

oscard0m commented May 12, 2021

I did some more testing, found a fix.

  • This problem only manifests itself when using TypeScript and you are using the types for express (ie not using require)
  • The problem arises when the middleware encounters a path it doesn't know about
  • Specifying the middleware first before any other fixes this problem

It seems the reverse of #534 but only when paths are unknown to the middleware

Just to clarify: Is the problem in our end and how we define types or is something maybe we should report to jest?

@wolfy1339
Copy link
Member Author

wolfy1339 commented May 12, 2021

Just to clarify: Is the problem in our end and how we define types or is something maybe we should report to jest?

I am really not sure what the underlying issue really is.

I only know that the issue doesn't arise if it is the first in the chain of middleware, and only happens when you try to access a path that isn't handled by the Webhooks middleware.

The types don't seem to be anything wrong with them that would conflict like this

@oscard0m
Copy link
Member

Just to clarify: Is the problem in our end and how we define types or is something maybe we should report to jest?

I am really not sure what the underlying issue really is.

I only know that the issue doesn't arise if it is the first in the chain of middleware, and only happens when you try to access a path that isn't handled by the Webhooks middleware.

The types don't seem to be anything wrong with them that would conflict like this

Do you think it would be interesting to try to reproduce it without Octokit (a small codesanbox maybe?)? This way we could know if it's something related to Octokit or to jest under concrete circumstances. Only if it is not a big effort to create this simplified example.

@wolfy1339
Copy link
Member Author

Do you think it would be interesting to try to reproduce it without Octokit (a small codesanbox maybe?)? This way we could know if it's something related to Octokit or to jest under concrete circumstances. Only if it is not a big effort to create this simplified example.

Yes, it would probably be helpful to diagnose the issue to create a simplified example.

We'd just need to extract the middleware logic from this module.

@wolfy1339 wolfy1339 force-pushed the use-full-relative-paths branch 3 times, most recently from 2af81d4 to 216efe4 Compare May 28, 2021 21:36
@gr2m gr2m added the esm-blocked Temporary label to denote esm dependency blocker label Jun 11, 2021
@oscard0m
Copy link
Member

I was discussing with some friends the status of jest regarding ESModules. Do you know what's the status on that? Is this the issue I should subscribe: jestjs/jest#11167?

@wolfy1339
Copy link
Member Author

Jest should work fine with plain JS using ESM.

The only issue we have, is because of TypeScript and the way Jest handles imports (kulshekhar/ts-jest#2010), since the file index.js doesn't exist, jest complains that it cannot find the import. It is valid TypeScript and the compiler deals with the file extensions.

Since we are going to be moving to pure ESM + external TypeScript definition files, most of the changes in this PR are moot.

@ollie-iterators
Copy link

I assume this will get unblocked by #819.

@wolfy1339
Copy link
Member Author

I don't think it will have an effect

@ollie-iterators
Copy link

ollie-iterators commented Mar 16, 2023

I thought that #819 was using "esbuild to transpile the TS source code into an ESM source ...". Does that mean that it should only unblock it once the changes go to the other areas in the repository?

@wolfy1339
Copy link
Member Author

Yes, we do build for ESM. However, those builds are meant for the browser/deno and we are still using CommonJS for NodeJS
Until work on octokit/octokit-next.js is complete we are still using CommonJS

This should help bring ESM compatibility in the future
@wolfy1339 wolfy1339 force-pushed the use-full-relative-paths branch from 30c7bc7 to a6e9186 Compare November 19, 2023 16:36
@wolfy1339 wolfy1339 removed the esm-blocked Temporary label to denote esm dependency blocker label Nov 19, 2023
@wolfy1339 wolfy1339 changed the title 🚧 refactor: use full relative paths in imports refactor: use full relative paths in imports Nov 19, 2023
@wolfy1339
Copy link
Member Author

I came back to this PR, and I have gotten it working

@wolfy1339 wolfy1339 requested review from gr2m, G-Rath and a team November 19, 2023 17:23
G-Rath
G-Rath previously approved these changes Nov 19, 2023
Copy link
Member

@G-Rath G-Rath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Just one question: looks like we're using .ts for type imports and .js for runtime code in the source code but not in tests - I'm figuring that's because the tests go through an extra step somewhere that handles that? but my actual question is are we able to use .js for the type imports too, and if so I think we should do that for consistency

(and sorry for not responding to your last ping on this - I don't recall ever seeing it in my inbox 😬)

@wolfy1339
Copy link
Member Author

Looks good to me! Just one question: looks like we're using .ts for type imports and .js for runtime code in the source code but not in tests - I'm figuring that's because the tests go through an extra step somewhere that handles that?

It's a functionality included in recent versions of TypeScript using the allowImportingTsExtensions option.

but my actual question is are we able to use .js for the type imports too, and if so I think we should do that for consistency

We are absolutely able to use the .js extension

@wolfy1339 wolfy1339 merged commit c5f266f into main Nov 20, 2023
6 checks passed
@wolfy1339 wolfy1339 deleted the use-full-relative-paths branch November 20, 2023 11:10
Copy link
Contributor

🎉 This PR is included in version 12.0.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

lerebear added a commit to lerebear/sizeup-cli that referenced this pull request Dec 17, 2023
octokit/webhooks.js#552 broke `npm publish` in
this repository by introduces `.ts`extensions to some imports (which
caused `npm publish` to fail with "error TS2691: An import path cannot
end with a '.ts' extension"). This fixes that.
lerebear added a commit to lerebear/sizeup-cli that referenced this pull request Dec 17, 2023
octokit/webhooks.js#552 broke `npm publish` in
this repository by introduces `.ts`extensions to some imports (which
caused `npm publish` to fail with "error TS2691: An import path cannot
end with a '.ts' extension"). This fixes that by adding the ts-node.esm
configuration to tsconfig.json (which in turn required several
dependency bumps).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released Type: Maintenance Any dependency, housekeeping, and clean up Issue or PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants