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

[WIP] Fix bug where running script without extension removes existing .mjs files #276

Merged
merged 5 commits into from
Feb 16, 2022

Conversation

Ivraj
Copy link
Contributor

@Ivraj Ivraj commented Jan 18, 2022

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 generated tmpFilename. This will ensure that if a user calls zx 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

  • Tests pass
  • Appropriate changes to README are included in PR (N/A)

@Ivraj
Copy link
Contributor Author

Ivraj commented Jan 18, 2022

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'
Copy link
Collaborator

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?

Copy link
Contributor Author

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!

Copy link
Contributor Author

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!

@Ivraj Ivraj requested a review from antonmedv January 27, 2022 17:47
@fisker
Copy link

fisker commented Feb 11, 2022

I'd suggestion solution like unused-filename

@antonmedv antonmedv merged commit f8bb1c7 into google:main Feb 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weird bug, symbol link of mjs file will remove linked dest
3 participants