Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Dec 18, 2024
1 parent 5d67416 commit e469207
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
20 changes: 12 additions & 8 deletions src/BuiltInTools/dotnet-watch/HotReloadDotNetWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,7 @@ private bool AcceptChange(ChangedPath change)
return false;
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
PathUtilities.GetContainingDirectories(path).FirstOrDefault(IsHiddenWindowsPath) is { } containingHiddenDir)
if (PathUtilities.GetContainingDirectories(path).FirstOrDefault(IsHiddenDirectory) is { } containingHiddenDir)
{
Context.Reporter.Report(MessageDescriptor.IgnoringChangeInHiddenDirectory, containingHiddenDir, kind, path);
return false;
Expand All @@ -621,15 +620,20 @@ private bool AcceptChange(ChangedPath change)
return true;
}

private static bool IsHiddenWindowsPath(string path)
// Note: the device root directory on Windows has hidden attribute:
=> File.GetAttributes(path).HasFlag(FileAttributes.Hidden) && Path.GetDirectoryName(path) != null;

internal static string FormatTimestamp(DateTime time)
=> time.ToString("HH:mm:ss.fffffff");
// Directory name starts with '.' on Unix is considered hidden.
// Apply the same convention on Windows as well (instead of checking for hidden attribute).
// This is consistent with SDK rules for default item exclusions:
// https://github.com/dotnet/sdk/blob/124be385f90f2c305dde2b817cb470e4d11d2d6b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.DefaultItems.targets#L42
private static bool IsHiddenDirectory(string dir)
=> Path.GetFileName(dir).StartsWith('.');

private static IReadOnlySet<string> GetProjectOutputDirectories(ProjectGraph projectGraph)
{
// TODO: https://github.com/dotnet/sdk/issues/45539
// Consider evaluating DefaultItemExcludes and DefaultExcludesInProjectFolder msbuild properties using
// https://github.com/dotnet/msbuild/blob/37eb419ad2c986ac5530292e6ee08e962390249e/src/Build/Globbing/MSBuildGlob.cs
// to determine which directories should be excluded.

var projectOutputDirectories = new HashSet<string>(PathUtilities.OSSpecificPathComparer);

foreach (var projectNode in projectGraph.ProjectNodes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"dotnet-watch": {
"commandName": "Project",
"commandLineArgs": "--verbose /bl:DotnetRun.binlog",
"workingDirectory": "C:\\sdk1\\artifacts\\tmp\\Debug\\RenameDirecto---333D0BB9\\AppWithDeps",
"workingDirectory": "$(RepoRoot)src\\Assets\\TestProjects\\BlazorWasmWithLibrary\\blazorwasm",
"environmentVariables": {
"DOTNET_WATCH_DEBUG_SDK_DIRECTORY": "$(RepoRoot)artifacts\\bin\\redist\\$(Configuration)\\dotnet\\sdk\\$(Version)",
"DCP_IDE_REQUEST_TIMEOUT_SECONDS": "100000",
Expand Down
6 changes: 5 additions & 1 deletion test/dotnet-watch.Tests/HotReload/ApplyDeltaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,11 @@ public async Task Razor_Component_ScopedCssAndStaticAssets()
App.Process.ClearOutput();
}

[PlatformSpecificFact(TestPlatforms.Windows | TestPlatforms.OSX)]
/// <summary>
/// Currently only works on Windows.
/// Add TestPlatforms.OSX once https://github.com/dotnet/sdk/issues/45521 is fixed.
/// </summary>
[PlatformSpecificFact(TestPlatforms.Windows)]
public async Task MauiBlazor()
{
var testAsset = TestAssets.CopyTestAsset("WatchMauiBlazor")
Expand Down
46 changes: 22 additions & 24 deletions test/dotnet-watch.Tests/HotReload/RuntimeProcessLauncherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,40 +533,38 @@ public enum DirectoryKind
Obj,
}

[PlatformSpecificTheory(TestPlatforms.Windows)]
[CombinatorialData]
public Task IgnoredChange_Windows(bool isExisting, bool isIncluded, DirectoryKind directoryKind)
=> IgnoredChange(isExisting, isIncluded, directoryKind);

[PlatformSpecificTheory(TestPlatforms.AnyUnix)]
[Theory]
[CombinatorialData]
public Task IgnoredChange_Unix(
public async Task IgnoredChange(
bool isExisting,
bool isIncluded,
[CombinatorialValues(DirectoryKind.Obj, DirectoryKind.Bin, DirectoryKind.Ordinary)] DirectoryKind directoryKind)
=> IgnoredChange(isExisting, isIncluded, directoryKind);

private async Task IgnoredChange(bool isExisting, bool isIncluded, DirectoryKind directoryKind)
[CombinatorialValues] DirectoryKind directoryKind)
{
var testAsset = CopyTestAsset("WatchNoDepsApp", [isExisting, isIncluded, directoryKind]);

var workingDirectory = testAsset.Path;
string dir;

var objDir = Path.Combine(workingDirectory, "obj", "Debug", ToolsetInfo.CurrentTargetFramework);
var binDir = Path.Combine(workingDirectory, "bin", "Debug", ToolsetInfo.CurrentTargetFramework);
switch (directoryKind)
{
case DirectoryKind.Bin:
dir = Path.Combine(workingDirectory, "bin", "Debug", ToolsetInfo.CurrentTargetFramework);
break;

var hiddenDir = Path.Combine(workingDirectory, "hidden");
Directory.CreateDirectory(hiddenDir);
File.SetAttributes(hiddenDir, FileAttributes.Hidden | FileAttributes.Directory);
case DirectoryKind.Obj:
dir = Path.Combine(workingDirectory, "obj", "Debug", ToolsetInfo.CurrentTargetFramework);
break;

case DirectoryKind.Hidden:
dir = Path.Combine(workingDirectory, ".dir");
break;

default:
dir = workingDirectory;
break;
}

var extension = isIncluded ? ".cs" : ".txt";
var dir = directoryKind switch
{
DirectoryKind.Bin => binDir,
DirectoryKind.Obj => objDir,
DirectoryKind.Hidden => hiddenDir,
_ => workingDirectory,
};

Directory.CreateDirectory(dir);

Expand All @@ -576,7 +574,7 @@ private async Task IgnoredChange(bool isExisting, bool isIncluded, DirectoryKind
{
File.WriteAllText(path, "class C { int F() => 1; }");

if (isIncluded && directoryKind is DirectoryKind.Bin or DirectoryKind.Obj)
if (isIncluded && directoryKind is DirectoryKind.Bin or DirectoryKind.Obj or DirectoryKind.Hidden)
{
var project = Path.Combine(workingDirectory, "WatchNoDepsApp.csproj");
File.WriteAllText(project, File.ReadAllText(project).Replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,11 @@ public void Log(string message)
public void UpdateSourceFile(string path, string text)
{
WriteAllText(path, text);
Log($"File '{path}' updated ({HotReloadDotNetWatcher.FormatTimestamp(File.GetLastWriteTimeUtc(path))}).");
Log($"File '{path}' updated.");
}

public void UpdateSourceFile(string path, Func<string, string> contentTransform)
{
WriteAllText(path, contentTransform(File.ReadAllText(path, Encoding.UTF8)));
Log($"File '{path}' updated.");
}
=> UpdateSourceFile(path, contentTransform(File.ReadAllText(path, Encoding.UTF8)));

/// <summary>
/// Replacement for <see cref="File.WriteAllText"/>, which fails to write to hidden file
Expand Down

0 comments on commit e469207

Please sign in to comment.