Skip to content

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

Merged
charliermarsh merged 1 commit into
mainfrom
charlie/spawn
Jul 18, 2024
Merged

Use spawn rather than exec to support paths with spaces#539
charliermarsh merged 1 commit into
mainfrom
charlie/spawn

Conversation

@charliermarsh

Copy link
Copy Markdown
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.

Comment thread src/common/server.ts
exec(command, (error, stdout, _) => {
if (error) {
reject(error);
const child = spawn(cmd, args);

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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
Copy Markdown

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