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

Darker running with --diff and --quiet on VSCode #147

Closed
hlrossato opened this issue Jun 11, 2021 · 13 comments · Fixed by #163
Closed

Darker running with --diff and --quiet on VSCode #147

hlrossato opened this issue Jun 11, 2021 · 13 comments · Fixed by #163

Comments

@hlrossato
Copy link

I've set up darker on VSCode the way it says on the tutorial but even if I don't pass --diff the command stills run with --diff and --quiet. I've seen there's a PR open for that but I also would like to know if it's possible to run the command without --diff and --quiet explicitly. Thanks

@hlrossato hlrossato changed the title Darker running with --diff and --quiet Darker running with --diff and --quiet on VSCode Jun 11, 2021
@akaihola
Copy link
Owner

I haven't found a way to prevent VSCode from adding the --diff and --quiet options on the Black (or Darker) command line.

Why do you want to run Darker without --diff and --quiet?

@hlrossato
Copy link
Author

@akaihola I want to run without those 2 args because with them VSCode doesn't format the file for me and it also don't display what's wrong. So, it runs behind the scenes because I confirm that with the console opened but doesn't do anything else. If I close the console, I don't know what happened.

@akaihola
Copy link
Owner

So it's for debugging purposes to find out why Darker won't format the file?

The --diff option is essential, because AFAIU VSCode expects to receive the formatted file as standard output from Black or Darker instead of having those tools modify the file in-place.

Below are some suggestions to help you debug the problem. Please try them out and see which ones work and which fail. If these don't help you solve the problem, please describe in detail

  • how would you like Darker to behave when using it with VSCode,
  • how does Black behave in the same situation, and
  • why shouldn't the issue be solved in VSCode's Python extension instead.

Thanks!

1. Try formatting a simple file which is valid Python

Does VSCode format a file which you know to be valid? E.g.

import  os
print ( os.listdir() )

If you reformat that in VSCode using Darker, does it fix the formatting? If yes, then your problem is probably that the file you're trying to format has a syntax error.

2. Try formatting your problematic file in VSCode using Black

i.e. enter the path to black instead of darker in the Python > Formatting: Black Path setting.

3. Try formatting the file on the command line with Darker

darker --diff my/file.py

If that doesn't work, try with more verbosity by adding -v or -vv.

4. Try formatting the file on the command line with Black

black --diff my/file.py

@hlrossato
Copy link
Author

@akaihola Thanks for the explanation and support. So, I've tried the ways you mentioned and below you can see my findings. The idea behind this is so I can have the same behaviour on my editor that I'd get on the terminal. I'm not sure if it's really for debugging but more to speed up my productivity.

1. Try formatting a simple file which is valid Python
VSCode doesn't do anything and when I open the output tab on VSCode this is what is the command it runs:

~/.virtualenvs/myenv/bin/darker --isort "--lint pylint" --line-length=100 --diff --quiet ~/path/to/my/file.py

2. Try formatting your problematic file in VSCode using Black
Same thing as the above but now with black

3. Try formatting the file on the command line with Darker
When I ran the command for darker manually on the terminal nothing happen. It runs but it doesn't display anything. So the command is this: darker --diff path/to/my/file.py and it doesn't display anything. If I run with -vv then I get the following

# Effective configuration:

[tool.darker]
src = [
    "path/to/my/file.py",
]
revision = "HEAD"
diff = true
check = false
isort = false
lint = [
]
log_level = "DEBUG"


# Configuration options which differ from defaults:

[tool.darker]
src = [
    "path/to/my/file.py",
]
diff = true
log_level = "DEBUG"

4. Try formatting the file on the command line with Black
Running black --diff file.py works. It shows me what should be changed in case I remove --diff

@hlrossato
Copy link
Author

Also, one thing I realised is that --diff runs by default so I guess there's no need to add that into the blackArgs

@hlrossato
Copy link
Author

@akaihola It seems to be working now. I found that my configuration on the blackArgs was wrong. I've changed from --lint pylint to --lint=pylint and now it's formatting. I think we can close this issue. Thanks a lot for the help and support.

@akaihola
Copy link
Owner

Yes, the "--lint pylint" part of what you saw in the output tab caught my eye as well.

I guess what VSCode does is it quotes each "Black Args" item which contains spaces and treats it as one command line argument. So this won't work:

    "python.formatting.blackArgs": [
        "--lint pylint"
    ],

but this will:

    "python.formatting.blackArgs": [
        "--lint",
        "pylint"
    ],

as well as this:

    "python.formatting.blackArgs": [
        "--lint=pylint"
    ],

I would assume Python's argparse understands ["--lint", "pylint"] and ["--lint=pylint"] but not ["--lint pylint"] in sys.argv.

The surprising thing is, for me "--lint pylint" suprisingly does work and outputs the reformatted lines (but does no linting):

darker --diff "--lint pylint" my/file.py

Could you try that out? I'm curious why Darker might ignore the unknown argument on some platforms/versions but not on others.

@hlrossato
Copy link
Author

@akaihola I've tried darker --diff "--lint pylint" my_file.py and it actually doesn't work on the terminal as well, so totally my bad :)

@akaihola
Copy link
Owner

akaihola commented Jul 6, 2021

So what happens with darker --diff "--lint pylint" my_file.py on your system? On mine, there's no error but Pylint isn't run either.

@hlrossato
Copy link
Author

@akaihola For me is the same. Nothing happen actually. No errors but no linting as well.

@akaihola
Copy link
Owner

Ah, what actually happens is that Darker interprets "--lint pylint" as a file path.

If you run darker "--lint pylint" --diff my_file.py, I'd expect you to actually get an error since the two non-option arguments are no longer next to each other.

If anything, we could throw an error from Darker for any file names beginning with a dash. Is it a safe assumption that no Python source file or directory name could ever start with -?

@hlrossato
Copy link
Author

@akaihola I think it's a safe assumption. Throwing an error would be a good idea I think

akaihola added a commit that referenced this issue Jul 14, 2021
Disallow paths starting with a hyphen - those are probably due to
quoting multiple arguments like "--lint pylint". Fixes #147.
@akaihola
Copy link
Owner

@hlrossato I'm working on this in #163. Would you like to be invited as a collaborator on this repository and review the pull request?

akaihola added a commit that referenced this issue Jul 22, 2021
Disallow paths starting with a hyphen - those are probably due to
quoting multiple arguments like "--lint pylint". Fixes #147.
akaihola added a commit that referenced this issue Jul 30, 2021
Disallow paths starting with a hyphen - those are probably due to
quoting multiple arguments like "--lint pylint". Fixes #147.
akaihola added a commit that referenced this issue Aug 1, 2021
Disallow paths starting with a hyphen - those are probably due to
quoting multiple arguments like "--lint pylint". Fixes #147.
akaihola added a commit that referenced this issue Aug 1, 2021
Disallow paths starting with a hyphen - those are probably due to
quoting multiple arguments like "--lint pylint". Fixes #147.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants