Visitar URL original
black is using cached pyproject.toml instead of the actual one · Issue #569 · microsoft/vscode-black-formatter · GitHub
Skip to content

black is using cached pyproject.toml instead of the actual one #569

Description

@Andrej730

Diagnostic Data

  • Python version: Python 3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)] on win32
  • Operating system (and version): Windows 11
  • Version of tool extension you are using: 2025.2.0

py -m black --version
python -m black, 25.1.1.dev27+gd0ff3bd (compiled: no)
Python (CPython) 3.11.9

Black formatter extension settings:

  • "fromEnvironment" option
  • only additional arg is --verbose

Behaviour

  1. Create test.py, it's 93 symbols long.
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
  1. Setup .black in your home directory (e.g. C:\Users\xxx\ if you're on Windows).
[tool.black]
line-length = 120
  1. Restart VS Code just to be sure.
  2. Run "Format Document" on test.py. For more verbosity I've included print('line length', line_length) to black/__init__.py.
Using configuration from user-level config at 'L:\Users\xxxx\.black'.
line_length: 120
Found input source: "__BLACK_STDIN_FILENAME__l:\test\5_fifth_test.py"
l:\test\5_fifth_test.py already well formatted, good job.

All done! ✨ 🍰 ✨
1 file left unchanged.
  1. Change line-length in .black to 90 and run "Format Document" again.
  2. Output is exactly the same as above, line_length is still 120. So extension is ignoring updated .toml file.
  3. Just to be sure that it's not a general black issue, let's run black from terminal. As expected, it's using the actual config, not the cached version.
cat L:\test\5_fifth_test.py | py -m black --verbose --stdin-filename l:\test\5_fifth_test.py -
line length 90
Identified `L:\test` as project root containing a .git directory.
Using configuration from user-level config at 'L:\test\config\.black'.
line_length: 90
Found input source: "__BLACK_STDIN_FILENAME__l:\test\5_fifth_test.py"
(
    1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
    + 1
)
reformatted l:\test\5_fifth_test.py

All done! ✨ 🍰 ✨
1 file reformatted.
  1. Restarting Black formatter server helps.
Using configuration from user-level config at 'L:\Users\xxx\.black'.
line_length: 90
Found input source: "__BLACK_STDIN_FILENAME__l:\test\5_fifth_test.py"
reformatted l:\test\5_fifth_test.py

All done! ✨ 🍰 ✨
1 file reformatted.

Expected Behavior

Extension to use the latest version of config to avoid confusion when config is edited during development and user is unsure whether the config was applied or not.

Actual Behavior

Extension is using the cached version until server restart.


Possibly related #519

No idea how exactly extension works, but I suspect it's running Python instance in background that's running black formatter with Python commands - noticed that extension output still had debug statements I've inserted into black for tests, though I've already removed them from the code.
And then maybe black is using lru_cache somewhere when it's parsing the config file and extension ends up also reusing the same config file because of it.

Activity

  1. karthiknadig commented on Apr 18, 2025

    @karthiknadig
    Member

    Andrej730 (@Andrej730) In normal cases the extension loads black like a module and runs it by calling main. I suspect that is what is happening, black is getting loaded and when changes occur to config files it is not getting picked up.

    This should be easy enough to fix. We need to use a file watcher to check if any of the known configs are updated, then use the runServer on change. That will automatically restart the server.

  2. enumura1 commented on Apr 28, 2025

    @enumura1

    I've addressed this issue and opened a PR.
    When you have time, I'd appreciate it if you could take a look.
    https://github.com/microsoft/vscode-black-formatter/pull/571/files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue identified by VS Code Team member as probable buggood first issueGood for newcomershelp wantedIssues identified as good community contribution opportunitiesneeds PR

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions