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

Possibly wrong command line split behavior on Windows #28

Closed
akaihola opened this issue Mar 17, 2024 · 1 comment · Fixed by #37
Closed

Possibly wrong command line split behavior on Windows #28

akaihola opened this issue Mar 17, 2024 · 1 comment · Fixed by #37
Labels
bug Something isn't working help wanted Extra attention is needed question Further information is requested

Comments

@akaihola
Copy link
Owner

akaihola commented Mar 17, 2024

(This issue was moved from akaihola/darker#456 together with the linting support implementation.)


A full linter command line can be provided to Darker, e.g.

darker --lint="mypy --strict" .

Splitting that command line into parts is done using shlex.split(), and on Windows the posix=False argument is added so backslashes as directory separators work correctly (todo: check this, not 100% sure).

This works otherwise as expected, but if there's a quoted parameter, the quotes don't get stripped properly:

>>> import shlex
>>> shlex.split('pylint --ignore "setup.py"')
['pylint', '--ignore', '"setup.py"']  # on Windows

which is different from how it behaves on Unix.

A test xfail on Windows is added to test_check_linter_output() to mark this as a known issue but have the test suite still pass.

I'm not sure if the quotes are still removed correctly on Windows.

@akaihola akaihola added bug Something isn't working help wanted Extra attention is needed question Further information is requested labels Mar 17, 2024
@akaihola akaihola added this to the Graylint 1.1.1 milestone Apr 3, 2024
@akaihola
Copy link
Owner Author

akaihola commented Apr 3, 2024

GitHub Copilot suggests this:

You can use the subprocess module's list2cmdline function to split the command line string in a way that's compatible with Windows command line parsing rules. Here's an example:

import subprocess
import shlex

def split_command(command):
   if subprocess.mswindows:
       return shlex.split(subprocess.list2cmdline(shlex.split(command, posix=False)))
   else:
       return shlex.split(command)

print(split_command('a "b"'))  # Outputs: ['a', 'b']

In this example, split_command function first checks if the current system is Windows. If it is, it uses subprocess.list2cmdline to convert the command line string into a format that's compatible with Windows command line parsing rules, and then splits it using shlex.split with posix=False. If the system is not Windows, it simply splits the command line string using shlex.split.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed question Further information is requested
Projects
Development

Successfully merging a pull request may close this issue.

1 participant