diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs index 96bbb6597e42aa..ed4f1639d07a73 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs @@ -390,7 +390,12 @@ private void AddDirectoryWatchUnlocked(WatchedDirectory? parent, string director // of the world, but there's little that can be done about that.) if (directoryEntry.Parent != parent) { - directoryEntry.Parent?.Children!.Remove (directoryEntry); + // Work around https://github.com/dotnet/csharplang/issues/3393 preventing Parent?.Children!. from behaving as expected + if (directoryEntry.Parent != null) + { + directoryEntry.Parent.Children!.Remove(directoryEntry); + } + directoryEntry.Parent = parent; if (parent != null) { @@ -443,7 +448,12 @@ private void RemoveWatchedDirectory(WatchedDirectory directoryEntry, bool remove Debug.Assert (_includeSubdirectories); lock (SyncObj) { - directoryEntry.Parent?.Children!.Remove(directoryEntry); + // Work around https://github.com/dotnet/csharplang/issues/3393 preventing Parent?.Children!. from behaving as expected + if (directoryEntry.Parent != null) + { + directoryEntry.Parent.Children!.Remove(directoryEntry); + } + RemoveWatchedDirectoryUnlocked (directoryEntry, removeInotify); } }