Skip to content

Fix problem with too many ripgrep processes being spawned by VSCode #1287

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

Merged
merged 6 commits into from
Apr 7, 2025

Conversation

thecrypticace
Copy link
Contributor

@thecrypticace thecrypticace commented Mar 26, 2025

We run a file search on a per-workspace basis but we wait until a file has been opened. However, there are two big problems here:

  • A file being "opened" does not mean it is visible. Just that an extension has, effectively, taken an interest in it, can read its contents, etc. This happens for things like tsconfig files, some files inside a .git folder, etc…

  • We're running the search any time we see an opened document. What should happen is that we run the search when a document is opened and visible, the language server has not started, and we need to checking a workspace folder that has not been searched yet.

This code here needs to be restructured to ensure that these searches only run when they are needed. If the searches don't return anything or time out then they should not be run again. Notifications from file watching should take care of the rest in case the initial search turned up nothing and the user adds a file that should cause the server to start.

Fixes #986 (for real maybe this time??)

@thecrypticace
Copy link
Contributor Author

So I've made some changes here:

  • When multiple requests are made to start the search, it starts once

  • Additionally, all requests to do so will await the same promise

  • If the search is cancelled, it will not start again

  • It analyzes all workspace folders

  • The config file search must complete before the stylesheet search

  • If a config file is found, the stylesheet search is skipped

  • The config file search checks all workspace folders concurrently

  • The stylesheet search checks all workspace folders concurrently

  • Stylesheets are read one at a time to minimize disk I/O

We do still listen to onDidOpenTextDocument(…) which can be called even for documents not in a visible editor. This is fine because the search only happens one time. After checking a number of other extensions they all pretty much use onDidOpenTextDocument for stuff.

@thecrypticace thecrypticace requested a review from Copilot March 28, 2025 23:56
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses the issue of spawning too many ripgrep processes by introducing caching for the file search operation. Key changes include:

  • Introducing a new module-level variable, pendingSearch, to cache the pending search promise.
  • Refactoring activate() to use pendingSearch via the nullish coalescing assignment (??=) and awaiting that promise.
  • Ensuring that bootClientIfNeeded() is invoked only once per activation, reducing redundant searches.

@thecrypticace thecrypticace force-pushed the fix/multiple-searches-on-multiple-documents branch from 50432c9 to 0fc5697 Compare March 28, 2025 23:58
@thecrypticace thecrypticace requested a review from Copilot March 28, 2025 23:59
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the handling of file searches for Tailwind CSS in VSCode to prevent unnecessary spawning of ripgrep processes. Key changes include removing redundant file exclusion logic from extension.ts, creating dedicated modules (exclusions.ts, api.ts, analyze.ts) to manage file analysis and exclusion, and updating the search logic to run only when a document is visible and the language server requires it.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
packages/vscode-tailwindcss/src/extension.ts Refactored search logic to use a new API and removed redundant exclusion and cancellation code.
packages/vscode-tailwindcss/src/exclusions.ts Moved file exclusion logic into its own module.
packages/vscode-tailwindcss/src/api.ts Introduced an API for checking if the language server should be started.
packages/vscode-tailwindcss/src/analyze.ts Moved file analysis logic (including searching for Tailwind CSS-related patterns) into a separate module.
Comments suppressed due to low confidence (1)

packages/vscode-tailwindcss/src/api.ts:39

  • The function name 'stylesheeNeedsLanguageServer' appears to be misspelled. Consider renaming it to 'stylesheetNeedsLanguageServer' for clarity.
async function stylesheeNeedsLanguageServer(uri: Uri) {

@thecrypticace thecrypticace marked this pull request as ready for review March 29, 2025 01:01
@thecrypticace thecrypticace force-pushed the fix/multiple-searches-on-multiple-documents branch from 4436f5a to 027a18a Compare April 7, 2025 15:33
Copy link
Member

@RobinMalfait RobinMalfait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did generate a lot of files in a mono repo setup and didn't run into any issues.

I also checked that it still works as expected on "normal" projects.

@thecrypticace thecrypticace merged commit f8313ab into main Apr 7, 2025
12 checks passed
@thecrypticace thecrypticace deleted the fix/multiple-searches-on-multiple-documents branch April 7, 2025 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tailwindcss-intellisense spawns ripgrep process that chews CPU on a project without Tailwindcss
3 participants