Skip to content

Commit 70ec26f

Browse files
committed
Merge pull request #908 from daniel-chambers/fix-watcher-paths
Fixed WatchChanges not properly removing subdirectories from watch list
2 parents 651aeff + 0263949 commit 70ec26f

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

src/app/FakeLib/ChangeWatcher.fs

+14-9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ let private handleWatcherEvents (status : FileStatus) (onChange : FileChange ->
1919
Name = e.Name
2020
Status = status })
2121

22+
let private calcDirsToWatch fileIncludes =
23+
let dirsToWatch = fileIncludes.Includes |> Seq.map (fun file -> Globbing.getRoot fileIncludes.BaseDirectory file)
24+
25+
// remove subdirectories from watch list so that we don't get duplicate file watchers running
26+
dirsToWatch
27+
|> Seq.filter (fun d ->
28+
dirsToWatch
29+
|> Seq.exists (fun p -> d.StartsWith p && p <> d)
30+
|> not)
31+
|> Seq.toList
32+
2233
/// Watches the for changes in the matching files.
2334
/// Returns an IDisposable which allows to dispose all FileSystemWatchers.
2435
///
@@ -39,14 +50,8 @@ let private handleWatcherEvents (status : FileStatus) (onChange : FileChange ->
3950
/// )
4051
///
4152
let WatchChanges (onChange : FileChange seq -> unit) (fileIncludes : FileIncludes) =
42-
let dirsToWatch = fileIncludes.Includes |> Seq.map (fun file -> Globbing.getRoot fileIncludes.BaseDirectory file)
43-
44-
// remove subdirectories from watch list so that we don't get duplicate file watchers running
45-
let dirsToWatch =
46-
dirsToWatch |> Seq.filter (fun d ->
47-
dirsToWatch
48-
|> Seq.exists (fun p -> p.StartsWith d && p <> d)
49-
|> not)
53+
let dirsToWatch = fileIncludes |> calcDirsToWatch
54+
5055
tracefn "dirs to watch: %A" dirsToWatch
5156

5257
// we collect changes in a mutable ref cell and wait for a few milliseconds to
@@ -84,7 +89,7 @@ let WatchChanges (onChange : FileChange seq -> unit) (fileIncludes : FileInclude
8489
(timer:System.Timers.Timer).Start() )
8590

8691
let watchers =
87-
dirsToWatch |> List.ofSeq |> List.map (fun dir ->
92+
dirsToWatch |> List.map (fun dir ->
8893
tracefn "watching dir: %s" dir
8994

9095
let watcher = new FileSystemWatcher(FullName dir, "*.*")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Linq;
2+
using Fake;
3+
using Machine.Specifications;
4+
using Microsoft.FSharp.Collections;
5+
6+
namespace Test.FAKECore
7+
{
8+
public class when_calculating_directories_to_watch
9+
{
10+
It should_watch_multiple_directories =
11+
() =>
12+
{
13+
var includes = ListModule.OfArray(new[] { @"test1\bin\*.dll", @"test2\bin\*.dll", });
14+
var fileIncludes = new FileSystem.FileIncludes(@"C:\Project", includes, ListModule.Empty<string>());
15+
16+
var dirsToWatch = ChangeWatcher.calcDirsToWatch(fileIncludes);
17+
18+
dirsToWatch.Length.ShouldEqual(2);
19+
dirsToWatch.ShouldContain(Fake.Globbing.normalizePath(@"C:\Project\test1\bin"));
20+
dirsToWatch.ShouldContain(Fake.Globbing.normalizePath(@"C:\Project\test2\bin"));
21+
};
22+
23+
It should_only_take_the_most_root_path_when_multiple_directories_share_a_root =
24+
() =>
25+
{
26+
var includes = ListModule.OfArray(new[] { @"tests\**\test1\bin\*.dll", @"tests\test2\bin\*.dll", });
27+
var fileIncludes = new FileSystem.FileIncludes(@"C:\Project", includes, ListModule.Empty<string>());
28+
29+
var dirsToWatch = ChangeWatcher.calcDirsToWatch(fileIncludes);
30+
31+
dirsToWatch.Length.ShouldEqual(1);
32+
dirsToWatch.ShouldContain(Fake.Globbing.normalizePath(@"C:\Project\tests"));
33+
};
34+
}
35+
}

src/test/Test.FAKECore/Test.FAKECore.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
</ItemGroup>
6868
<ItemGroup>
6969
<Compile Include="AssemblySpecs.cs" />
70+
<Compile Include="ChangeWatcherSpecs.cs" />
7071
<Compile Include="FSIHelperSpecs.cs" />
7172
<Compile Include="CliSpecs.cs" />
7273
<Compile Include="FileHandling\CopyFileSpecs.cs" />

0 commit comments

Comments
 (0)