-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[WIP] Fix bug where running script without extension removes existing .mjs
files
#276
Conversation
I just filled out the CLA at https://cla.developers.google.com/. If a maintainer could re-run the check, that would be much appreciated. Thanks in advance! |
zx.mjs
Outdated
@@ -101,10 +101,12 @@ async function writeAndImport(script, filepath, origin = filepath) { | |||
|
|||
async function importPath(filepath, origin = filepath) { | |||
let ext = extname(filepath) | |||
let tmpFilename= Math.random().toString(36).substr(2) + '.mjs' |
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.
One thing to consider: error messages.
User will get errors with file name is random which can lead to bad DX.
What if filename + '.mjs'
is not exists? We can use it (same as old behavior).
In case there is a collision use: filename + random + '.mjs'
.
WDYF?
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.
Oh, good call. Yeah, that makes sense to me. Will push that change later today!
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.
Hi @antonmedv, sorry for the delay.
Just pushed the requested fix. If you feel like it's good to go, I'll let you hit resolve conversation. If you'd like me to make any additional fixes, though, just let me know. Thanks!
I'd suggestion solution like |
This PR fixes #208.
Description
Node cannot run scripts without an extension. To account for this, whenever a user would try to call
zx
on a script without an extension,zx
would try to write the script to an.mjs
file before importing it. However, this caused a bug where if there was an existing.mjs
file with a similar name (e.g. user runs./hello
, but there already exists a./hello.mjs
in the same dir), the existing file would get overwritten and then removed.Changes
This PR fixes the issue by adjusting
importPath
to use a uniquely generatedtmpFilename
. This will ensure that if a user callszx
using a script without an extension, it will not inadvertently overwrite/remove a similarly named existing.mjs
file.Tests
This PR includes an update to the no extension test case (
test.mjs:83
) to verify the fix worked as intended.Checklist