-
Notifications
You must be signed in to change notification settings - Fork 623
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(path): make isWindows
check compatible with Node and Bun
#4961
refactor(path): make isWindows
check compatible with Node and Bun
#4961
Conversation
All that needs to be done here is to check if the version is windows. Handling each combination of runtime and operating system will make the existing osType difficult to test and maintain.
It was using a deprecated web API.
path/_os.ts
Outdated
})(); | ||
function getNodeOsModule(): OsModule | undefined { | ||
if (require !== undefined) { | ||
return require("os"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If node.js is in ESM mode (.mjs or type: module
in package.json), then it doesn't have require
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I tested by manually converting to js and then running with node os.js
, explains why this worked for me then.
I'll mark this PR as Draft again until I can come up with a better solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
@std
is published in JSR, and it's distributed as ESM in Node.js. So I think it's enough to only support ESM context.
This change can be far more easily made using |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4961 +/- ##
=======================================
Coverage 96.26% 96.26%
=======================================
Files 479 479
Lines 38705 38700 -5
Branches 5617 5619 +2
=======================================
- Hits 37259 37256 -3
+ Misses 1403 1401 -2
Partials 43 43 ☔ View full report in Codecov by Sentry. |
I noticed that while I was testing. But that doesn't allow us to check if the underlying os is Windows (which is the only thing this module does that's public facing). |
On second thought, we could do the following:
WDYT? |
I'm not in favor of this as this introduces Top-level await and makes the module async. We saw many issues about async modules in the past.. Node.js (>=21) and Bun seem exposing |
I also considered this and I don't like the idea of using async/await in any way. I'm going to look and see how the node os module works under the hood and see if maybe we can't just do it the same way they do. |
This is how NodeJS checks to see if the underlying OS is windows.
…an981/deno_std into is-windows-cross-runtime
I poked around in node's os module and saw how they check to see if node is running on windows. They just use process.platform, which we of course can do here as well. I've tested it and it seems to work. |
Ah, yes, I forgot about top-level |
@iuioiua for this PR that isn't necessary. I was able to do it with |
@std/path
compatible with Node and Bun
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delayed review, @BenMcLean981. I've simplified your logic to just use Deno.build.os
or navigator.platform
, depending on the runtime. This should all be compatible with Deno, Bun, Node and the browser.
Thank you very much for your contribution! @kt3k, can you PTAL and be sure to update the compatibility of @std/path
on JSR once the next release is cut?
@std/path
compatible with Node and Bun@std/path
compatible with Node and Bun
I changed the commit type from |
I don't see how this PR, in it's current state, not yet being tested on Node or Bun makes this a refactor PR. A package being made available in two new runtimes is a feature. @BenMcLean981, is there any chance you'd be able to test this PR, in its current state, on the various platforms/runtimes once more? |
We can't ask somebody to test on every change. We need to set up CI to ensure it's working on specific platform. |
I can confirm this works on macOS (POSIX) for Deno, Bun, Windows and Chrome. |
I'd suggest we would make |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
While I disagree with this being a |
@std/path
compatible with Node and BunisWindows
check compatible with Node and Bun
I changed the title a bit as there seem somethings remaining (like the usage of |
@iuioiua I ran my little set of manual tests again on my Windows machine. It doesn't work on Node. An error gets thrown that navigator is undefined, if I use a nullish check on it then it incorrectly returns false. I added in a branch to the conditional check to use I agree with the discussion around cross-platform and cross-runtime automated testing. I was thinking about setting that up for this repo, but I think that needs to be part of a larger discussion. I'm also not sure what tooling even exists to make that happen. |
Ah ok. It would be better to support lower versions like 18.x, 20.x, as they are still officially supported versions. |
I think we should support from the LTS version (now v20.17.0), upwards. So the recent change makes sense. Thanks, @BenMcLean981 for confirming and checking. |
No problem! Happy I could contribute. |
Description
As described by the linked issue the
isWindows
function@std/path
is not cross runtime. In it's existing state it should work on Deno and the browser (though there are issues there) but not bun or node. In this PR I believe I have resolved those issues and added support for bun or node.Tested Runtimes
I manually tested this solution in the following runtimes:
I couldn't think of an easy way to test this automatically. I could probably write a bash script to do it so that it can be added to the pipeline if necessary.
Potential Issues with Existing Browser Support
As mentioned in the discussion there may have been issues with the existing implementation for browsers. The
navigator.appVersion
property is deprecated, so I moved away from that and started usingnavigator.userAgent
.Bun and Node Support
I got Bun and Node working using Node's os module which also has an implementation in bun. The code for this is pretty ugly, as
require
does not exist in Deno or the browser. I could not think of a better way to do this.Resolves #4914