From a2a1e3c079aaaea13182dbe04b2395e1be0243a8 Mon Sep 17 00:00:00 2001 From: David Federman Date: Mon, 30 Sep 2024 21:11:23 -0700 Subject: [PATCH] (chore) Upgrade packages (#103) --- Directory.Build.props | 8 +- ReferenceTrimmer.sln | 1 + global.json | 6 ++ src/Loggers/MSVC/ForwardingLogger.cs | 11 +-- src/Loggers/ReferenceTrimmer.Loggers.csproj | 2 +- src/Package/ReferenceTrimmer.Package.csproj | 2 +- src/Tasks/CollectDeclaredReferencesTask.cs | 20 ++++- src/Tasks/ReferenceTrimmer.Tasks.csproj | 4 +- src/Tests/E2ETests.cs | 88 ++++++++++----------- src/Tests/MsvcLoggerTests.cs | 3 +- src/Tests/ReferenceTrimmer.Tests.csproj | 9 +-- 11 files changed, 88 insertions(+), 66 deletions(-) create mode 100644 global.json diff --git a/Directory.Build.props b/Directory.Build.props index e784e12..349fd61 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,4 +1,4 @@ - + embedded + + + false diff --git a/ReferenceTrimmer.sln b/ReferenceTrimmer.sln index 1227437..39728ac 100644 --- a/ReferenceTrimmer.sln +++ b/ReferenceTrimmer.sln @@ -9,6 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution .gitignore = .gitignore Directory.Build.props = Directory.Build.props Directory.Build.rsp = Directory.Build.rsp + global.json = global.json LICENSE = LICENSE NuGet.Config = NuGet.Config README.md = README.md diff --git a/global.json b/global.json new file mode 100644 index 0000000..9825ffb --- /dev/null +++ b/global.json @@ -0,0 +1,6 @@ +{ + "msbuild-sdks": { + "Microsoft.Build.NoTargets": "3.7.56", + "MSTest.Sdk": "3.6.0" + } +} \ No newline at end of file diff --git a/src/Loggers/MSVC/ForwardingLogger.cs b/src/Loggers/MSVC/ForwardingLogger.cs index 9b30f8a..718edad 100644 --- a/src/Loggers/MSVC/ForwardingLogger.cs +++ b/src/Loggers/MSVC/ForwardingLogger.cs @@ -229,13 +229,14 @@ private static string EscapeJsonChars(string str) private void OnMessageRaised(object sender, BuildMessageEventArgs e) { - if (string.IsNullOrEmpty(e.ProjectFile)) + string? projectFilePath = e.ProjectFile; + string? message = e.Message; + + if (string.IsNullOrEmpty(projectFilePath) || message is null) { return; } - string projectFilePath = e.ProjectFile; - if (!_projects.TryGetValue(projectFilePath, out ProjectStateLibs? projState)) { return; @@ -246,14 +247,14 @@ private void OnMessageRaised(object sender, BuildMessageEventArgs e) switch (projState.ProjectState) { case State.LinkStarted: - if (e.Message.IndexOf("Unused libraries:", StringComparison.OrdinalIgnoreCase) != -1) + if (message!.IndexOf("Unused libraries:", StringComparison.OrdinalIgnoreCase) != -1) { projState.ProjectState = State.UnusedLibsStarted; } break; case State.UnusedLibsStarted: - string lib = e.Message.Trim(); + string lib = message!.Trim(); if (lib.Length > 0) { try diff --git a/src/Loggers/ReferenceTrimmer.Loggers.csproj b/src/Loggers/ReferenceTrimmer.Loggers.csproj index 4e5f37c..f222034 100644 --- a/src/Loggers/ReferenceTrimmer.Loggers.csproj +++ b/src/Loggers/ReferenceTrimmer.Loggers.csproj @@ -5,7 +5,7 @@ - + diff --git a/src/Package/ReferenceTrimmer.Package.csproj b/src/Package/ReferenceTrimmer.Package.csproj index 5908f75..c954ce4 100644 --- a/src/Package/ReferenceTrimmer.Package.csproj +++ b/src/Package/ReferenceTrimmer.Package.csproj @@ -1,4 +1,4 @@ - + netstandard2.0 true diff --git a/src/Tasks/CollectDeclaredReferencesTask.cs b/src/Tasks/CollectDeclaredReferencesTask.cs index 851ba4e..247acc5 100644 --- a/src/Tasks/CollectDeclaredReferencesTask.cs +++ b/src/Tasks/CollectDeclaredReferencesTask.cs @@ -202,8 +202,12 @@ private Dictionary GetPackageInfos() var lockFile = LockFileUtilities.GetLockFile(ProjectAssetsFile, NullLogger.Instance); var packageFolders = lockFile.PackageFolders.Select(item => item.Path).ToList(); - var nugetFramework = NuGetFramework.ParseComponents(TargetFrameworkMoniker, TargetPlatformMoniker); - LockFileTarget? nugetTarget = lockFile.GetTarget(nugetFramework, RuntimeIdentifier); + LockFileTarget? nugetTarget = null; + if (!string.IsNullOrEmpty(TargetFrameworkMoniker)) + { + var nugetFramework = NuGetFramework.ParseComponents(TargetFrameworkMoniker!, TargetPlatformMoniker); + nugetTarget = lockFile.GetTarget(nugetFramework, RuntimeIdentifier); + } List nugetLibraries; if (nugetTarget?.Libraries is not null) @@ -222,7 +226,12 @@ private Dictionary GetPackageInfos() var nugetDependents = new Dictionary>(StringComparer.OrdinalIgnoreCase); foreach (LockFileTargetLibrary nugetLibrary in nugetLibraries) { - var packageId = nugetLibrary.Name; + string? packageId = nugetLibrary.Name; + if (packageId is null) + { + continue; + } + foreach (var dependency in nugetLibrary.Dependencies) { if (!nugetDependents.TryGetValue(dependency.Id, out var parents)) @@ -238,6 +247,11 @@ private Dictionary GetPackageInfos() // Get the transitive closure of assemblies included by each package foreach (LockFileTargetLibrary nugetLibrary in nugetLibraries) { + if (nugetLibrary.Name is null) + { + continue; + } + string nugetLibraryRelativePath = lockFile.GetLibrary(nugetLibrary.Name, nugetLibrary.Version).Path; string nugetLibraryAbsolutePath = packageFolders .Select(packageFolder => Path.Combine(packageFolder, nugetLibraryRelativePath)) diff --git a/src/Tasks/ReferenceTrimmer.Tasks.csproj b/src/Tasks/ReferenceTrimmer.Tasks.csproj index d348665..3c56450 100644 --- a/src/Tasks/ReferenceTrimmer.Tasks.csproj +++ b/src/Tasks/ReferenceTrimmer.Tasks.csproj @@ -3,8 +3,8 @@ netstandard2.0 - - + + diff --git a/src/Tests/E2ETests.cs b/src/Tests/E2ETests.cs index b7ec31f..7107775 100644 --- a/src/Tests/E2ETests.cs +++ b/src/Tests/E2ETests.cs @@ -1,7 +1,6 @@ using System.Diagnostics; using System.Runtime.InteropServices; using System.Text.RegularExpressions; -using Microsoft.VisualStudio.TestTools.UnitTesting; using ReferenceTrimmer.Loggers.MSVC; namespace ReferenceTrimmer.Tests; @@ -34,7 +33,7 @@ public Task UsedProjectReference() { return RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -42,7 +41,7 @@ public Task UsedProjectReferenceProduceReferenceAssembly() { return RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -50,7 +49,7 @@ public Task UsedProjectReferenceNoReferenceAssembly() { return RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -94,7 +93,7 @@ public Task UnusedProjectReferenceNoWarn() { return RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -102,7 +101,7 @@ public Task UnusedProjectReferenceSuppressed() { return RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -136,11 +135,11 @@ public async Task UsedReferenceHintPath() // For direct references, MSBuild can't determine build order so we need to ensure the dependency is already built await RunMSBuildAsync( projectFile: "Dependency/Dependency.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); await RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -149,11 +148,11 @@ public async Task UsedReferenceItemSpec() // For direct references, MSBuild can't determine build order so we need to ensure the dependency is already built await RunMSBuildAsync( projectFile: "Dependency/Dependency.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); await RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -162,7 +161,7 @@ public async Task UnusedReferenceHintPath() // For direct references, MSBuild can't determine build order so we need to ensure the dependency is already built await RunMSBuildAsync( projectFile: "Dependency/Dependency.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); await RunMSBuildAsync( projectFile: "Library/Library.csproj", @@ -178,11 +177,11 @@ public async Task UnusedReferenceHintPathNoWarn() // For direct references, MSBuild can't determine build order so we need to ensure the dependency is already built await RunMSBuildAsync( projectFile: "Dependency/Dependency.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); await RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -191,7 +190,7 @@ public async Task UnusedReferenceItemSpec() // For direct references, MSBuild can't determine build order so we need to ensure the dependency is already built await RunMSBuildAsync( projectFile: "Dependency/Dependency.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); await RunMSBuildAsync( projectFile: "Library/Library.csproj", @@ -202,12 +201,11 @@ await RunMSBuildAsync( ? @"RT0001: Reference ..\Dependency\bin\x64\Debug\net472\\Dependency.dll can be removed" : @"RT0001: Reference ../Dependency/bin/x64/Debug/net472/Dependency.dll can be removed", "Library/Library.csproj", - new[] - { + [ // Alt: Can leave out 'x64' path segment on VS or Linux. @"RT0001: Reference ..\Dependency\bin\Debug\net472\\Dependency.dll can be removed", @"RT0001: Reference ../Dependency/bin/Debug/net472/Dependency.dll can be removed", - }), + ]), }); } @@ -217,11 +215,11 @@ public async Task UnusedReferenceItemSpecNoWarn() // For direct references, MSBuild can't determine build order so we need to ensure the dependency is already built await RunMSBuildAsync( projectFile: "Dependency/Dependency.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); await RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -252,7 +250,7 @@ public async Task UsedReferenceFromGac() await RunMSBuildAsync( projectFile: "Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -260,7 +258,7 @@ public Task UsedPackageReference() { return RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -268,7 +266,7 @@ public Task UsedIndirectPackageReference() { return RunMSBuildAsync( projectFile: "WebHost/WebHost.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -287,7 +285,7 @@ public Task UnusedPackageReferenceNoWarn() { return RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -306,7 +304,7 @@ public Task BuildPackageReference() { return RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -314,7 +312,7 @@ public Task MissingReferenceSourceTarget() { return RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -334,7 +332,7 @@ public Task NoTargets() { return RunMSBuildAsync( projectFile: "Project.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -342,7 +340,7 @@ public Task TargetFrameworkWithOs() { return RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -350,7 +348,7 @@ public Task AbsoluteIntermediateOutputPath() { return RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -358,7 +356,7 @@ public Task BuildExtensions() { return RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -366,7 +364,7 @@ public Task ReferenceInPackage() { return RunMSBuildAsync( projectFile: "Tests/Tests.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -374,7 +372,7 @@ public Task ReferenceTrimmerDisabled() { return RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -388,7 +386,7 @@ public async Task LegacyStyleProject() await RunMSBuildAsync( projectFile: "Library/Library.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } [TestMethod] @@ -402,16 +400,16 @@ public async Task UnusedWinSdkImportLibrary() await RunMSBuildAsync( projectFile: "App/App.vcxproj", - expectedWarnings: Array.Empty(), - expectedConsoleOutputs: new[] - { + expectedWarnings: [], + expectedConsoleOutputs: + [ "Unused libraries:", // Ensure link.exe unused lib flags are active @"\user32.lib", // Tail of variable unused lib paths like "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\user32.lib" "Unused MSVC libraries detected in project", " * Default Windows SDK import libraries:", " - Libraries needed: ", " - Unneeded: ", - }, + ], expectUnusedMsvcLibrariesLog: true); } @@ -426,14 +424,14 @@ public async Task UnusedCppLibrary() await RunMSBuildAsync( projectFile: "App/App.vcxproj", - expectedWarnings: Array.Empty(), - expectedConsoleOutputs: new[] - { + expectedWarnings: [], + expectedConsoleOutputs: + [ "Unused libraries:", // Ensure link.exe unused lib flags are active "Unused MSVC libraries detected in project", " * Other libraries - ", @"\Library.lib", // Tail of variable unused lib paths like "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\user32.lib" - }, + ], expectUnusedMsvcLibrariesLog: true); } @@ -448,14 +446,14 @@ public async Task UnusedCppDelayLoadLibrary() await RunMSBuildAsync( projectFile: "App/App.vcxproj", - expectedWarnings: Array.Empty(), - expectedConsoleOutputs: new[] - { + expectedWarnings: [], + expectedConsoleOutputs: + [ "Unused libraries:", // Ensure link.exe unused lib flags are active "Unused MSVC libraries detected in project", " * Other libraries - ", @"\DLL.lib", // Tail of variable unused lib paths like "C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x86\user32.lib" - }, + ], expectUnusedMsvcLibrariesLog: true); } @@ -464,7 +462,7 @@ public Task WpfApp() { return RunMSBuildAsync( projectFile: "WpfApp/WpfApp.csproj", - expectedWarnings: Array.Empty()); + expectedWarnings: []); } private static (string ExePath, string Verb) GetMsBuildExeAndVerb() diff --git a/src/Tests/MsvcLoggerTests.cs b/src/Tests/MsvcLoggerTests.cs index 4ac3648..e8ac3fd 100644 --- a/src/Tests/MsvcLoggerTests.cs +++ b/src/Tests/MsvcLoggerTests.cs @@ -1,5 +1,4 @@ using Microsoft.Build.Framework; -using Microsoft.VisualStudio.TestTools.UnitTesting; using ReferenceTrimmer.Loggers.MSVC; #pragma warning disable CA1707 // Underscores in test names @@ -184,7 +183,7 @@ public void ForwardingLogger_ForwardsUnusedLibs() var unusedLibArgs = eventRedirector.Events[0] as UnusedLibsCustomBuildEventArgs; Assert.IsNotNull(unusedLibArgs); Assert.AreEqual("a.proj", unusedLibArgs.ProjectPath); - Assert.IsTrue(unusedLibArgs.Message.Contains("user32.lib", StringComparison.Ordinal), unusedLibArgs.Message); + Assert.IsTrue(unusedLibArgs.Message!.Contains("user32.lib", StringComparison.Ordinal), unusedLibArgs.Message); Assert.IsTrue(unusedLibArgs.Message.Contains("bar.lib", StringComparison.Ordinal), unusedLibArgs.Message); Assert.IsTrue(unusedLibArgs.UnusedLibraryPathsJson.Length > 0); } diff --git a/src/Tests/ReferenceTrimmer.Tests.csproj b/src/Tests/ReferenceTrimmer.Tests.csproj index 9dae2d5..4bb2618 100644 --- a/src/Tests/ReferenceTrimmer.Tests.csproj +++ b/src/Tests/ReferenceTrimmer.Tests.csproj @@ -1,16 +1,13 @@ - + - net6.0 + net8.0 x64 AnyCPU;x64 - 0067;1591 + $(NoWarn);0067;1591;CA1861 $(DefaultItemExcludes);TestData/**;TestResults/** - - - false