Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't fail metadata updates on missing assemblies #40725

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions src/BuiltInTools/DotNetDeltaApplier/HotReloadAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,36 @@ private UpdateHandlerActions GetMetadataUpdateHandlerActions()
var handlerActions = new UpdateHandlerActions();
foreach (var assembly in sortedAssemblies)
{
foreach (var attr in assembly.GetCustomAttributesData())
try
{
// Look up the attribute by name rather than by type. This would allow netstandard targeting libraries to
// define their own copy without having to cross-compile.
if (attr.AttributeType.FullName != "System.Reflection.Metadata.MetadataUpdateHandlerAttribute")
foreach (var attr in assembly.GetCustomAttributesData())
{
continue;
// Look up the attribute by name rather than by type. This would allow netstandard targeting libraries to
// define their own copy without having to cross-compile.
if (attr.AttributeType.FullName != "System.Reflection.Metadata.MetadataUpdateHandlerAttribute")
{
continue;
}

IList<CustomAttributeTypedArgument> ctorArgs = attr.ConstructorArguments;
if (ctorArgs.Count != 1 ||
ctorArgs[0].Value is not Type handlerType)
{
_log($"'{attr}' found with invalid arguments.");
continue;
}

GetHandlerActions(handlerActions, handlerType);
}

IList<CustomAttributeTypedArgument> ctorArgs = attr.ConstructorArguments;
if (ctorArgs.Count != 1 ||
ctorArgs[0].Value is not Type handlerType)
{
_log($"'{attr}' found with invalid arguments.");
continue;
}

GetHandlerActions(handlerActions, handlerType);
}
catch (Exception e)
{
// In cross-platform scenarios, such as debugging in VS through WSL, Roslyn
// runs on Windows, and the agent runs on Linux. Assemblies accessible to Windows
// may not be available or loaded on linux (such as WPF's assemblies).
// In such case, we can ignore the assemblies and continue enumerating handlers for
// the rest of the assemblies of current domain.
_log($"'{assembly.FullName}' is not loaded ({e.Message})");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>$(CurrentTargetFramework)</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Dep\Dep.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Diagnostics;
using System.Reflection.Metadata;

[assembly: MetadataUpdateHandler(typeof(UpdateHandler))]
[assembly: MetadataUpdateHandler(typeof(Dep.UpdateHandler))]

// delete the dependency dll to cause load failure of DepSubType
var depPath = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location!)!, "Dep.dll");
File.Delete(depPath);
Console.WriteLine($"File deleted: {depPath}");

while (true)
{
lock (UpdateHandler.Guard)
{
Printer.Print();
}

Thread.Sleep(100);
}

static class UpdateHandler
{
// Lock to avoid the updated Print method executing concurrently with the update handler.
public static object Guard = new object();

public static void UpdateApplication(Type[] types)
{
lock (Guard)
{
Console.WriteLine($"Updated types: {(types == null ? "<null>" : types.Length == 0 ? "<empty>" : string.Join(",", types.Select(t => t.Name)))}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

class DepSubType : Dep.DepType
{
int F() => 1;
}

class Printer
{
public static void Print()
=> Console.WriteLine("Hello!");
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Dep;

public class DepType
{
void F()
{
Console.WriteLine(1);
}
}

public static class UpdateHandler
{
// Lock to avoid the updated Print method executing concurrently with the update handler.
public static object Guard = new object();

public static void UpdateApplication(Type[] types)
{
lock (Guard)
{
Console.WriteLine($"Dep Updated types: {(types == null ? "<null>" : types.Length == 0 ? "<empty>" : string.Join(",", types.Select(t => t.Name)))}");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(CurrentTargetFramework)</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

</Project>
51 changes: 51 additions & 0 deletions test/dotnet-watch.Tests/HotReload/ApplyDeltaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,56 @@ public async Task BlazorWasm()
//UpdateSourceFile(Path.Combine(testAsset.Path, "Pages", "Index.razor"), newSource);
//await App.AssertOutputLineStartsWith(MessageDescriptor.HotReloadSucceeded);
}

// Test is timing out on .NET Framework: https://github.com/dotnet/sdk/issues/41669
[CoreMSBuildOnlyFact(Skip = "https://github.com/dotnet/sdk/issues/42850")]
public async Task HandleMissingAssemblyFailure()
{
var testAsset = TestAssets.CopyTestAsset("WatchAppMissingAssemblyFailure")
.WithSource();

App.Start(testAsset, [], "App");

await App.AssertWaitingForChanges();

var newSrc = /* lang=c#-test */"""
class DepSubType : Dep.DepType
{
int F() => 2;
}

class Printer
{
public static void Print()
{
Console.WriteLine("Changed!");
}
}

public static class UpdateHandler
{
// Lock to avoid the updated Print method executing concurrently with the update handler.
public static object Guard = new object();

public static void UpdateApplication(Type[] types)
{
lock (Guard)
{
Console.WriteLine($"Dep Updated types: {(types == null ? "<null>" : types.Length == 0 ? "<empty>" : string.Join(",", types.Select(t => t.Name)))}");
}
}
}
""";

// Delete all files in testAsset.Path named Dep.dll
foreach (var depDll in Directory.GetFiles(testAsset.Path, "Dep.dll", SearchOption.AllDirectories))
{
File.Delete(depDll);
}

File.WriteAllText(Path.Combine(testAsset.Path, "App", "Update.cs"), newSrc);

await App.AssertOutputLineStartsWith("Updated types: Printer");
}
}
}