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

--stdout option throws error if dash is given indicating stdin input – breaks VSCode compatibility #523

Closed
Tracked by #527
seweissman opened this issue Nov 6, 2023 · 6 comments · Fixed by #574
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@seweissman
Copy link

Describe the bug
-d/--stdout option should allow "-" to indicate stdin

To Reproduce
Steps to reproduce the behavior:

  1. Run darker -d --stdin-filename=somefile -
  2. Darker exits with error "darker.config.ConfigurationError: Either --stdin-filename= or exactly one Python source file which exists on disk must be provided when using the stdout option"

Expected behavior
Darker should run and accept stdin input in the same way that it does without the trailing dash.

Environment (please complete the following information):

  • OS macos
  • Python version 3.9.7
  • Darker version 1.7.2
  • Black version 22.6.0

Additional context
I am trying to use darker with the new VSCode black-formatter extension. The extension sends stdin input and seems to expect stdout, which can be accomplished by configuring it to run with -d. The main piece keeping it from working is that darker doesn't like the dash indicating stdin, which is added by VSCode.

I belive that validate_stdout_src is likely the issue. The validate_stdin_src function already supports the dash.

@seweissman
Copy link
Author

I'm happy to attempt to open a pull request for a fix f there is agreement that this is a bug.

@dkeraudren
Copy link
Collaborator

On my side, darker is completely broken in vscode since the python extension removed all formating options.
Since I switched to ms-python.black-formatter I'm unable to use darker.
I use format on save but anytime I save I got a failed to save because of conflicts, no issue when I use plain black

@seweissman
Copy link
Author

seweissman commented Nov 8, 2023

@dkeraudren I had the same issue and I believe the reason is the the new black extension is expecting the output on stdout, which darker doesn't do by default. I was able to get it to work by setting the formatter path to darker and by setting darker to output to stdout with -d, but I ran into the bug described above. I was able to patch the darker code locally to fix the bug and then it worked.

  "black-formatter.path": [
    "/path/to/bin/darker"
  ],
  "black-formatter.args": [
    "-d",
  ],

@akaihola akaihola self-assigned this Feb 23, 2024
@akaihola akaihola added the enhancement New feature or request label Feb 23, 2024
@akaihola akaihola added this to the Darker 1.9.0 - Graylint milestone Feb 23, 2024
@akaihola
Copy link
Owner

Hi @seweissman and @dkeraudren, thanks for the report and analysis! I'd love to make Darker work perfectly together with the VSCode black-formatter extension as well as other IDEs and tools.

@seweissman could you share your local patch to Darker?

@akaihola akaihola changed the title stdout option throws error if dash is given indicating stdin input --stdout option throws error if dash is given indicating stdin input – breaks VSCode compatibility Feb 23, 2024
@Garfounkel
Copy link
Collaborator

@dkeraudren I had the same issue and I believe the reason is the the new black extension is expecting the output on stdout, which darker doesn't do by default. I was able to get it to work by setting the formatter path to darker and by setting darker to output to stdout with -d, but I ran into the bug described above. I was able to patch the darker code locally to fix the bug and then it worked.

  "black-formatter.path": [
    "/path/to/bin/darker"
  ],
  "black-formatter.args": [
    "-d",
  ],

Can confirm this works, however it was a bit confusing, so here's a step-by-step for those who want to have this working before a proper PR is created:

  1. clone darker locally
  2. cd darker
  3. pip install -e . -e installs in editable mode so that you can modify the fork without re-installing every time
  4. which darker save the path
  5. Modify darker/src/darker/config.py:
diff --git a/src/darker/config.py b/src/darker/config.py
index 7beb307..6db29da 100644
--- a/src/darker/config.py
+++ b/src/darker/config.py
@@ -57,7 +57,7 @@ class OutputMode:
             return
         if stdin_filename is None and len(src) == 1 and Path(src[0]).is_file():
             return
-        if stdin_filename is not None and len(src) == 0:
+        if stdin_filename is not None and len(src) == 0 or src == ["-"]:
             return
         raise ConfigurationError(
             "Either --stdin-filename=<path> or exactly one Python source file which"
  1. Then in your vscode settings.json:
"python.editor.defaultFormatter": "ms-python.black-formatter",

"black-formatter.path": ["<path_from_4.>"],
"black-formatter.args": ["-d"],

@akaihola
Copy link
Owner

@Garfounkel's fix is now in PR #574. I also invited you @Garfounkel, @seweissman and @dkeraudren as collaborators to this repository. If you accept the invitation, I can assign you as reviewers for #574. I'd appreciate especially a second look at the changes I made to README.rst. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging a pull request may close this issue.

4 participants