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

Race condition during format on save #220

Closed
crucialfelix opened this issue Apr 28, 2023 · 3 comments
Closed

Race condition during format on save #220

crucialfelix opened this issue Apr 28, 2023 · 3 comments
Assignees
Labels
triage-needed Issue is not triaged.

Comments

@crucialfelix
Copy link

crucialfelix commented Apr 28, 2023

This is odd. Every time I save a python file, the last code block is duplicated.

This appears to be a race condition between vscode-black-formatter and ruff

They both reformatted this import statement in different ways:

Screenshot 2023-04-28 at 2 27 05 PM

Screenshot 2023-04-28 at 2 29 11 PM

Screen.Recording.2023-04-28.at.2.22.24.PM.mov

Then content at the bottom of the file is duplicated by that number of lines.

Screenshot 2023-04-28 at 12 35 14 PM

If I deactivate Black Formatter the problem stops.

solved: I've turned off Ruff organize imports. Only black formats now.

They shouldn't both be on, but there is a bug in either the Ruff or Black extension. file modtime should be checked.

Versions

vscode-black-formatter: v2023.2.0

VSCode: 1.77.3
x64

extensions

42Crunch.vscode-openapi
4ops.terraform
actboy168.tasks
AdamCaviness.theme-monokai-dark-soda
adelphes.google-search-ext
adpyke.vscode-sql-formatter
ahmadalli.vscode-nginx-conf
alarie-tano.new-file-from-selectionn
alefragnani.Bookmarks
alefragnani.project-manager
alefragnani.rtf
anderslanglands.softpaper
andrewcourtice.theme-aurora
andys8.jest-snippets
Anjali.clipboard-history
anseki.vscode-color
antn9x.lodash-import-sub-module
apollographql.vscode-apollo
arcticicestudio.nord-visual-studio-code
Arjun.swagger-viewer
AtticEngineering.vscode-line-wrapper
aws-scripting-guy.cform
azemoh.one-monokai
ban.spellright
batisteo.vscode-django
be5invis.vscode-icontheme-nomo-dark
betajob.modulestf
bibhasdn.django-html
bibhasdn.django-snippets
bierner.color-info
bierner.markdown-checkbox
bierner.markdown-mermaid
bierner.markdown-preview-github-styles
bierner.markdown-yaml-preamble
bokuweb.vscode-ripgrep
bradlc.vscode-tailwindcss
BriteSnow.vscode-toggle-quotes
bungcip.better-toml
champgm.cloudformation-yaml-validator
charliermarsh.ruff
chrisdias.vscode-opennewinstance
christian-kohler.npm-intellisense
christian-kohler.path-intellisense
christopherstyles.html-entities
ckolkman.vscode-postgres
clinyong.vscode-css-modules
cmstead.js-codeformer
CodeStream.codestream
cssho.vscode-svgviewer
csstools.postcss
dakara.transformer
DavidAnson.vscode-markdownlint
dbaeumer.vscode-eslint
dbankier.vscode-quick-select
deitry.apt-source-list-syntax
DengSir.template-generator-vscode
denoland.vscode-deno
derek-nextty.openapi-viewer
donjayamanne.githistory
donjayamanne.python-environment-manager
donjayamanne.typescript-notebook
DotJoshJohnson.xml
drKnoxy.eslint-disable-snippets
dsznajder.es7-react-js-snippets
dunstontc.dark-plus-syntax
eamodio.gitlens
ebrithil30.vscode-ts-auto-return-type
EditorConfig.EditorConfig
eg2.tslint
eliostruyf.vscode-front-matter
esbenp.prettier-vscode
ev3dev.ev3dev-browser
evilz.vscode-reveal
fabiospampinato.vscode-open-in-npm
fabiospampinato.vscode-todo-plus
fayras.simple-new-file
felipecaputo.git-project-manager
firsttris.vscode-jest-runner
fisheva.eva-theme
formulahendry.code-runner
foxundermoon.shell-format
Fr43nk.seito-openfile
funkyremi.vscode-google-translate
gerane.Theme-Amber
gerda.vscode-parquet-viewer
GitHub.copilot
GitHub.copilot-labs
GitHub.github-vscode-theme
GitHub.vscode-pull-request-github
goessner.mdmath
golang.go
googlecloudtools.cloudcode
GrapeCity.gc-excelviewer
GraphQL.vscode-graphql
GraphQL.vscode-graphql-execution
GraphQL.vscode-graphql-syntax
Gruntfuggly.todo-tree
gurayyarar.dummytextgenerator
hangxingliu.vscode-nginx-conf-hint
hashicorp.terraform
henoc.color-scheme-selector
herrmannplatz.npm-dependency-links
hilleer.yaml-plus-json
humao.rest-client
IgorSbitnev.error-gutters
inercia.vscode-k3d
infeng.vscode-react-typescript
iterative.dvc
ivangabriele.vscode-git-add-and-commit
jackie-onai.i18next
jakearl.search-editor-apply-changes
jakeboone02.cypher-query-language
jakob101.RelativePath
janisdd.vscode-edit-csv
jatinchowdhury18.vscode-supercollider
jebbs.plantuml
jithurjacob.nbpreviewer
jmviz.quote-list
jock.svg
joelday.docthis
jrjrjr.amberlight
jspolancor.presentationmode
jsynowiec.vscode-insertdatestring
juanallo.vscode-dependency-cruiser
julialang.language-julia
jumpinjackie.vscode-map-preview
kahole.magit
kameshkotwani.google-search
kddejong.vscode-cfn-lint
kentos.move-selection-to-new-file
KevinRose.vsc-python-indent
ko-dev.vscode-open-files-in-directory
kogai.regex-railroad-diagrams
kortina.vscode-markdown-notes
kumar-harsh.graphql-for-vscode
laurencebahiirwa.deno-std-lib-snippets
lego-education.ev3-micropython
leizongmin.node-module-intellisense
lextudio.restructuredtext
liviuschera.noctis
lkytal.FlatUI
lninl.theme-hbuilderx-soft-green-light
lokalise.i18n-ally
marcoq.vscode-typescript-to-json-schema
matangover.mypy
mdickin.markdown-shortcuts
Mebrahtom.plantumlpreviewer
mechatroner.rainbow-csv
medzhidov.hex-rgba-converter
mermade.openapi-lint
mgmeyers.markdown-writer-theme
mhutchie.git-graph
michaelgriscom.leadermode
miguelsolorio.min-theme
mitchdenny.ecdc
mohd-akram.vscode-html-format
mohsen1.react-javascript-to-typescript-transform-vscode
mquandalle.graphql
mrmlnc.vscode-remark
mrorz.language-gettext
ms-azuretools.vscode-docker
MS-CEINTL.vscode-language-pack-de
MS-CEINTL.vscode-language-pack-fr
ms-edgedevtools.vscode-edge-devtools
ms-kubernetes-tools.vscode-kubernetes-tools
ms-ossdata.vscode-postgresql
ms-python.black-formatter
ms-python.flake8
ms-python.isort
ms-python.python
ms-python.vscode-pylance
ms-toolsai.jupyter
ms-toolsai.jupyter-keymap
ms-toolsai.jupyter-renderers
ms-toolsai.vscode-jupyter-cell-tags
ms-toolsai.vscode-jupyter-slideshow
ms-vscode-remote.remote-containers
ms-vscode-remote.remote-ssh
ms-vscode-remote.remote-ssh-edit
ms-vscode.live-server
ms-vscode.makefile-tools
ms-vscode.remote-explorer
ms-vscode.vscode-typescript-tslint-plugin
ms-vsliveshare.vsliveshare
ms-vsliveshare.vsliveshare-audio
msrvida.vscode-sanddance
msyrus.go-doc
mtxr.sqltools
mushan.vscode-paste-image
mycelo.embrace
naumovs.color-highlight
neuron.neuron-IPE
nickdemayo.vscode-json-editor
nickytonline.vscode-gatsby-snippets
ninoseki.vscode-pylens
njpwerner.autodocstring
noaal.writer-mode
octref.vetur
oderwat.indent-rainbow
okitavera.vscode-nunjucks-formatter
Orta.vscode-jest
pajoma.vscode-journal
PaperFanz.ibm-color-palette-color-scheme
paulvarache.vscode-taskfile
Perkovec.emoji
pflannery.vscode-versionlens
philipbe.theme-gray-matter
pierrecapo.voir
pilotkid.lodash-import-what-you-need
PKief.markdown-checkbox
planbcoding.vscode-react-refactor
Prisma.prisma
RandomFractalsInc.geo-data-viewer
RandomFractalsInc.vscode-chartjs
RandomFractalsInc.vscode-data-preview
RandomFractalsInc.vscode-vega-viewer
rangav.vscode-thunder-client
raynigon.nginx-formatter
rbbit.typescript-hero
redhat.vscode-commons
redhat.vscode-yaml
rockingskier.copy-copy-paste
ronnidc.nunjucks
rpinski.shebang-snippets
rubberduck.rubberduck-vscode
rust-lang.rust-analyzer
rvest.vs-code-prettier-eslint
ryanluker.vscode-coverage-gutters
sajibsrs.soft-sight
salbert.comment-ts
satokaz.vscode-markdown-header-coloring
sburg.vscode-javascript-booster
scala-lang.scala
searKing.preview-vscode
shaharkazaz.git-merger
shakram02.bash-beautify
shamanu4.django-intellisense
shd101wyy.markdown-preview-enhanced
sleistner.vscode-fileutils
stackbreak.comment-divider
stkb.rewrap
streetsidesoftware.code-spell-checker
stylelint.vscode-stylelint
SubratThakur.remark-preview
svelte.svelte-vscode
systemticks.c4-dsl-extension
tahabasri.snippets
tamj0rd2.ts-quickfixes-extension
tberman.json-schema-validator
tchayen.markdown-links
tgreen7.vs-code-node-require
thenikso.github-plus-theme
tht13.html-preview-vscode
TomasHubelbauer.vscode-markdown-todo
tomoki1207.pdf
tomoyukim.vscode-mermaid-editor
tomphilbin.lodash-snippets
tootone.org-mode
TrungNgo.autoflake
twixes.pypi-assistant
Tyriar.lorem-ipsum
unifiedjs.vscode-mdx
usernamehw.errorlens
veggiemonk.solarized-light-functional
vilicvane.es-quotes
vscodevim.vim
vsls-contrib.gistfs
WakaTime.vscode-wakatime
Wattenberger.footsteps
william-voyek.vscode-nginx
wix.glean
wix.vscode-import-cost
wmaurer.change-case
wooodhead.html-to-react
wraith13.unsaved-files-vscode
xabikos.JavaScriptSnippets
xdebug.php-debug
xdebug.php-pack
yy0931.go-to-next-error
yzane.markdown-pdf
yzhang.markdown-all-in-one
ZainChen.json
zhuangtongfa.material-theme
zobo.php-intellisense
zoellner.openapi-preview
zurassic.monokai-slate
zxh404.vscode-proto3

