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

Extension does not seem to use the local pyproject.toml #519

Open
nbro opened this issue Jun 17, 2024 · 6 comments
Open

Extension does not seem to use the local pyproject.toml #519

nbro opened this issue Jun 17, 2024 · 6 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug triage-needed Issue is not triaged.

Comments

@nbro
Copy link

nbro commented Jun 17, 2024

Diagnostic Data

  • Python version (& distribution if applicable, e.g., Anaconda): 3.10.14
  • Type of virtual environment used (e.g., conda, venv, virtualenv, etc.): poetry
  • Operating system (and version): MacOS Monterey
  • Version of tool extension you are using: v2024.2.0

Behaviour

When I save my .py files, the extension converts single-quote strings to double-quote strings, even though I have the following in my pyproject.toml

[tool.black]
target-version = ["py310"]
exclude = "migrations"
skip-string-normalization = true

I tried to call black directly from the command-line and it does do this, so it works as expected.

But now suddenly it stopped ignoring the single-quotes and it doesn't format them anymore. If I change my settings in pyproject.toml to

[tool.black]
target-version = ["py310"]
exclude = "migrations"
# skip-string-normalization = true

Now, it doesn't format the single-quotes to double-quotes.

Maybe this is a cache problem?

Logs:

2024-06-17 15:39:46.372 [info] /Users/me/Library/Caches/pypoetry/virtualenvs/myproject-GRsX74iM-py3.10/bin/python -m black --stdin-filename /mydir/file.py -
2024-06-17 15:39:46.372 [info] CWD formatter: /mydir
2024-06-17 15:39:46.392 [info] reformatted /mydir/file.py

All done! ✨ 🍰 ✨
1 file reformatted.

Outcome When Attempting Debugging Steps:

Did running it from the command line work? No

If I run /Users/me/Library/Caches/pypoetry/virtualenvs/myproject-GRsX74iM-py3.10/bin/python -m black --stdin-filename /mydir/file.py from the terminal, I get the error

Usage: python -m black [OPTIONS] SRC ...

One of 'SRC' or 'code' is required.

So, I tried /Users/me/Library/Caches/pypoetry/virtualenvs/myproject-GRsX74iM-py3.10/bin/python -m black --stdin-filename SRC /mydir/file.py and it works now.

@nbro nbro added the bug Issue identified by VS Code Team member as probable bug label Jun 17, 2024
@github-actions github-actions bot added the triage-needed Issue is not triaged. label Jun 17, 2024
@karthiknadig karthiknadig self-assigned this Jun 17, 2024
@MarcusVirg
Copy link

MarcusVirg commented Jun 22, 2024

I have the same problem. I have Black settings in my pyproject.toml to configure line length. When I run black from cli directly it works fine but not on editor save with the extension. This happens on either import strategy useBundled or fromEnvironment. Here are my workspace settings:

{
    "cwd": "/workspaces/core-service",
    "workspace": "file:///workspaces/core-service",
    "args": [],
    "path": [],
    "interpreter": [
        "/home/vscode/.cache/pypoetry/virtualenvs/core-service-Td9uKyt_-py3.12/bin/python"
    ],
    "importStrategy": "fromEnvironment",
    "showNotifications": "off"
}

My pyproject.toml file lives at /workspaces/core-service/pyproject.toml

@karthiknadig
Copy link
Member

Can you try running it using this command line? The extension jsut run black but uses stdin to send code. The exact command line equivalent is this:

cat /mydir/file.py | python -m black --stdin-filename /mydir/file.py -

@karthiknadig
Copy link
Member

Additionally, you can use "black-formatter.args" : ["--config", "${workspaceFolder}/pyproject.toml"] to explicitly ask black to use pyproject.toml

@MarcusVirg
Copy link

MarcusVirg commented Jun 22, 2024

Thanks for responding! I used the black-formatter args directly to set the line-length configuration and my files were being formatted incorrectly but I found the real issue. I have black as my default formatter for python files in my VSCode settings but VSCode was still using the autopep8 extension to format. Uninstalling this extension fixed the issue and Black is now working correct. Settings are now being respected in pyproject.toml as well.

Sorry for the false positive there.
For anyone else coming along this issue, try uninstalling the autopep8 formatter extension first (it might get added automatically when installing the ms-python.python extension)

@cmditch
Copy link

cmditch commented Jul 1, 2024

@MarcusVirg oof thanks for your post! I've been having the same issue for weeks. Despite having black as my formatter, vscode was still using autopep8. I don't even remember installing autopep8. When opening the command palette and typing > Format document with... I could see that vscode was using autopep8, and it was set as the default (despite what was in my settings).

This has been such a thorn in my side, glad I found a fix.

Would be nice if we could fix the bug which is causing vscode to use autopep8, despite whatever settings one is using.

@derekwisong
Copy link

This worked for me

"[python]": {
    "editor.defaultFormatter": "ms-python.black-formatter"
},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug triage-needed Issue is not triaged.
Projects
None yet
Development

No branches or pull requests

5 participants