-
Notifications
You must be signed in to change notification settings - Fork 56
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
Visual Studio Code format on save doesn't work with --diff #104
Comments
Thanks @Krischtopp for your bug report! Do you want Darker to modify the files on the disk? If that's the case, you can't use the
Or is Visual Studio Code supposed to read the diff from the output of Darker (or Black) and apply the changes itself? In that case the reason may be the slightly different output format between Darker and Black – and for that there's already pull request #84 which I'm waiting for someone to test and review. If you're willing to help by testing and reviewing PRs, please accept my invitation to be a collaborator on this project, and I'll add you as a reviewer for #84. |
@Carreau do you have an insight about this issue? |
No I'm not sure I went back to vim :-) if I have time I'll look into it. One of the difficulty is darker need access to the repo to get the changes, and not all vs code hooks allow that maybe ? |
What about @virtuald, IIRC you were trying out Darker with Visual Studio Code as well? Any insight on this issue? |
Oh, and @DylanYoung tried to get Darker working with VSCode as well. Does the problem @Krischtopp describes sound familiar to you? |
@akaihola Thank you very much for your invite. I tried some more things. Beforehand I emptied
|
Thanks @Krischtopp for your investigation! If I understand correctly, your conclusion is that Visual Studio Code somehow expects different behavior from the external formatter (in this case Darker) depending on whether formatting happens as the result of the explicit Have you verified that VS Code actually calls Darker on save? Or could it be that it uses some kind of an internal formatter instead? One thing to note is that currently Darker always expects to read the source code to be reformatted from the disk. It can't e.g. accept Python code on the standard input and provide a reformatted version on its standard output. Could it be that VS Code calls Black in that mode? Try e.g. this to see how the stdin/out mode of Black works: echo "print( 1 )" | black - |
I had a look at the Visual Studio Code Python extension repository and how they call the black formatter: https://github.com/microsoft/vscode-python/blob/main/src/client/formatters/blackFormatter.ts#L43 And I found something that looks like what happens on |
Thanks @Krischtopp for your continued investigation! As I'm not currenly a VS Code user, I'd like to leave figuring this out to you and other VS Code users (I mentioned some in previous comments). Just an idea: maybe a good way to debug this is to modify Darker to dump its command line (or the parsed options in Also, keep in mind that Darker looks for defaults for its configuration options in |
For Darker 1.3.0, you can apply this patch: diff --git a/src/darker/__main__.py b/src/darker/__main__.py
index bc497a3..8ab0a2b 100644
--- a/src/darker/__main__.py
+++ b/src/darker/__main__.py
@@ -279,6 +279,7 @@ def main(argv: List[str] = None) -> int:
"""
if argv is None:
argv = sys.argv[1:]
+ print(Path.cwd(), "$", " ".join(sys.argv), file=open("/tmp/darker.log", "a"))
args, config, config_nondefault = parse_command_line(argv)
logging.basicConfig(level=args.log_level)
if args.log_level == logging.INFO: You can then |
I have this in VSCode settings: "python.formatting.blackPath": "/home/kaiant/.local/bin/darker",
"python.formatting.provider": "black",
"python.formatting.blackArgs": [
"--revision=master...",
"--isort"
], and when I save a file, I see this in /home/akaihola/darker $ /home/akaihola/.local/bin/darker --revision=master... --isort --diff --quiet /home/akaihola/darker/src/darker/__main__.py.7b5da28ec78040aee3d8d0048e94a4a6.tmp and modified lines do get reformatted in VSCode. |
Thank you for looking into this again! I updated darker to 1.3.0 and applied your patch.
When I save an edited file, nothing gets reformatted, and I see this in the logs (edit: wrapped lines for clarity by @akaihola):
When I save an unedited file, reformatting works as expected, and I see this in the logs:
As a quick sanity check, I saved an edited JSON file and reformatting worked as expected. |
@Krischtopp, I believe this revealed the issue. I took the liberty of wrapping lines in your log entries above, and – d'uh, of course! – Darker refuses to process Two possible way to fix this in Darker:
|
I did some debugging and had a look at the code of Darker and Black. Lines 142 to 143 in d269159
*.py files. I'm not sure whether this is necessary at all. gen_python_files already filters for filename extensions.
darker/src/darker/black_diff.py Lines 138 to 148 in d269159
gen_python_files to all files and therefore also filters *.tmp files.
https://github.com/psf/black/blob/7b153936587e17b9db992a2d8c8b6cfba3ef7209/src/black/__init__.py#L537-L551 Darker doesn't behave like Black in this case, and only reformats the file if it has a matching filename extension. I don't know if there's a reason for that, but to me, it seems like Black's behavior is preferable. One could probably add There should be one more issue here. The |
@Krischtopp, FYI, I've started to work on this issue. Thanks for your additional notes as well – especially diffing the |
I would like Visual Studio Code to format the file with Darker on save, but it does nothing with Darker when I'm using the
--diff
argument. I'm using"editor.formatOnSave": true
insettings.json
and it works fine with Black.If you choose to make this work, there is also the
"editor.formatOnSaveMode": "modifications"
setting. I would find it quite nice if it was possible to use this in Visual Studio Code instead of the--diff
argument.The text was updated successfully, but these errors were encountered: