From 13d1bf8534751a4d694b89429513e3f4bf746eca Mon Sep 17 00:00:00 2001 From: Forgind <12969783+Forgind@users.noreply.github.com> Date: Wed, 17 Jan 2024 13:35:08 -0800 Subject: [PATCH 1/2] Include (file/assembly) version in deps.json --- .../DependencyContextBuilder.cs | 12 ++++++++---- .../Microsoft.NET.Build.Tasks/GenerateDepsFile.cs | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs b/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs index 1adef018d052..1a26305ef4c6 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs @@ -1,6 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System.Diagnostics; using Microsoft.Build.Framework; using Microsoft.Extensions.DependencyModel; using NuGet.Packaging; @@ -253,7 +254,7 @@ public DependencyContextBuilder WithPackagesThatWereFiltered(Dictionary !l.ExcludeFromRuntime && l.Type != "runtimepack")) { - var runtimeLibrary = GetRuntimeLibrary(library); + var runtimeLibrary = GetRuntimeLibrary(library, userRuntimeAssemblies); if (runtimeLibrary != null) { runtimeLibraries.Add(runtimeLibrary); @@ -505,7 +506,7 @@ private IEnumerable GetRuntimePackLibraries() }); } - private RuntimeLibrary GetRuntimeLibrary(DependencyLibrary library) + private RuntimeLibrary GetRuntimeLibrary(DependencyLibrary library, string[] userRuntimeAssemblies) { GetCommonLibraryProperties(library, out string hash, @@ -527,7 +528,10 @@ private RuntimeLibrary GetRuntimeLibrary(DependencyLibrary library) if (library.Type == "project" && !(referenceProjectInfo is UnreferencedProjectInfo)) { - runtimeAssemblyGroups.Add(new RuntimeAssetGroup(string.Empty, referenceProjectInfo.OutputName)); + var fileName = Path.GetFileNameWithoutExtension(library.Path); + var assemblyPath = userRuntimeAssemblies?.FirstOrDefault(p => Path.GetFileNameWithoutExtension(p).Equals(fileName)); + runtimeAssemblyGroups.Add(new RuntimeAssetGroup(string.Empty, + [ new RuntimeFile(referenceProjectInfo.OutputName, library.Version.ToString(), assemblyPath is null ? string.Empty : FileVersionInfo.GetVersionInfo(assemblyPath).FileVersion) ])); resourceAssemblies.AddRange(referenceProjectInfo.ResourceAssemblies .Select(r => new ResourceAssembly(r.RelativePath, r.Culture))); diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs b/src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs index 090e7d4514cb..0ca1f6f2c200 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs @@ -246,7 +246,7 @@ bool ShouldIncludeRuntimeAsset(ITaskItem item) .Concat(ResolvedRuntimeTargetsFiles.Select(f => new ResolvedFile(f, true))); builder = builder.WithResolvedNuGetFiles(resolvedNuGetFiles); - DependencyContext dependencyContext = builder.Build(); + DependencyContext dependencyContext = builder.Build(UserRuntimeAssemblies); var writer = new DependencyContextWriter(); using (var fileStream = File.Create(depsFilePath)) From 70e168674c954bb5c1520b09eb9888b250c1510f Mon Sep 17 00:00:00 2001 From: Forgind <12969783+Forgind@users.noreply.github.com> Date: Wed, 17 Jan 2024 15:01:04 -0800 Subject: [PATCH 2/2] Add File.Exists check This is only necessary for tests that pretend particular assemblies have been created without actually creating them --- .../Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs b/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs index 1a26305ef4c6..3a6579001523 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs @@ -531,7 +531,11 @@ private RuntimeLibrary GetRuntimeLibrary(DependencyLibrary library, string[] use var fileName = Path.GetFileNameWithoutExtension(library.Path); var assemblyPath = userRuntimeAssemblies?.FirstOrDefault(p => Path.GetFileNameWithoutExtension(p).Equals(fileName)); runtimeAssemblyGroups.Add(new RuntimeAssetGroup(string.Empty, - [ new RuntimeFile(referenceProjectInfo.OutputName, library.Version.ToString(), assemblyPath is null ? string.Empty : FileVersionInfo.GetVersionInfo(assemblyPath).FileVersion) ])); + [ new RuntimeFile( + referenceProjectInfo.OutputName, + library.Version.ToString(), + assemblyPath is null || !File.Exists(assemblyPath) ? string.Empty : FileVersionInfo.GetVersionInfo(assemblyPath).FileVersion) + ])); resourceAssemblies.AddRange(referenceProjectInfo.ResourceAssemblies .Select(r => new ResourceAssembly(r.RelativePath, r.Culture)));