Repository navigation
black is using cached pyproject.toml instead of the actual one #569
Description
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
- 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- Setup .black in your home directory (e.g. C:\Users\xxx\ if you're on Windows).
[tool.black]
line-length = 120
- Restart VS Code just to be sure.
- Run "Format Document" on test.py. For more verbosity I've included
print('line length', line_length)toblack/__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.
- Change line-length in .black to 90 and run "Format Document" again.
- Output is exactly the same as above, line_length is still 120. So extension is ignoring updated .toml file.
- Just to be sure that it's not a general
blackissue, let's runblackfrom 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.- 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
karthiknadig commentedon Apr 18, 2025
Andrej730 (@Andrej730) In normal cases the extension loads
blacklike 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
runServeron change. That will automatically restart the server.enumura1 commentedon Apr 28, 2025
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