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

Use spawn rather than exec to support paths with spaces #539

Merged
merged 1 commit into from
Jul 18, 2024

Conversation

charliermarsh
Copy link
Member

Summary

If the interpreter path contains a space, then the find script fails. We need to escape the arguments that we pass to exec. But, it seems easier to just use spawn, which doesn't require escaping.

Closes astral-sh/ruff#12394.

Test Plan

Created a virtual environment foo bar in a repo; verified that the script failed before but succeeds after this change.

@charliermarsh charliermarsh added the bug Something isn't working label Jul 18, 2024
@charliermarsh charliermarsh merged commit e1777e1 into main Jul 18, 2024
6 checks passed
@charliermarsh charliermarsh deleted the charlie/spawn branch July 18, 2024 23:26
exec(command, (error, stdout, _) => {
if (error) {
reject(error);
const child = spawn(cmd, args);
Copy link
Member

Choose a reason for hiding this comment

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

I think execFile is what we want here https://nodejs.org/api/child_process.html

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh yeah, that looks better.

Copy link
Member Author

Choose a reason for hiding this comment

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

I can change.

charliermarsh added a commit that referenced this pull request Jul 19, 2024
## Summary

Recommended in #539.
@flying-sheep
Copy link

I was about to file an issue for that, thanks!

Although I’m mystified why “slap a string together and execute it as a shell script” is still a thing people occasionally do. Sorry for the harshness in the preceding sentence, I appreciate all the hard work!

@dhruvmanila
Copy link
Member

Although I’m mystified why “slap a string together and execute it as a shell script” is still a thing people occasionally do.

Our use-case is to find out the ruff binary which might be installed in the current virtual environment. We do this by asking the Python interpreter which requires this script. Another use-case is to find out the ruff version which is required for certain functionality like automatically switching between ruff server and ruff-lsp.

@flying-sheep
Copy link

What I’m saying is that there’s a reasonable API to call a binary with optional arguments (prog: string|path, args: (string|path)[]) and there is an error-prone way that often leads to bugs and security holes (commandline: string)

This PR switched from the latter to the former, for which I’m grateful, but I’m puzzled why anyone would ever default to the latter way.

dhruvmanila added a commit that referenced this pull request Aug 8, 2024
## Summary

I use ruff for our embedded python interpreter. This interpreter needs
to be bootstrapped (env-vars) before be able to get called (technical
limit of the environment itself).
This is usually done with `.cmd` file (the modern version of `.bat`) on
windows which is kind of similar to `.sh` on linux.
`.cmd` files are often use on Windows, even vscode itself use it to
start vscode on windows.

Today I noticed that I get a crash when using the `.cmd` interpreter
path.

It seems that node require shell mode (in windows calling `cmd.exe` as
the shell) to be able to call `.cmd` files correctly.
I needed to quote the input filename as well to avoid whitespace issues
which looks like a bug in node itself.

With that PR I got no crashes anymore when the extension try to run ruff
😄 .

Note: The shell mode only get activated when the platform is windows and
the file extension is `.cmd`, so users with regular executables should
not be affected at all.

## Test Plan

Manual testing it locally works great and as expected.

I added a utils-test to check the require shell mode flag.

The PR is related to the changes of #539

---------

Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error while trying to find the Ruff binary - VS Code Ruff Extension
4 participants