77

I just started using VSCode version 1.24.1.

After loading a folder, it shows warning

Visual Studio Code is unable to watch for file changes in this large workspace

After i check the limit as suggested on their guide, using

cat /proc/sys/fs/inotify/max_user_watches

I get 8192, while my project has only 650 files (of which 400 are inside .git)

Why does this happen? Is this a bug or am I missing something?

(Increasing the limit is clearly not the right solution.)

4
  • 3
    Have you tried to increase the limit? From the same page: “The limit can be increased to its maximum by editing /etc/sysctl.conf and adding this line to the end of the file: fs.inotify.max_user_watches=524288”, followed by sudo sysctl -p.
    – grooveplex
    Jun 17, 2018 at 23:07
  • That is not a solution. Check my answer after 5 min. I think I got it.
    – DonJoe
    Jun 17, 2018 at 23:08
  • 1
    The limit is not just for Visual Studio, and may have been exceeded even before VSCode is launched Dec 27, 2018 at 8:54
  • VSCode 1.61 (Sept. 2021) comes with a new files watcher which should improve this issue.
    – VonC
    Oct 3, 2021 at 19:12

16 Answers 16

108

what linux ppl dont know, there are ppl new to linux like me. So if you're a noob, this is for you.

  1. Open a new terminal.
  2. cat /proc/sys/fs/inotify/max_user_watches (might be a number 8k+)

now (a) for vim-Editor

  1. (a) sudo vim /etc/sysctl.conf
  2. (a) go all the way down and add a new line with: fs.inotify.max_user_watches=524288 (make sure you DONT have a # in front of the command)
  3. (a) type :wq! and press enter

or (b) for nano-Editor (thanks to @bradrar)

  1. (b) sudo nano /etc/sysctl.conf

  2. (b) go all the way down and add a new line with: fs.inotify.max_user_watches=524288 (make sure you DONT have a # in front of the command)

  3. (b) type ctrl + x, type y and press enter

  1. type sudo sysctl -p
  2. type again: cat /proc/sys/fs/inotify/max_user_watches (should be 500k+ now)
  3. (thank me later.)
3
  • 7
    for people who don't have vim, try sudo nano /etc/sysctl.conf , then step 4, then instead of :wq! hit ctrl+x, click y and enter
    – bradrar
    Nov 17, 2019 at 8:42
  • I'm confused - do I run this on my local macOS, or on the remote? (centos in my current case) Those files don't exist on my macOS, but does on the remote...
    – Jonny
    Mar 3, 2020 at 10:01
  • 1
    this is a linux solution. dont know about any mac related stuff... Mar 3, 2020 at 10:41
32

The solution I found and it's work for me is

add this line fs.inotify.max_user_watches=524288 in to /etc/sysctl.conf

and then run the command sudo sysctl -p

and then go to your vscode settings find a file called settings.json

and this line to it

"files.watcherExclude": {
    "**/.git/objects/**": true,
    "**/.git/subtree-cache/**": true,
    "**/node_modules/*/**": true
  }

you can also refer this link https://code.visualstudio.com/docs/setup/linux#_visual-studio-code-is-unable-to-watch-for-file-changes-in-this-large-workspace-error-enospc

6
  • 4
    My project has only 650 files (of which 400 are inside .git), so I don't think that is the acceptable solution.
    – DonJoe
    Nov 20, 2018 at 16:45
  • 1
    Not working for me. Rather not increase the file size at OS level Dec 1, 2018 at 17:18
  • 1
    set fs.inotify.max_user_watches=524288 worked for me
    – Mathiasfc
    Jun 4, 2019 at 13:35
  • 3
    Editing files.watcherExclude would be a nice solution if this persistent bug did not exist. And I bet it's gonna stay there for long..
    – Marinos An
    Apr 22, 2020 at 9:10
  • will this prevent vscode from detecting git related code changes?
    – mike01010
    Apr 1 at 0:45
23

The fact that you are out of watches does not mean that its VSCode's fault.

VSCode has for sure issues with excluding directories from watch (on linux) (update: most are recently resolved here, and here)

But since you have counted the files yourself, the error message in this case is probably misleading and some other application has already exhausted watches.

To trace the guilty app you can use this nice script

0
8

In my case I do not have enough privileges to change the sysctl.conf, so my solution for Ubuntu 18.04 LTS was:

sudo /bin/su -c "echo 'fs.inotify.max_user_watches=524288' >> /etc/sysctl.conf"
sudo sysctl -p
1
  • 7
    what do you mean that you do not have the priviledge to change sysctl.conf when you can use sudo and attach text to the file? :)
    – leo
    Jan 11, 2021 at 19:13
8

In my case (PHP using Composer), I had to exclude vendor path from watching

enter image description here

Depending on your case, you should exclude your dependencies folder.

1
  • Correct ! Wishing this was more obvious. For Python users, note that VSCode is trying to watch your environment folder, i.e. miniconda3/envs/my_env_name/.... Aug 24 at 12:53
7

Following settings worked for me (inside .vscode/settings.json, you can also put them at user level / system level settings in vscode instead of workspace level settings)

"files.watcherExclude": {
        "**/.git": true,
        "**/.DS_Store": true,
        "**/node_modules": true,
        "**/some-soft-link-to-higher-level-directory-in-my-file-system" : true,
        "**/.cache-loader" : true
}

.git, node_modules are perhaps excluded by default, but depending on your workspace, you may need to add others. As an example, I had a soft link to a higher level directory in my file-system (which recursively had 100s of thousands of files). Similarly, .cache-loader often has thousands of files.

Possibly useful note: it took me some time to realize that files.exclude and files.watcherExclude are two different settings.

Some theoretical opinion: Most answers here (and even in the official vscode documentation) suggest increasing the system watcher limits to a very large number. This works okay in most cases, however, it is like using a hammer in place of a screwdriver (is brute force, may not always work, and is not efficient). While the absolute system limit can (and perhaps should be) raised from the default limit, it is beneficial to optimize your workspace and avoid using unnecessary system resources.

6
  • 1
    Is this watcherExclude set per workspace or per User/globally?
    – Foton
    Apr 16, 2020 at 7:45
  • 1
    @Foton both should work. If you are collaborating with others and the soft link is part of the repository, better to put it in workspace config. If the link is restricted to your machine, you should put it in User config.
    – workwise
    Apr 17, 2020 at 7:48
  • 1
    This answer should have more upvotes. I've upped the watcher limits in the past, which is helpful, but recently added a symlink to a directory with a massive number of data files. The files.watcherExclude setting worked like a charm.
    – mdisibio
    Dec 5, 2020 at 1:31
  • This doesn't work for me, still get warning at startup. I can't do the other suggestions on a work computer due to permissions either...
    – Mastiff
    Jan 22, 2021 at 20:27
  • 1
    @terary In the official documentation code.visualstudio.com/docs/getstarted/settings files.exclude has entries like "**/.git": true, while watcherExclude has entries like "**/.git/objects/**": true, "**/node_modules/*/**": true etc. I am not sure if this is by design or just due to random examples in documentation. Will need to test (I realized that even I have been using a combination of settings with wildcards and without wildcards - perhaps that is why performance in my vscode has been slow as of late because some of them don't work as expected).
    – workwise
    Sep 27, 2021 at 4:47
5

Here is the solution : https://code.visualstudio.com/docs/setup/linux

The limit can be increased to its maximum by editing /etc/sysctl.conf and adding this line to the end of the file: fs.inotify.max_user_watches=524288

The new value can then be loaded in by running sudo sysctl -p

4

Oct. 2021: this is improved from VSCode 1.61 (Sept. 2021, see below)

VSCode 1.62 includes:

File watching changes

File watching in VS Code changes to a new library, thanks to the work of the Parcel team with their @parcel/watcher.
We will gradually roll out this change to all users in the upcoming weeks.
Linux users will be happy to learn that the files.watcherExclude now applies natively so that the number of file handles VS Code needs to open for file watching can be greatly reduced.

The existing files.legacyWatcher setting was changed to an enumeration with these values:

  • on - The new file watcher will never be used.
  • off - The new file watcher will always be used.
  • default - The new file watcher will only be used when you open multi-root workspaces (via .code-workspace file).

Feb. 2021: As noted in issue 40898, the issue persists for the multi-root workspace (see the last part, for VSCode 1.61 improvements)

Initial problem is that the npm install takes twice as long to install dependencies when VSCode is running.

I've figured out that this is because of file watching for the node_modules folder so I added it to files.watcherExclude.

I use the following combinations (but non of them seems to be working):

"files.watcherExclude": {
       "**/node_modules": true,
       "**/node_modules/**": true,
       "**/node_modules/*/**": true
   }

This comment points out a script from Dirk Feytons to see which inotify watches are actually being created to confirm whether my watcher excludes were being used or not.

/*
 * If you want to see which inotify watches are being created by an application then you can
 * use this simple piece of code, compile it to a shared library and LD_PRELOAD it when starting
 * the application. Keep an eye on syslog to see the list of watches.
 * **NOTE**: This only logs the watches, it won't actually create the watch and thus watching
 * for changes WON'T actually WORK!
 *
 * More details (adjust as needed for your environment/distribution):
 * - Save this file in e.g. $HOME/inotify.c
 * - Compile: gcc -shared -o inotify.so inotify.c
 * - Start monitoring syslog: tail -f -n 0 /var/log/syslog | tee $HOME/watches.log
 * - Run your application with: LD_PRELOAD=$HOME/inotify.so <application>
 */

#include <sys/inotify.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>

int inotify_add_watch(int fd, const char *pathname, uint32_t mask)
{
    syslog(LOG_USER | LOG_ERR, "********** [%u] inotify_add_watch for %s", getpid(), pathname);
    return 100000;
}

Update Sept. 2021, from Lionel Gatibelza:

I was able to fix the problem, I don't know the real cause and why Vscode performed this check for the error, but I did the known fix on the remote server and on my local machine:

  • Add this fs.inotify.max_user_watches=524288 to /etc/sysctl.conf
  • Reload the configuration: sudo sysctl -p
  • Close and reopen vscode After that I didn't have the error again!

And from issue 132483 from Benjamin Pasero (software engineer at Microsoft in Zurich):

Just to clarify, nsfw is used on Windows as well in multi-root workspaces.
It was our original intent 5y ago to replace all other watching solutions with nsfw so we enabled it only for multi-root scenarios, which target a lower user base (maybe 200.000 users) to get some testing and feedback.
The next step in this journey is to enable it for all users and remove the others.

PS: the only platform where nsfw is currently not enabled by default (unless you are in a multi-root workspace) is Linux because unfortunately nsfw does not support ignore patterns (our files.watcherExclude setting) and each folder in a hierarchy is counted as an opened file handle against the limited set of file handles in the OS, forcing users to increase that limit.
The plan is to implement that support in October and then also get Linux on board.


Sept. 2021, VSCode 1.61:

File watching changes

The file watcher used to detect changes to files and folders on disk changed to a library that can handle all of our supported platforms (Windows, Linux, macOS) and reduces the overhead of maintaining different watchers for different platforms.

We plan to enable this library by default on all platforms. This iteration we enabled it on Windows and macOS, while Linux is planned shortly after.

The new watcher should be faster on startup, resulting in fewer CPU cycles spent on large folders.

One downside of using the library is that the file watcher no longer automatically detects folders in a workspace that are symbolic links on macOS.
If you have such a setup, you can use the new files.watcherInclude setting to add symbolic link paths to include for file watching explicitly.

On the upside, you can use this new setting on Windows to explicitly include symbolic link folders inside the workspace - something that was not possible before on Windows.

You should not notice any difference in your day to day work, but if file watching is broken for you, please report an issue.
There is a setting files.legacyWatcher to enable the old watcher in case of problems.

1

What helped me was creating a separate workspace for the project i was working on. So if i'm working on something in /htdocs/project/ then instead of just opening that folder i create it as a workspace.

1

TL; DR;

Seems like a bug.

-----

It seems the warning is gone now.

Unfortunately I cannot reproduce the bug right now, but here are some steps.

  • Installed Php Intellisense extension
  • From linux terminal did git init
  • Added folder into workspace
  • Did some work, added, saved, commit and push from command palette
  • closed VSC
  • Opened VSC -> warning was shown.

At this moment i saw in htop that there was a process /usr/share/code with long arguments which included something with TypeScript that was using 100% of 1 CPU and around 1G RAM. Then

  • closes VSC
  • killed process
  • opened VSC

Now the warning is not showing anymore, also CPU is being used normally.

1

I followed the recommendations from https://stackoverflow.com/users/4270633/harsh-shah[harsh-shah][1]

Almost worked for me. I discovered I had resources in my project that were also being watched unnecessary.

VScode behavior seems to be watch everything, unless excluded.

If you find you have a couple hundred files in your project but have 32000 watchers - you may want to inventory what is in your project directory and exclude unnecessary directories.

0

For those using Pylance, setting "files.watcherExclude" may not suffice. The extension watches all paths reported by python sys.path.

source: https://github.com/microsoft/pylance-release/issues/2914#issuecomment-1154254992

Deactivating the extension is one, admittedly imperfect, solution if increasing max_user_watches is not an options.

0

If you are using for JavaScript development, there is a workaround:

Just disable this built-in extension: TypeScript and JavaScript Language Features

0
0

Raising the file limits of the system really only papers over the problem.

Are you really going to raise the watcher limit over the 100s of thousands that it already is at?

Instead, search in the repo for folders with a tone of files in them:

du --inodes -S | sort -rh | sed -n \
        '1,50{/^.\{71\}/s/^\(.\{30\}\).*\(.\{37\}\)$/\1...\2/;p}'

vscode should already exclude node_modules and .git by default, so it is probably other folders that you have, likely auto-generated files from some app (a compilation or directory target, etc).

This one ignores .git and node_modules directories:

du --inodes -S | grep -v "node_modules" | grep -v ".git" | sort -rh | sed -n \
    '1,50{/^.\{71\}/s/^\(.\{30\}\).*\(.\{37\}\)$/\1...\2/;p}'

You could also just remove recursively your node_modules if you are confident in running a fresh npm install. To remove:

find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +
0

I found that this problem is due to the directory level on which you open your file explorer. That is, when you open the file explorer, VS code auto-fills the directory to open with /home/yourUsername. If you accept this, then it only watches files in your home directory. However, if you change this to the root ( / ), then it will create file watches for everything in the entire OS, and devour your system resources.

Since you're not connecting remotely as root (right?), you can't access root-level directories through the file explorer anyway.

-1

If you are writing Javascript, then this is probably gonna work for this issue:

You have to access the side-bar extensions menu: Ctrl + Shift + X.
Then type: @builtin types and disable the extension: TypeScript and JavaScript language features.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.