@github-actions github-actions bot added the triage-needed Issue is not triaged. label Apr 28, 2023
@crucialfelix crucialfelix changed the title Duplicates last code block every time I save the file Race condition during format on save Apr 28, 2023
@karthiknadig
Copy link
Member

karthiknadig commented Apr 28, 2023

@crucialfelix Did this begin recently or has this been happening? The last release of black-formatter extension was 3 weeks ago:
image

You could use the command Help: Start Extension Bisect to see if this was occurring due to interaction with some other extension. By using the trace above you can monitor what black receives.

Here is my suspicion:
You have both ruff and isort which both provide organize imports on save. If you have turned this on in the settings this can occur due to microsoft/vscode#174295. I will need to see your settings to help address that one. Most likely you will need to set it like this:
image

To get detailed logs:

  1. Get rid of the duplicated code blocks and save, before opening the project in VS Code.
  2. Launch vs code, and Run the command Developer: set log level ..., change the level to trace.
  3. Open the file that cause issue, and save the file (to trigger formatting).
  4. In the Output > Black Formatter you should see logs with content of file before black receives it and after black processes it.

Please share the logs, and I would also like to see your user and workspace settings.

@karthiknadig karthiknadig self-assigned this May 1, 2023
@crucialfelix
Copy link
Author

