-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Changes to files happing quickly after running black
are not detected
#4116
Comments
(moved out of the description) I ran into this bug while writing automated tests for a script that is used to merge branches where I'm assuming that this is the classic problem of detecting file modifications on file systems with low timestamp resolution. My tests are done on APFS, idk what resolution it uses, but it looks like Python only sees whole seconds:
Let's say it's The usual solution to this is to only record a cache entry if the timestamp of the file is strictly older ( I'm not sure what the best way is to get the current time An alterniatve would be to assume some maximum error due to rounding (e.g. 2 seconds) and then subtract that from |
Thanks for the issue! Note that Python seems to print st_mtime rounded when it isn't necessarily:
I think this is a regression — @cdce8p do you remember why #3821 starts rounding mtimes in the cache? I'm guessing you were copying logic inside mypy, which based on python/mypy@f63ccbb I'm thinking doesn't really applies to black? Opened #4128 to remove it, let me know what you think |
Yeah, I looked at the mypy implementation when I added that feature to black. My best guess (why mypy is doing so) is that it might help reduce the overall cache size if the mtimes are stored as integers instead of floats. IIRC that's also what I had in mind for black but alas I never seem to have added it. Removing the int conversion in #4128 seems fine to me. |
#4128 indeed fixes the behavior!
(no space between |
Describe the bug
It seems
black
does not detect that a file has been changed and needs to be re-checked, if the change happens quickly afterblack
has been run on the file.I'm talking about this feature: Ignoring unmodified files
To Reproduce
I'm using the following shell script to reproduce the issue:
The script writes overwrites the file and then runs
black
, doing this twice in a row. In the second run,black
should reformatprint ()
toprint()
, but this does not happen:Even running it manually after a few seconds does not fix the issue, but removing the cache directory does:
Expected behavior
I think black should not get confused by changes to files that happen quickly after it has formatted a file. The file should be checked again if it is possible that its content has changed without also changing its timestamp.
Environment
Black's version:
OS and Python version: macOS 12.7.1, Python 3.11.6 installed via Homebrew.
The text was updated successfully, but these errors were encountered: