{"thread":{"id":"66872","subject":"[BUG] fsmonitor (Linux): directory moved into working tree is not watched","startedAt":"2026-11-18T14:03:43Z","lastAt":"2026-11-18T14:03:43Z","messageCount":1,"participants":["John Mellor"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"558709","messageId":"CAG_kaUYEjQhiPMatcPXr00q3oO77BM2Mu7KUZyP0aSGGwKTsJA@mail.gmail.com","threadId":"66872","inReplyTo":null,"subject":"[BUG] fsmonitor (Linux): directory moved into working tree is not watched","fromName":"John Mellor","fromEmail":"johnme@google.com","sentAt":"2026-11-18T14:03:43Z","receivedAt":"2026-11-18T14:03:43Z","isPatch":false,"sender":{"key":"johnme@google.com","avatar":null},"body":"What did you do before the bug happened? (Steps to reproduce your issue)\n\nWhilst I had the built-in fsmonitor daemon enabled on Linux, I moved\na directory into the working tree from outside it (a plain rename on\nthe same filesystem), committed it, then edited one file directly\ninside it and one in a subdirectory of the moved directory.\n\nSteps to reproduce:\n\n    git init fsm-repro && cd fsm-repro\n    git config core.fsmonitor true\n    git commit -q --allow-empty -m init\n    git status --short          # starts the daemon\n    mkdir -p ../fsm-outside/pkg/sub\n    echo a >../fsm-outside/pkg/top.txt\n    echo a >../fsm-outside/pkg/sub/f.txt\n    mv ../fsm-outside/pkg pkg\n    git add pkg && git commit -qm add\n    echo b >>pkg/top.txt\n    echo b >>pkg/sub/f.txt\n    git status --short\n\nWhat did you expect to happen? (Expected behavior)\n\nI expected both edited files to be listed as modified:\n\n     M pkg/sub/f.txt\n     M pkg/top.txt\n\nWhat happened instead? (Actual behavior)\n\nOnly the file in the subdirectory is listed:\n\n     M pkg/sub/f.txt\n\n`git -c core.fsmonitor=false status --short` lists both files.\n\nWhat's different between what you expected and what actually happened?\n\nEdits to files directly inside the moved directory are invisible to\nstatus, diff, add -u, commit -a and stash, with no warning. It's not\na race: it happens every time, and later edits in that directory are\nmissed too, until the daemon is restarted.\n\ncheckout and rm don't see the edit either, so they can accidentally\ndiscard it. Continuing from the steps above:\n\n    git restore pkg/sub/f.txt   # discard the edit that git can see\n    git checkout HEAD~          # should refuse: local modifications\n\nThis incorrectly succeeds, silently deleting pkg/top.txt along with\nits uncommitted edit. Similarly, running `git rm pkg/top.txt` instead\nof the checkout deletes it rather than refusing due to local\nmodifications.\n\nAnything else you want to add:\n\nRunning the steps above in /tmp, GIT_TRACE_FSMONITOR shows the move\nand the edit in pkg/sub/, but nothing for pkg/top.txt:\n\n    inotify_event: '/tmp/fsm-repro/pkg', mask=0x40000080 IN_MOVED_TO|IN_ISDIR\n    no matching cookie for rename to '/tmp/fsm-repro/pkg'\n    inotify_event: '/tmp/fsm-repro/pkg/sub/f.txt', mask=0x00000002 IN_MODIFY\n\nI think the cause is in compat/fsmonitor/fsm-listen-linux.c. On\nIN_MOVED_TO|IN_ISDIR, process_event() calls rename_dir() and then\nregister_inotify(path). As the directory came from outside the working\ntree, there is no matching IN_MOVED_FROM cookie, so rename_dir() takes\nits \"Directory was moved from outside the watch tree\" branch, which\nonly logs \"no matching cookie\" and returns. register_inotify() then\nadds watches for the subdirectories of path, but not for path itself\n(whereas the IN_CREATE branch calls add_watch(path) first). So pkg/\nis never watched while pkg/sub/ is. A possible fix might be to call\nadd_watch(path, data) in that branch of rename_dir()?\n\nThis looks related to, but separate from, Erik Cervin-Edin's report\nabout subdirectories of a directory renamed within the working tree:\nhttps://lore.kernel.org/git/as4z1SskiPyrSKIa@vader/\n\nI reproduced it on 2.56.1 and on next (720f3f593):\n\n[System Info]\ngit version:\ngit version 2.56.1.831.g720f3f593\ncpu: x86_64\nbuilt from commit: 720f3f59372f7249d3b84f824ca9f570b40301ab\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nrust: disabled\nfeature: fsmonitor--daemon\nOpenSSL: OpenSSL 3.6.5 29 Sep 2026\nzlib: 1.3.2\nSHA-1: SHA1_DC\nSHA-256: SHA256_BLK\ndefault-ref-storage-format: files\ndefault-hash: sha1\nuname: Linux 7.1.6 x86_64 (Debian-based)\ncompiler info: gnuc: 15.2\nlibc info: glibc: 2.43\n$SHELL (typically, interactive shell): /bin/bash\n\n\n[Enabled Hooks]\n\n"}]}