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

Exclude **/node_modules/** from .vscode/launch.json searches #1234

Merged

Conversation

gluxon
Copy link
Contributor

@gluxon gluxon commented Oct 11, 2022

This is a strange PR and I would have no qualms with it being rejected.

Problem

Excluding **/node_modules/** from vscode.workspace.findFiles(...) calls is somewhat necessary to prevent excessive CPU usage when this extension is installed and users open a large frontend project in VS Code. findFiles searches the entire repo by default, even .gitignore files.

My teammates are running into this excessive CPU usage problem as they jump between frontend repos and Java repos in VS Code.

Workaround

One comment from a VS Code member recommends extensions exclude node_modules since a proper fix to the findFiles API would be a breaking change: microsoft/vscode#75314 (comment)

This seems to be somewhat standard, even in extensions that have nothing to do with Node.js. For example:

I've also PR'ed a similar change in other repos:

Would this be reasonable in vscode-java-debug as well?

Risks

The docs on workspace.findFiles(...) say an undefined 2nd argument defaults to the user's files.exclude setting. Changing this argument to **/node_modules/** means this search no longer excludes the user configured files.exclude. If that's a concern, I think we can concatenate **/node_modules/** after looking up the files.exclude setting.

@testforstephen
Copy link
Contributor

@gluxon this is a good finding, thanks for bringing it up for our awareness.

const results: vscode.Uri[] = await vscode.workspace.findFiles(".vscode/launch.json");
// Excluding "**/node_modules/**" as a common cause of excessive CPU usage.
// https://github.com/microsoft/vscode/issues/75314#issuecomment-503195666
const results: vscode.Uri[] = await vscode.workspace.findFiles(".vscode/launch.json", "**/node_modules/**");
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm OK with excluding both node_modules and .git.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! So to be clear, updating this argument **/node_modules/** to {**/node_modules/**,.git/**} would be ideal?

I'll test that out to make sure I have the right glob syntax.

Copy link
Contributor

Choose a reason for hiding this comment

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

It would be interesting if there is some performance comparison data to share.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like the 2nd argument appends rather than replaces. The existing command in this PR:

await vscode.workspace.findFiles(".vscode/launch.json", "**/node_modules/**");

runs the following process:

'/Applications/Visual Studio Code.app/Contents/Resources/app/node_modules.asar.unpacked/@vscode/ripgrep/bin/rg' \
    --files \
    --hidden \
    --case-sensitive \
    -g '/.vscode/launch.json' \
    -g '!**/node_modules/**' \
    -g '!**/.git' \
    -g '!**/.svn' \
    -g '!**/.hg' \
    -g '!**/CVS' \
    -g '!**/.DS_Store' \
    -g '!**/Thumbs.db' \
    --no-ignore \
    --follow \
    --no-config \

The -g '!**/.git' exclusion comes from VS Code's default settings.

{
  "files.exclude": {
    "**/.git": true,
    "**/.svn": true,
    "**/.hg": true,
    "**/CVS": true,
    "**/.DS_Store": true,
    "**/Thumbs.db": true
  }
}

https://github.com/microsoft/vscode/blob/2798da9d47387c0fb6ab4ebbead95efb5a85c250/src/vs/workbench/contrib/files/browser/files.contribution.ts#L143

It would be interesting if there is some performance comparison data to share.

Definitely. I was curious too, but hadn't had time until now to run some tests.

Without the **/node_modules/** exclusion, the rg process runs for over an hour at 1100%+ CPU in the repo my team works on. I can run the process overnight to see exactly how long it takes if that's something we're curious about.

Here's some data for comparing whether .git is excluded. For my specific machine and repo:

node_modules Excluded .git Excluded Trial 1 Trial 2 Average
1 hour+ - 1 hour+
536ms 493ms 514.5ms
788ms 477ms 632.5ms

It looks like excluding .git is fairly negligible in terms of timing. Either way, I don't think we have to do something explicit here since .git should be excluded unless users explicitly set files.excludes["**/.git"] to false?

Copy link
Contributor

Choose a reason for hiding this comment

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

Great to see the perf data, thanks for sharing that. Looks like it's enough to exclude "node_modules" only.

Copy link
Member

Choose a reason for hiding this comment

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

Looks like this utility can get another improvement if we set the workspaceFolder as the base part of the relative pattern

https://github.com/microsoft/vscode/blob/4b759b0492d1df969e8595e3db0f66f6058c3643/src/vscode-dts/vscode.d.ts#L2116

And then limit the search maxResults to 1

https://github.com/microsoft/vscode/blob/4b759b0492d1df969e8595e3db0f66f6058c3643/src/vscode-dts/vscode.d.ts#L12171

Copy link
Contributor

Choose a reason for hiding this comment

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

I created an issue to track the further optimization. #1252

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jdneo Those both look like great suggestions. Thanks!

@gluxon gluxon marked this pull request as ready for review October 18, 2022 03:58
@gluxon
Copy link
Contributor Author

gluxon commented Oct 18, 2022

@gluxon this is a good finding, thanks for bringing it up for our awareness.

Thank you for reviewing and considering it! This change will be a big help to my team.

@testforstephen testforstephen merged commit ac25688 into microsoft:main Oct 19, 2022
@testforstephen
Copy link
Contributor

@gluxon thanks for contribution.

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.

3 participants