sourcery-ai / sourcery

Instant AI code reviews
https://sourcery.ai
MIT License
1.5k stars 65 forks source link

Sourcery plugin in VSCode uses too much process #405

Open PhunkyBob opened 7 months ago

PhunkyBob commented 7 months ago

Checklist

Description

In VSCode, Sourcery is always scanning my code, even if I don't touch it. It uses a lot of processor time. So much process that the others features of the IDE are not working well (Pylance, ...). Sometimes I type a variable and my IDE takes 10 seconds to understand that the variable exists!

If I look in the VSCode notification, I can see Sourcery is scanning 10 times the same file. OOrbNoABcz

Code snippet that reproduces issue

No particular code. It do so on every Python script.

Debug Information

IDE Version: VSCode Version: 1.84.2 (user setup) Commit: 1a5daa3a0231a0fbba4f14db7ec463cf99d7768e Date: 2023-11-09T10:51:52.184Z Electron: 25.9.2 ElectronBuildId: 24603566 Chromium: 114.0.5735.289 Node.js: 18.15.0 V8: 11.4.183.29-electron.0 OS: Windows_NT x64 10.0.19045

Sourcery Version: v1.14.0

Operating system and Version: Windows 10

ruancomelli commented 7 months ago

Hello, @PhunkyBob! Thanks for reporting this issue.

This is definitely a strange bug. If I understand correctly:

  1. Sourcery does this for every Python script in your project
  2. You don't even need to open the file for Sourcery to start scanning it

Is this correct?

If so, can you please send your Sourcery log file to support@sourcery.ai?

PhunkyBob commented 7 months ago

Sourcery does this for every Python script in your project

Not every script. At this time, it scans in loop 2 of my files.

You don't even need to open the file for Sourcery to start scanning it

Yes. Sometimes it scans files that are not opened in my IDE.

Log file has been sent by email.

ruancomelli commented 7 months ago

Thank you for your reply and for sending your log file, @PhunkyBob! We will investigate this issue and fix it as soon as we can.

blakeNaccarato commented 6 months ago

I've been tracking this issue since it was closed by the original poster (in my opinion unresolved) over in https://github.com/sourcery-ai/sourcery-vscode/issues/150. I believe that the identified root cause is correct, that Pylance and Sourcery are not playing nicely together.

However, the conclusion seems to be that it's not Sourcery's responsibility (or was understandably not top priority at the time), since these files are being opened by Pylance and thus triggering the re-scanning. I suspect that Pylance+Sourcery is not an entirely uncommon combination, so maybe this issue can be revisited (either here or in the VSCode extension repo)?

I think the conclusion reached in https://github.com/sourcery-ai/sourcery-vscode/issues/150 is not quite right, because lots of these pending Sourcery scans get banished in a limbo state, never actually completing their scan because perhaps there is no file left to scan. I believe Pylance creates ad-hoc temporary files when scanning, especially with Jupyter notebooks it creates temporary Python scripts per-cell and scans those to report on notebook contents. I imagine that the temporary files are necessitated by some limitation I'm not aware of.

I suspect the solution options might look like the following:

Some contact with the Pylance team might help chase this problem down. Here are some quoted snippets from the earlier issue for some more context:

https://github.com/sourcery-ai/sourcery-vscode/issues/150#issuecomment-1561703574 As you suggested, it seems that many files are being opened and closed, triggering Sourcery reviews multiple times. Just note that Sourcery does not open/close files for you - instead, a Sourcery review is triggered every time a file is opened via other means.

https://github.com/sourcery-ai/sourcery-vscode/issues/150#issuecomment-1567234715 After a little big of extra digging, I've narrowed this down to an issue with Pyright/Pylance going crazy and opening and closing files en masse, so I'm fairly certain this is not an actual bug with Sourcery, so I'm closing this issue. I've since put a pyrightconfig.json file in my project with a lot of explicit exclusions and inclusions and it seems to have calmed down.

I've configured my pyrightconfig.json various ways, and have never been able to get Sourcery to calm down. For now, I just set my VSCode notifications to "silent mode" and ignore the dozen or so infinite-duration Sourcery notifications that pop up in my repos.

PhunkyBob commented 6 months ago

lots of these pending Sourcery scans get banished in a limbo state, never actually completing their scan because perhaps there is no file left to scan

Sometimes, I create a new file ("Untitled 1") and I quickly save it as "[something].py". But I see in the Sourcery window a loop in scanning "Untitled 1". This coroborates your statement.