# [BUG] fsmonitor (Linux): directory moved into working tree is not watched

1 messages from 2026-11-18 to 2026-11-18. Participants: John Mellor.
Thread: https://gitlist.dev/t/66872

## John Mellor, 2026-11-18 14:03

Subject: [BUG] fsmonitor (Linux): directory moved into working tree is not watched
Message-ID: <CAG_kaUYEjQhiPMatcPXr00q3oO77BM2Mu7KUZyP0aSGGwKTsJA@mail.gmail.com>
URL: https://gitlist.dev/e/CAG_kaUYEjQhiPMatcPXr00q3oO77BM2Mu7KUZyP0aSGGwKTsJA%40mail.gmail.com

```
What did you do before the bug happened? (Steps to reproduce your issue)

Whilst I had the built-in fsmonitor daemon enabled on Linux, I moved
a directory into the working tree from outside it (a plain rename on
the same filesystem), committed it, then edited one file directly
inside it and one in a subdirectory of the moved directory.

Steps to reproduce:

    git init fsm-repro && cd fsm-repro
    git config core.fsmonitor true
    git commit -q --allow-empty -m init
    git status --short          # starts the daemon
    mkdir -p ../fsm-outside/pkg/sub
    echo a >../fsm-outside/pkg/top.txt
    echo a >../fsm-outside/pkg/sub/f.txt
    mv ../fsm-outside/pkg pkg
    git add pkg && git commit -qm add
    echo b >>pkg/top.txt
    echo b >>pkg/sub/f.txt
    git status --short

What did you expect to happen? (Expected behavior)

I expected both edited files to be listed as modified:

     M pkg/sub/f.txt
     M pkg/top.txt

What happened instead? (Actual behavior)

Only the file in the subdirectory is listed:

     M pkg/sub/f.txt

`git -c core.fsmonitor=false status --short` lists both files.

What's different between what you expected and what actually happened?

Edits to files directly inside the moved directory are invisible to
status, diff, add -u, commit -a and stash, with no warning. It's not
a race: it happens every time, and later edits in that directory are
missed too, until the daemon is restarted.

checkout and rm don't see the edit either, so they can accidentally
discard it. Continuing from the steps above:

    git restore pkg/sub/f.txt   # discard the edit that git can see
    git checkout HEAD~          # should refuse: local modifications

This incorrectly succeeds, silently deleting pkg/top.txt along with
its uncommitted edit. Similarly, running `git rm pkg/top.txt` instead
of the checkout deletes it rather than refusing due to local
modifications.

Anything else you want to add:

Running the steps above in /tmp, GIT_TRACE_FSMONITOR shows the move
and the edit in pkg/sub/, but nothing for pkg/top.txt:

    inotify_event: '/tmp/fsm-repro/pkg', mask=0x40000080 IN_MOVED_TO|IN_ISDIR
    no matching cookie for rename to '/tmp/fsm-repro/pkg'
    inotify_event: '/tmp/fsm-repro/pkg/sub/f.txt', mask=0x00000002 IN_MODIFY

I think the cause is in compat/fsmonitor/fsm-listen-linux.c. On
IN_MOVED_TO|IN_ISDIR, process_event() calls rename_dir() and then
register_inotify(path). As the directory came from outside the working
tree, there is no matching IN_MOVED_FROM cookie, so rename_dir() takes
its "Directory was moved from outside the watch tree" branch, which
only logs "no matching cookie" and returns. register_inotify() then
adds watches for the subdirectories of path, but not for path itself
(whereas the IN_CREATE branch calls add_watch(path) first). So pkg/
is never watched while pkg/sub/ is. A possible fix might be to call
add_watch(path, data) in that branch of rename_dir()?

This looks related to, but separate from, Erik Cervin-Edin's report
about subdirectories of a directory renamed within the working tree:
https://lore.kernel.org/git/as4z1SskiPyrSKIa@vader/

I reproduced it on 2.56.1 and on next (720f3f593):

[System Info]
git version:
git version 2.56.1.831.g720f3f593
cpu: x86_64
built from commit: 720f3f59372f7249d3b84f824ca9f570b40301ab
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
rust: disabled
feature: fsmonitor--daemon
OpenSSL: OpenSSL 3.6.5 29 Sep 2026
zlib: 1.3.2
SHA-1: SHA1_DC
SHA-256: SHA256_BLK
default-ref-storage-format: files
default-hash: sha1
uname: Linux 7.1.6 x86_64 (Debian-based)
compiler info: gnuc: 15.2
libc info: glibc: 2.43
$SHELL (typically, interactive shell): /bin/bash


[Enabled Hooks]


```
