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

Use AssemblyLoadContext Name instead of AppDomain #9353

Merged
Merged
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
16 changes: 12 additions & 4 deletions src/Build.UnitTests/BinaryLogger_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,13 @@ public void UnusedEnvironmentVariablesDoNotAppearInBinaryLog()
}
}

[Fact]
public void AssemblyLoadsDuringTaskRunLogged()
[WindowsFullFrameworkOnlyFact(additionalMessage: "Tests if the AppDomain used to load the task is included in the log text for the event, which is true only on Framework.")]
public void AssemblyLoadsDuringTaskRunLoggedWithAppDomain() => AssemblyLoadsDuringTaskRun("AppDomain: [Default]");

[DotNetOnlyFact(additionalMessage: "Tests if the AssemblyLoadContext used to load the task is included in the log text for the event, which is true only on Core.")]
public void AssemblyLoadsDuringTaskRunLoggedWithAssemblyLoadContext() => AssemblyLoadsDuringTaskRun("AssemblyLoadContext: Default");

private void AssemblyLoadsDuringTaskRun(string additionalEventText)
{
using (TestEnvironment env = TestEnvironment.Create())
{
Expand All @@ -201,7 +206,7 @@ public void AssemblyLoadsDuringTaskRunLogged()
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup />
<Task>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.Reflection"/>
Expand Down Expand Up @@ -235,17 +240,20 @@ public void AssemblyLoadsDuringTaskRunLogged()
"Assembly loaded during TaskRun (InlineCode.HelloWorld): System.Diagnostics.Debug";
string text = File.ReadAllText(Path.Combine(logFolder.Path, "logFile.log"));
text.ShouldContain(assemblyLoadedEventText);
text.ShouldContain(additionalEventText);
// events should not be in logger with verbosity normal
string text2 = File.ReadAllText(Path.Combine(logFolder.Path, "logFile2.log"));
text2.ShouldNotContain(assemblyLoadedEventText);

text2.ShouldNotContain(additionalEventText);
RunnerUtilities.ExecMSBuild($"{logger.Parameters} -flp1:logfile={Path.Combine(logFolder.Path, "logFile3.log")};verbosity=diagnostic -flp2:logfile={Path.Combine(logFolder.Path, "logFile4.log")};verbosity=normal", out success);
success.ShouldBeTrue();
text = File.ReadAllText(Path.Combine(logFolder.Path, "logFile3.log"));
text.ShouldContain(assemblyLoadedEventText);
text.ShouldContain(additionalEventText);
// events should not be in logger with verbosity normal
text2 = File.ReadAllText(Path.Combine(logFolder.Path, "logFile4.log"));
text2.ShouldNotContain(assemblyLoadedEventText);
text2.ShouldNotContain(additionalEventText);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using System.Linq;
#endif
using System.Reflection;
#if FEATURE_ASSEMBLYLOADCONTEXT
using System.Runtime.Loader;
#endif
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Framework;

Expand Down Expand Up @@ -152,10 +155,15 @@ private void CurrentDomainOnAssemblyLoad(object? sender, AssemblyLoadEventArgs a
string? assemblyName = args.LoadedAssembly.FullName;
string assemblyPath = args.LoadedAssembly.IsDynamic ? string.Empty : args.LoadedAssembly.Location;
Guid mvid = args.LoadedAssembly.ManifestModule.ModuleVersionId;
#if FEATURE_ASSEMBLYLOADCONTEXT
// AssemblyLoadContext.GetLoadContext returns null when the assembly isn't a RuntimeAssembly, which should not be the case here.
// Name would only be null if the AssemblyLoadContext didn't supply a name, but MSBuildLoadContext does.
string appDomainDescriptor = AssemblyLoadContext.GetLoadContext(args.LoadedAssembly)?.Name ?? "Unknown";
#else
string? appDomainDescriptor = _appDomain.IsDefaultAppDomain()
? null
: $"{_appDomain.Id}|{_appDomain.FriendlyName}";

#endif

AssemblyLoadBuildEventArgs buildArgs = new(_context, _initiator, assemblyName, assemblyPath, mvid, appDomainDescriptor);

Expand Down
3 changes: 3 additions & 0 deletions src/Build/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,9 @@ Utilization: {0} Average Utilization: {1:###.0}</value>
<data name="TaskAssemblyLoaded" xml:space="preserve">
<value>Assembly loaded during {0}{1}: {2} (location: {3}, MVID: {4}, AppDomain: {5})</value>
</data>
<data name="TaskAssemblyLoadedWithAssemblyLoadContext" xml:space="preserve">
<value>Assembly loaded during {0}{1}: {2} (location: {3}, MVID: {4}, AssemblyLoadContext: {5})</value>
</data>
<data name="NodeReused" xml:space="preserve">
<value>Reusing node {0} (PID: {1}).</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Build/Resources/xlf/Strings.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/Framework/AssemblyLoadBuildEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public AssemblyLoadBuildEventArgs(
public string? AssemblyName { get; private set; }
public string? AssemblyPath { get; private set; }
public Guid MVID { get; private set; }
// Null string indicates that load occurred on Default AppDomain (for both Core and Framework).
// Null string indicates that load occurred on Default AppDomain (for Framework).
// For Core, string won't be null.
public string? AppDomainDescriptor { get; private set; }

internal override void WriteToStream(BinaryWriter writer)
Expand Down Expand Up @@ -77,7 +78,12 @@ public override string Message
if (RawMessage == null)
{
string? loadingInitiator = LoadingInitiator == null ? null : $" ({LoadingInitiator})";
RawMessage = FormatResourceStringIgnoreCodeAndKeyword("TaskAssemblyLoaded", LoadingContext.ToString(), loadingInitiator, AssemblyName, AssemblyPath, MVID.ToString(), AppDomainDescriptor ?? DefaultAppDomainDescriptor);
#if FEATURE_ASSEMBLYLOADCONTEXT
string resourceName = "TaskAssemblyLoadedWithAssemblyLoadContext";
#else
string resourceName = "TaskAssemblyLoaded";
#endif
RawMessage = FormatResourceStringIgnoreCodeAndKeyword(resourceName, LoadingContext.ToString(), loadingInitiator, AssemblyName, AssemblyPath, MVID.ToString(), AppDomainDescriptor ?? DefaultAppDomainDescriptor);
}

return RawMessage;
Expand Down
16 changes: 16 additions & 0 deletions src/Shared/UnitTests/TypeLoader_Dependencies_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void LoadAssemblyAndDependency_InsideProjectFolder()
string dllPath = Path.Combine(dir.Path, TaskDllFileName);

CheckIfCorrectAssemblyLoaded(output, dllPath);
CheckIfCorrectAssemblyLoadedMessageLogged(output);
}
}

Expand All @@ -51,6 +52,7 @@ public void LoadAssemblyAndDependency_OutsideProjectFolder()
successfulExit.ShouldBeTrue(output);

CheckIfCorrectAssemblyLoaded(output, newTaskDllPath);
CheckIfCorrectAssemblyLoadedMessageLogged(output);
}
}

Expand Down Expand Up @@ -107,5 +109,19 @@ private void CheckIfCorrectAssemblyLoaded(string scriptOutput, string expectedAs
scriptOutput.ShouldNotContain(successfulMessage, Case.Insensitive);
}
}

private void CheckIfCorrectAssemblyLoadedMessageLogged(string scriptOutput)
{
var assemblyLoadedTaskRun = "Assembly loaded during TaskRun";

#if FEATURE_ASSEMBLYLOADCONTEXT
var message = "AssemblyLoadContext: MSBuild plugin";
#else
var message = "AppDomain: [Default]";
#endif

scriptOutput.ShouldContain(assemblyLoadedTaskRun);
scriptOutput.ShouldContain(message);
}
}
}