crucialfelix commented May 2, 2023

@karthiknadig I can now confirm it's ruff and isort. I disabled isort and the duplication problem is solved.

I will post my logs on the other issue.

It's not black at all.

Note: I set log level to trace, triggered the error (with both ruff and isort going at it)

The logs do not show the content of the file for me. Just that it got run.

2023-05-02 08:49:38.244 [info] SUPPORTED black>=22.3.0
FOUND black==23.3.0

2023-05-02 08:49:51.486 [info] [Trace - 8:49:51 AM] Sending notification 'textDocument/didOpen'.
2023-05-02 08:49:57.904 [info] [Trace - 8:49:57 AM] Sending notification 'textDocument/didOpen'.
2023-05-02 08:50:07.120 [info] [Trace - 8:50:07 AM] Sending notification 'textDocument/didChange'.
2023-05-02 08:50:11.911 [info] [Trace - 8:50:11 AM] Sending notification 'textDocument/didChange'.
2023-05-02 08:50:11.936 [info] [Trace - 8:50:11 AM] Sending request 'textDocument/formatting - (1)'.
2023-05-02 08:50:11.950 [info] [Trace - 8:50:11 AM] Received notification 'window/logMessage'.
2023-05-02 08:50:11.950 [info] /Users/crucialfelix/.pyenv/versions/3.10.0/bin/python -m black --line-length 100 --stdin-filename /Users/crucialfelix/github/client/backend/eu_project/services/extract_project.py -
2023-05-02 08:50:11.950 [info] [Trace - 8:50:11 AM] Received notification 'window/logMessage'.
2023-05-02 08:50:11.950 [info] CWD formatter: /Users/crucialfelix/github/client
2023-05-02 08:50:12.342 [info] [Trace - 8:50:12 AM] Received notification 'window/logMessage'.
2023-05-02 08:50:12.342 [info] reformatted /Users/crucialfelix/github/client/backend/eu_project/services/extract_project.py

