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

- as source file not allowed when using the --stdin-filename option (unlike in Black) #492

Closed
my-tien opened this issue May 17, 2023 · 4 comments · Fixed by #494
Closed

Comments

@my-tien
Copy link
Collaborator

my-tien commented May 17, 2023

Describe the bug
I have the Black Formatter extension and configured darker in the black formatter path.
When formatting on save in VSCode, the output log will contain below error.

NOTE: everything works fine when not using the Black Formatter extension, but using the formatter feature of the Python extension (ms-python.python) instead.

  File "C:\Program Files\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Program Files\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\xxx\envs\yyy\Scripts\darker.exe\__main__.py", line 7, in <module>
  File "C:\Users\xxx\Envs\yyy\lib\site-packages\darker\__main__.py", line 631, in main_with_error_handling
    return main()
  File "C:\Users\xxx\Envs\yyy\lib\site-packages\darker\__main__.py", line 484, in main
    args, config, config_nondefault = parse_command_line(argv)
  File "C:\Users\xxx\Envs\yyy\lib\site-packages\darker\command_line.py", line 150, in parse_command_line
    validate_stdin_src(args.stdin_filename, args.src)
  File "C:\Users\xxx\Envs\yyy\lib\site-packages\darker\config.py", line 114, in validate_stdin_src
    raise ConfigurationError(
darker.config.ConfigurationError: No Python source files are allowed when using the `stdin-filename` option

To Reproduce
Steps to reproduce the behavior:

  1. pip install darker
  2. In VSCode install extension Black Formatter (ms-python.black-formatter)
  3. Configure Black as default formatter for python files.
  4. In settings.json:
  "black-formatter.path": [
    "path\\to\\darker.exe"
  ],
  "python.formatting.provider": "black",
  "python.formatting.blackArgs": [
    "--line-length",
    "100"
  ],
  "[python]": {
    "editor.formatOnSave": true
  }
  1. Add whitespace after a line in a python file and save.

Expected behavior
Reformatted file

Environment (please complete the following information):

  • OS: Windows 10
  • Python 3.9.13
  • Git version 2.39.1.windows.1
  • Darker version 1.7.1
  • Black version 22.3.0
@akaihola
Copy link
Owner

Hi @my-tien, thanks for the report!

Can you find the full Darker command line in an output/debug window?

The correct format is

darker --line-length 100 --stdin-filename path/to/file.py

So the path of the file must immediately follow the --stdin-filename option, or alternatively be expressed using an equals sign like --stdin-filename=path/to/file.py.

If you can't find the used command line inside VSCode, you can modify Darker's source code to catch the exception at darker/command_line.py:150, print repr(argv) to stderr and re-raise. You should then be able to see whether the Darker command line used by the Black Formatter extension is correct.

@my-tien
Copy link
Collaborator Author

my-tien commented May 23, 2023

Hi, this is the generated Darker command:

path\to\darker.exe --line-length 100 --stdin-filename path\to\python\file.py -

So maybe the hyphen at the end is the problem?

@akaihola
Copy link
Owner

Yes, probably the hyphen is the problem. So Darker is then not fully compatible as a drop-in replacement for Black. This should certainly be fixed. Could you still verify that Black indeed works with a similar command line:

path\to\black.exe --line-length 100 --stdin-filename path\to\python\file.py -

Thanks! I'll then think about how to best create the fix for the problem.

@my-tien
Copy link
Collaborator Author

my-tien commented May 27, 2023

I did not have time to test this on my initial machine again, but on my Linux machine at home I successfully reproduced the problem on the command line. Since the hyphen at the end expects something on stdin, you need to adapt the command like below:

$ cat test.py | black --line-length 100 --stdin-filename test.py -

print("hello")
reformatted test.py

All done! ✨ 🍰 ✨
1 file reformatted.
$ cat test.py | darker --line-length 100 --stdin-filename test.py -

Traceback (most recent call last):
  File "/home/tieni/.local/bin/darker", line 8, in <module>
    sys.exit(main_with_error_handling())
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tieni/.local/lib/python3.11/site-packages/darker/__main__.py", line 631, in main_with_error_handling
    return main()
           ^^^^^^
  File "/home/tieni/.local/lib/python3.11/site-packages/darker/__main__.py", line 484, in main
    args, config, config_nondefault = parse_command_line(argv)
                                      ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tieni/.local/lib/python3.11/site-packages/darker/command_line.py", line 150, in parse_command_line
    validate_stdin_src(args.stdin_filename, args.src)
  File "/home/tieni/.local/lib/python3.11/site-packages/darker/config.py", line 114, in validate_stdin_src
    raise ConfigurationError(
darker.config.ConfigurationError: No Python source files are allowed when using the `stdin-filename` option

@akaihola akaihola changed the title VSCode: No Python source files are allowed when using the stdin-filename option - as source file not allowed when using the --stdin-filename option (unlike in Black) May 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging a pull request may close this issue.

2 participants