0

If I watch a directory that has a .git inside of it and run fs.watch, does that listen to changes within .git folder?

For example if I use:

 fs.watch(dir, { recursive: true }
2
  • 1
    Did you try it? What happened? Commented Nov 11 at 11:42
  • Yes, it emits events for changes in hidden folders such as .git, .vscode, and others starting with a dot. I guess fs.watch just treats them as regular folders. Commented Nov 11 at 19:24

1 Answer 1

1

Yes fs watch listens to hidden folders.

I was running custom logic in a file system watcher with recursive: true. Since my watcher also observed the .git folder, running Git commands inside the watcher created a feedback loop: the Git commands modified the .git folder, which triggered the watcher again, and so on.

Fix: Ignore changes in the .git folder:

const watcher = fs.watch(dir, { recursive: true }, (eventType, filename) => {
  if (filename && (filename.startsWith(".git") || filename.includes(`${path.sep}.git`))) {
    console.log("Git folder changed, ignoring...");
    return;
  }

  throttledNotify();
});

Somewhere in throttledNotify i was running const stdout = await runGitCommand(["status", "--porcelain"], dir); which causes the infinite feedback loop

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.