diff --git a/src/Microsoft.DotNet.Watcher.Core/Internal/FileWatcher/PollingFileWatcher.cs b/src/Microsoft.DotNet.Watcher.Core/Internal/FileWatcher/PollingFileWatcher.cs index 2bc335f76a..d1f2dac231 100644 --- a/src/Microsoft.DotNet.Watcher.Core/Internal/FileWatcher/PollingFileWatcher.cs +++ b/src/Microsoft.DotNet.Watcher.Core/Internal/FileWatcher/PollingFileWatcher.cs @@ -150,7 +150,8 @@ namespace Microsoft.DotNet.Watcher.Core.Internal private void RecordChange(FileSystemInfo fileInfo) { - if (_changes.Contains(fileInfo.FullName) || + if (fileInfo == null || + _changes.Contains(fileInfo.FullName) || fileInfo.FullName.Equals(_watchedDirectory.FullName, StringComparison.Ordinal)) { return; diff --git a/src/Microsoft.DotNet.Watcher.Core/Internal/Implementation/FileWatcher.cs b/src/Microsoft.DotNet.Watcher.Core/Internal/Implementation/FileWatcher.cs index 388f7f52ab..e0dbcabed5 100644 --- a/src/Microsoft.DotNet.Watcher.Core/Internal/Implementation/FileWatcher.cs +++ b/src/Microsoft.DotNet.Watcher.Core/Internal/Implementation/FileWatcher.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; namespace Microsoft.DotNet.Watcher.Core.Internal @@ -39,6 +40,8 @@ namespace Microsoft.DotNet.Watcher.Core.Internal private void AddDirectoryWatcher(string directory) { + directory = EnsureTrailingSlash(directory); + var alreadyWatched = _watchers .Where(d => directory.StartsWith(d.Key)) .Any(); @@ -99,5 +102,16 @@ namespace Microsoft.DotNet.Watcher.Core.Internal throw new ObjectDisposedException(nameof(FileWatcher)); } } + + private static string EnsureTrailingSlash(string path) + { + if (!string.IsNullOrEmpty(path) && + path[path.Length - 1] != Path.DirectorySeparatorChar) + { + return path + Path.DirectorySeparatorChar; + } + + return path; + } } } \ No newline at end of file