microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
144 stars 34 forks source link

errors parsing nested quotes #513

Open rjwalters opened 2 months ago

rjwalters commented 2 months ago

I ran into the same problem reported in #419 this morning related to parsing nested double quotes in formatted strings like f"{"value"}".

It was especially confusing because I was able to format the file without any issue using commands in the vscode terminal:

% black --version
black, 24.4.2 (compiled: yes)
Python (CPython) 3.12.3
% black file.py

This sent me down a rabbit hole of trying to figure out how vscode was misconfigured.

I tried changing black-formatter.importStrategy to either fromEnvironment or useBundled but both settings resulted in the same error message...

2024-05-03 10:30:50.144 [info] error: cannot format (file.py): Cannot parse: f"{"value"}"

How can I check which version of black the extension uses?

karthiknadig commented 2 months ago

@rjwalters In the logs Output > Black Formatter, it should have the exact version that is loaded by the extension. image

karthiknadig commented 2 months ago

@rjwalters When you run black from the command line, it might run black from different location than the one that the extension runs. This really depends on which python you have selected. because python -m black can be different depending on which python in running. The useBundled case will always run the bundled version and the version info there should be available in readme or in the extension welcome page from the extension view.

You can also force the extension to use the black binary that you have installed globally, by setting "black-formatter.path": ["black"]

rjwalters commented 2 months ago

Okay here is what I see for each import strategy:

2024-05-03 10:00:15.229 [info] Global settings received on server:
{
    "cwd": "/",
    "workspace": "/",
    "args": [],
    "path": [],
    "interpreter": [],
    "importStrategy": "fromEnvironment",
    "showNotifications": "off"
}

2024-05-03 10:00:15.229 [info] sys.path used to run Server:
   /Users/rwalters/.vscode/extensions/ms-python.black-formatter-2024.2.0/bundled/tool
   /opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python312.zip
   /opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12
   /opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload
   /Users/rwalters/.vscode/extensions/ms-python.black-formatter-2024.2.0/bundled/libs
2024-05-03 10:00:15.231 [info] /Users/rwalters/Documents/GitHub.nosync/python-robb/myenv/bin/python -m black --version
2024-05-03 10:00:15.231 [info] CWD formatter: /Users/rwalters/Documents/GitHub.nosync/python-robb
2024-05-03 10:00:15.275 [info] Version info for formatter running for /Users/rwalters/Documents/GitHub.nosync/python-robb:
black, 24.3.0 (compiled: no)
Python (CPython) 3.12.3

vs

2024-05-03 10:00:15.275 [info] SUPPORTED black>=22.3.0
FOUND black==24.3.0

2024-05-03 09:57:38.208 [info] Global settings received on server:
{
    "cwd": "/",
    "workspace": "/",
    "args": [],
    "path": [],
    "interpreter": [],
    "importStrategy": "useBundled",
    "showNotifications": "off"
}

2024-05-03 09:57:38.208 [info] sys.path used to run Server:
   /Users/rwalters/.vscode/extensions/ms-python.black-formatter-2024.2.0/bundled/libs
   /Users/rwalters/.vscode/extensions/ms-python.black-formatter-2024.2.0/bundled/tool
   /opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python312.zip
   /opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12
   /opt/homebrew/Cellar/python@3.12/3.12.3/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload
2024-05-03 09:57:38.210 [info] /Users/rwalters/Documents/GitHub.nosync/python-robb/myenv/bin/python -m black --version
2024-05-03 09:57:38.210 [info] CWD formatter: /Users/rwalters/Documents/GitHub.nosync/python-robb
2024-05-03 09:57:38.256 [info] Version info for formatter running for /Users/rwalters/Documents/GitHub.nosync/python-robb:
black, 24.3.0 (compiled: no)
Python (CPython) 3.12.3

2024-05-03 09:57:38.256 [info] SUPPORTED black>=22.3.0
FOUND black==24.3.0

It doesn't seem like importStrategy gets me to the same black version that gets used in the terminal...

I suppose this problem will go away when the extension updates the bundled black to 23.4.2+

karthiknadig commented 2 months ago

@rjwalters This means that the python that is being used by extension is not the same python where you installed the terminal black.

With fromEnvironment the black being used is /Users/rwalters/Documents/GitHub.nosync/python-robb/myenv/bin/python -m black Try updating that version.

> /Users/rwalters/Documents/GitHub.nosync/python-robb/myenv/bin/python -m pip -U black

Then reload.