VS Code File Watcher Optimization

Published on

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 16GB is no longer enough still gives me a bit of a headache, but that is another post - so 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:

**/.idea/**`

Excludes for a grails dir:

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

Excludes for react project dir:

**/__snapshots__/**
/[reactProjectDir]/build/static/css/*
/[reactProjectDir]/build/static/js/*

Excludes for a reactNative dir:

/[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.

Update

It sort of helped for a while, but I kept bumping into the problem. I did try ignoring a few more things, but to no avail. Just closing VS Code, always seemed to be a sure way to make it go away for a while, so pretty sure VS Code itself or one of the extensions I have installed in it are the big culprit, but anyway I just maxed out the max watchable files configuration and the problem has not re-emerged, so far…