Skip to content
This repository was archived by the owner on Nov 6, 2018. It is now read-only.

Commit 4f95e44

Browse files
author
Nate McMaster
committed
Add a quirks mode switch for issue dotnet/aspnetcore#2808
1 parent 1a7b10c commit 4f95e44

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/Microsoft.Extensions.FileProviders.Physical/PhysicalFilesWatcher.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ public class PhysicalFilesWatcher : IDisposable
3333
private readonly string _root;
3434
private readonly bool _pollForChanges;
3535

36+
/// <summary>
37+
/// If for some unforseen reason the patch for aspnet/Home#2808 causes issues for users,
38+
/// this gives them a way to disable the behavior change.
39+
/// They is enabled by setting <c>Switch.Microsoft.AspNetCore.FileProviders.IgnoreEmptyRenameEvents</c> to <c>true</c>
40+
/// in the app configuration.
41+
/// </summary>
42+
private readonly bool _emptyRenameEventQuirksMode;
43+
3644
/// <summary>
3745
/// Initializes an instance of <see cref="PhysicalFilesWatcher" /> that watches files in <paramref name="root" />.
3846
/// Wraps an instance of <see cref="System.IO.FileSystemWatcher" />
@@ -48,6 +56,7 @@ public PhysicalFilesWatcher(
4856
FileSystemWatcher fileSystemWatcher,
4957
bool pollForChanges)
5058
{
59+
AppContext.TryGetSwitch("Switch.Microsoft.AspNetCore.FileProviders.IgnoreEmptyRenameEvents", out _emptyRenameEventQuirksMode);
5160
_root = root;
5261
_fileWatcher = fileSystemWatcher;
5362
_fileWatcher.IncludeSubdirectories = true;
@@ -245,7 +254,7 @@ ex is SecurityException ||
245254

246255
private void ReportChangeForMatchedEntries(string path)
247256
{
248-
if (string.IsNullOrEmpty(path))
257+
if (!_emptyRenameEventQuirksMode && string.IsNullOrEmpty(path))
249258
{
250259
// System.IO.FileSystemWatcher may trigger events that are missing the file name,
251260
// which makes it appear as if the root directory is renamed or deleted. Moving the root directory
@@ -361,4 +370,4 @@ public ChangeTokenInfo(
361370
public Matcher Matcher { get; }
362371
}
363372
}
364-
}
373+
}

test/Microsoft.Extensions.FileProviders.Physical.Tests/PhysicalFilesWatcherTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.IO;
56
using System.Threading.Tasks;
67
using Xunit;
@@ -32,6 +33,19 @@ public void CreateFileChangeToken_DoesNotAllowPathsAboveRoot()
3233
[Fact]
3334
public async Task HandlesOnRenamedEventsThatMatchRootPath()
3435
{
36+
AppContext.SetSwitch("Switch.Microsoft.AspNetCore.FileProviders.IgnoreEmptyRenameEvents", true);
37+
using (var root = new DisposableFileSystem())
38+
using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
39+
using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
40+
{
41+
var token = physicalFilesWatcher.CreateFileChangeToken("**");
42+
token.RegisterChangeCallback(o => { }, null);
43+
44+
Assert.Throws<ArgumentException>(
45+
() => fileSystemWatcher.CallOnRenamed(new RenamedEventArgs(WatcherChangeTypes.Renamed, root.RootPath, string.Empty, string.Empty)));
46+
}
47+
48+
AppContext.SetSwitch("Switch.Microsoft.AspNetCore.FileProviders.IgnoreEmptyRenameEvents", false);
3549
using (var root = new DisposableFileSystem())
3650
using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
3751
using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))

0 commit comments

Comments
 (0)