All done! ✨ 🍰 ✨
1 file reformatted.

2023-05-02 08:50:12.343 [info] [Trace - 8:50:12 AM] Received response 'textDocument/formatting - (1)' in 407ms.
2023-05-02 08:50:12.355 [info] [Trace - 8:50:12 AM] Sending notification 'textDocument/didChange'.
2023-05-02 08:50:15.877 [info] [Trace - 8:50:15 AM] Sending notification 'textDocument/didChange'.
2023-05-02 08:50:15.904 [info] [Trace - 8:50:15 AM] Sending notification 'textDocument/didChange'.
2023-05-02 08:50:15.918 [info] [Trace - 8:50:15 AM] Sending request 'textDocument/formatting - (2)'.
2023-05-02 08:50:15.922 [info] [Trace - 8:50:15 AM] Received notification 'window/logMessage'.
2023-05-02 08:50:15.922 [info] /Users/crucialfelix/.pyenv/versions/3.10.0/bin/python -m black --line-length 100 --stdin-filename /Users/crucialfelix/github/client/backend/eu_project/services/extract_project.py -
2023-05-02 08:50:15.922 [info] [Trace - 8:50:15 AM] Received notification 'window/logMessage'.
2023-05-02 08:50:15.922 [info] CWD formatter: /Users/crucialfelix/github/client
2023-05-02 08:50:16.270 [info] [Trace - 8:50:16 AM] Received notification 'window/logMessage'.
2023-05-02 08:50:16.270 [info] reformatted /Users/crucialfelix/github/client/backend/eu_project/services/extract_project.py

All done! ✨ 🍰 ✨
1 file reformatted.

2023-05-02 08:50:16.270 [info] [Trace - 8:50:16 AM] Received response 'textDocument/formatting - (2)' in 352ms.
2023-05-02 08:50:16.279 [info] [Trace - 8:50:16 AM] Sending notification 'textDocument/didChange'.

@karthiknadig
Copy link
Member

@crucialfelix You can set this in the settings to control which import sorter you are using:

    "editor.codeActionsOnSave": {
        "source.organizeImports.ruff": true
    }

The logs do not show the content of the file for me. Just that it got run.

I will investigate that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage-needed Issue is not triaged.
Projects
None yet
Development

No branches or pull requests

2 participants