15

Although I seem to have configured both linitng and formatting properly, the linting runs but the black formatting does not.

    "python.linting.flake8Enabled": true,
    "python.linting.flake8Args": [
        "--max-line-length=110"
    ],
    "python.linting.lintOnSave": true,
    "python.formatting.provider": "black",
    "python.formatting.blackPath": "/usr/local/bin/black",
    "python.formatting.blackArgs": [
        "--line-length",
        "110"
    ],
    "editor.formatOnSave": true,

I have checked, that black is properly configured and I can run the formatting from the command-line without issues.

But when saved in the IDE, black does not format the file. It also does not complain, there are no error-messages that pop up and nothing showing up in the logs. It just does not run the formatting at all.

I am running this insisde a docker-container using remote-editing: not sure, if this makes a difference.

The black-version is:

black>=19.3b0

and the vscode-version is 1.36.1.

Thx for any help, as right now I am pretty clueless, why the black-formatting does not run...

mugwump
  • 161
  • 1
  • 6
Stefan Frank
  • 181
  • 1
  • 1
  • 8
  • What is black8? The Python supports [black](https://pypi.org/project/black/) but not any tool named black8. And did you check the Output panel to see if there were any errors logged there? – Brett Cannon Jul 16 '19 at 00:35
  • black8 was just a typo, the configuration properly references black. And no, there is nothing showing in any of the output panels. – mugwump Jul 16 '19 at 09:02
  • @mugwump what do you mean by "remote-editing"? Are you using the Remote-Container extension from VS Code? – Brett Cannon Jul 16 '19 at 18:39
  • yes, I'm running a django-app inside a container and use the remote-editing from vscode to run the editing inside this container. – mugwump Jul 23 '19 at 09:36

4 Answers4

7

The line argument for black is wrong, it should be

--line-length=120

according to https://stackoverflow.com/a/58048911/4435175.

Vega
  • 2,661
  • 5
  • 24
  • 49
7

Late answer, in case it helps anybody. I had the same problem and resolved it as below

I needed to have this line in user settings.json

"editor.defaultFormatter": "ms-python.python"

Otherwise, I guess, the formatting was overwritten by my default formatting option which was

"editor.defaultFormatter": "esbenp.prettier-vscode",

So my overall settings for formatting related stuff were as below

"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
  "[python]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "ms-python.python"
  },
  "[javascript]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },

  "python.formatting.provider": "black",
  "python.formatting.blackPath": "/home/<user>/anaconda3/bin/black",

This line "editor.defaultFormatter": "ms-python.python" could also have been activated as below from VS-Code command-pallette (Ctrl + shift + P).

Format Document with > Configure Default formatter > Select Python

Rohan_Paul
  • 1,504
  • 3
  • 29
  • 36
0

But here is a wild guess: I started the dockerd manually from the command-line using sudo: This gave me all kind of weird issues (files created from inside the container e.g. migrations or cache-files ended up being owned by root. I recently moved the dockerd into a service, now suddenly black is running on save. The root of all evil could have been rights-issues, that come from the fact that the dockerd was run as sudo and not as a service.

mugwump
  • 161
  • 1
  • 6
0

I add the blackPath: "python.formatting.provider": "black", "python.formatting.blackPath": "C:/conda/Scripts/black.exe", and solve this issue.

I use miniconda.