VS Code File Watcher Optimization

I’ve been bumping into an odd thing,

Error: ENOSPC: System limit for number of file watchers reached

And yet again StackOverflow to the rescue. Turns out this is Linux specific, but the fix is probably worthwhile or relevant for those on other OSes as well.

Ram Eater

VS Code consumes a fair bit of RAM, is what it is, but turns out at least in my case, part of this is unnecessary. Code watches all files in a workspace, NBD by itself probably, but it gets to be more of an issue for larger projects. Where it can really enter problem territory is that Webpack and Metro (used by ReactNative) and Grails also watch a lot of these same files for changes, so as my projects and my Code workspace has grown it has snowballed a bit.

A little less hungry

In Linux there is a default max number of actively tracked files for the OS, and I was bumping into this. That was causing my local builds to fail. Of course, I could always bump that number up, there is no real problem in doing that, but I am having some memory constraint issues on my laptop currently anyway - that 16MB is no longer enough still gives me a bit of a headache, but that is another post. Anyway, it felt worth it to see if I could just reduce the number of watched files. There are a bunch of generated artifacts inside of these project dirs that I bet it would be best for my editor to ignore. And indeed, VS Code has a setting for “watcherExclude” that covers this use case. The defaults are pretty good, but for me it was worth adding some workspace specific ones.

There is a decent GUI for editing settings and workspace specific settings too.

workspace setting gui

The un-menu

General:

1
**/.idea/**`

Excludes for a grails dir:

1
2
/[grailsProjectDir]/build/**": true,
/[grailsProjectDir]/lib/*": true,

Excludes for react project dir:

1
2
3
**/__snapshots__/**
/[reactProjectDir]/build/static/css/*
/[reactProjectDir]/build/static/js/*

Excludes for a reactNative dir:

1
/[reactNativeProjectDir]/android/app/build/**

Satisfied?

This seems to have helped avoid the problem I was seeing, for now at least. I am tempted to also exclude snapshot files or some other generated files.