From 684ee87f20be9cdf0af3442fa4032780e4168da2 Mon Sep 17 00:00:00 2001 From: Victor Hurdugaci Date: Mon, 18 Apr 2016 16:57:52 -0700 Subject: [PATCH] Fix the prefix comparison --- .../Internal/FileWatcher/PollingFileWatcher.cs | 3 ++- .../Internal/Implementation/FileWatcher.cs | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.DotNet.Watcher.Core/Internal/FileWatcher/PollingFileWatcher.cs b/src/Microsoft.DotNet.Watcher.Core/Internal/FileWatcher/PollingFileWatcher.cs index 2bc335f..d1f2dac 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 388f7f5..e0dbcab 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