From 2b561d5a801cb5d2ada5aa4f3430599cbad14f04 Mon Sep 17 00:00:00 2001 From: William Rall Date: Fri, 16 Sep 2022 15:29:56 -0700 Subject: [PATCH 1/8] Initial change to try to get project references that are not a part of the restore to be included in deps.json --- .../GivenADependencyContextBuilder.cs | 9 ++--- .../DependencyContextBuilder.cs | 4 +-- .../GenerateDepsFile.cs | 34 +++++++++---------- .../ReferenceInfo.cs | 14 +++++--- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenADependencyContextBuilder.cs b/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenADependencyContextBuilder.cs index 31b3bee2408c..b3a13c52492f 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenADependencyContextBuilder.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenADependencyContextBuilder.cs @@ -40,6 +40,7 @@ public void ItBuildsDependencyContextsFromProjectLockFiles( object[] resolvedNuGetFiles) { LockFile lockFile = TestLockFiles.GetLockFile(mainProjectName); + LockFileLookup lockFileLookup = new LockFileLookup(lockFile); SingleProjectInfo mainProject = SingleProjectInfo.Create( "/usr/Path", @@ -52,7 +53,7 @@ public void ItBuildsDependencyContextsFromProjectLockFiles( ReferenceInfo.CreateDirectReferenceInfos( referencePaths ?? new ITaskItem[] { }, referenceSatellitePaths ?? new ITaskItem[] { }, - projectContextHasProjectReferences: false, + lockFileLookup: lockFileLookup, i => true); ProjectContext projectContext = lockFile.CreateProjectContext( @@ -67,7 +68,7 @@ public void ItBuildsDependencyContextsFromProjectLockFiles( resolvedNuGetFiles = Array.Empty(); } - DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph: null, projectContext: projectContext) + DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph: null, projectContext: projectContext, libraryLookup: lockFileLookup) .WithDirectReferences(directReferences) .WithCompilationOptions(compilationOptions) .WithResolvedNuGetFiles((ResolvedFile[]) resolvedNuGetFiles) @@ -264,7 +265,7 @@ private DependencyContext BuildDependencyContextWithReferenceAssemblies(bool use useCompilationOptions ? CreateCompilationOptions() : null; - DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph: null, projectContext: projectContext) + DependencyContext dependencyContext = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph: null, projectContext: projectContext, libraryLookup: new LockFileLookup(lockFile)) .WithReferenceAssemblies(ReferenceInfo.CreateReferenceInfos(referencePaths)) .WithCompilationOptions(compilationOptions) .Build(); @@ -325,7 +326,7 @@ public void ItCanGenerateTheRuntimeFallbackGraph() void CheckRuntimeFallbacks(string runtimeIdentifier, int fallbackCount) { projectContext.LockFileTarget.RuntimeIdentifier = runtimeIdentifier; - var dependencyContextBuilder = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph, projectContext); + var dependencyContextBuilder = new DependencyContextBuilder(mainProject, includeRuntimeFileVersions: false, runtimeGraph, projectContext, libraryLookup: new LockFileLookup(lockFile)); var runtimeFallbacks = dependencyContextBuilder.Build().RuntimeGraph; runtimeFallbacks diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs b/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs index 5428145aa35f..bbbaeb68ba98 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs @@ -47,14 +47,12 @@ internal class DependencyContextBuilder private const string NetCorePlatformLibrary = "Microsoft.NETCore.App"; - public DependencyContextBuilder(SingleProjectInfo mainProjectInfo, bool includeRuntimeFileVersions, RuntimeGraph runtimeGraph, ProjectContext projectContext) + public DependencyContextBuilder(SingleProjectInfo mainProjectInfo, bool includeRuntimeFileVersions, RuntimeGraph runtimeGraph, ProjectContext projectContext, LockFileLookup libraryLookup) { _mainProjectInfo = mainProjectInfo; _includeRuntimeFileVersions = includeRuntimeFileVersions; _runtimeGraph = runtimeGraph; - var libraryLookup = new LockFileLookup(projectContext.LockFile); - _dependencyLibraries = projectContext.LockFileTarget.Libraries .Select(lockFileTargetLibrary => { diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs b/src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs index 67369ee412eb..ec0584c4162f 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/GenerateDepsFile.cs @@ -125,20 +125,19 @@ private Dictionary GetFilteredPackages() private void WriteDepsFile(string depsFilePath) { - ProjectContext projectContext; - if (AssetsFilePath == null) - { - projectContext = null; - } - else + ProjectContext projectContext = null; + LockFileLookup lockFileLookup = null; + if (AssetsFilePath != null) { LockFile lockFile = new LockFileCache(this).GetLockFile(AssetsFilePath); projectContext = lockFile.CreateProjectContext( - TargetFramework, - RuntimeIdentifier, - PlatformLibraryName, - RuntimeFrameworks, - IsSelfContained); + TargetFramework, + RuntimeIdentifier, + PlatformLibraryName, + RuntimeFrameworks, + IsSelfContained); + + lockFileLookup = new LockFileLookup(lockFile); } CompilationOptions compilationOptions = CompilationOptionsConverter.ConvertFrom(CompilerOptions); @@ -156,13 +155,14 @@ private void WriteDepsFile(string depsFilePath) IEnumerable referenceAssemblyInfos = ReferenceInfo.CreateReferenceInfos(ReferenceAssemblies); - // If there is a generated asset file. The projectContext will have project reference. - // So remove it from directReferences to avoid duplication - var projectContextHasProjectReferences = projectContext != null; + // If there is a generated asset file. The projectContext will have most project references. + // So remove any project reference contained within projectContext from directReferences to avoid duplication IEnumerable directReferences = - ReferenceInfo.CreateDirectReferenceInfos(ReferencePaths, + ReferenceInfo.CreateDirectReferenceInfos( + ReferencePaths, ReferenceSatellitePaths, - projectContextHasProjectReferences, isUserRuntimeAssembly); + lockFileLookup, + isUserRuntimeAssembly); IEnumerable dependencyReferences = ReferenceInfo.CreateDependencyReferenceInfos(ReferenceDependencyPaths, ReferenceSatellitePaths, isUserRuntimeAssembly); @@ -210,7 +210,7 @@ bool ShouldIncludeRuntimeAsset(ITaskItem item) RuntimeGraph runtimeGraph = IsSelfContained ? new RuntimeGraphCache(this).GetRuntimeGraph(RuntimeGraphPath) : null; - builder = new DependencyContextBuilder(mainProject, IncludeRuntimeFileVersions, runtimeGraph, projectContext); + builder = new DependencyContextBuilder(mainProject, IncludeRuntimeFileVersions, runtimeGraph, projectContext, lockFileLookup); } else { diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs index dfe0866df067..b9a43c386670 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs @@ -54,17 +54,21 @@ public static IEnumerable CreateReferenceInfos(IEnumerable CreateDirectReferenceInfos( IEnumerable referencePaths, IEnumerable referenceSatellitePaths, - bool projectContextHasProjectReferences, + LockFileLookup lockFileLookup, Func isRuntimeAssembly) { - - bool filterOutProjectReferenceIfInProjectContextAlready(ITaskItem referencePath) + bool lockFileContains(ITaskItem referencePath) { - return (projectContextHasProjectReferences ? !IsProjectReference(referencePath) : true); + if (lockFileLookup == null) + { + return false; + } + + return IsProjectReference(referencePath) && lockFileLookup.GetProject(referencePath.ItemSpec) != null; } IEnumerable directReferencePaths = referencePaths - .Where(r => filterOutProjectReferenceIfInProjectContextAlready(r) && !IsNuGetReference(r) && isRuntimeAssembly(r)); + .Where(r => !lockFileContains(r) && !IsNuGetReference(r) && isRuntimeAssembly(r)); return CreateFilteredReferenceInfos(directReferencePaths, referenceSatellitePaths); } From 5cb3fc2b0fdb76d96133ab8768c3ea4a82727323 Mon Sep 17 00:00:00 2001 From: William Rall Date: Tue, 25 Oct 2022 15:25:37 -0700 Subject: [PATCH 2/8] Attempt to fix null-ref when we are putting a project reference into deps.json that doesn't come from project assets --- src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs index b9a43c386670..a29c9e8130be 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs @@ -151,7 +151,7 @@ private static string GetVersion(ITaskItem referencePath) if (!string.IsNullOrEmpty(fusionName)) { AssemblyName assemblyName = new AssemblyName(fusionName); - version = assemblyName.Version.ToString(); + version = assemblyName.Version?.ToString(); } if (string.IsNullOrEmpty(version)) From 39f682e9fa4cec91d25c4b11a2bb2a6bf062cfc8 Mon Sep 17 00:00:00 2001 From: William Rall Date: Wed, 26 Oct 2022 14:27:15 -0700 Subject: [PATCH 3/8] Fix project lookup --- .../Microsoft.NET.Build.Tasks/ReferenceInfo.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs index a29c9e8130be..0cf305695608 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs @@ -64,7 +64,18 @@ bool lockFileContains(ITaskItem referencePath) return false; } - return IsProjectReference(referencePath) && lockFileLookup.GetProject(referencePath.ItemSpec) != null; + if (!IsProjectReference(referencePath)) + { + return false; + } + + string fusionName = referencePath.GetMetadata("FusionName"); + if (string.IsNullOrEmpty(fusionName)) + { + return true; + } + + return lockFileLookup.GetProject(fusionName) != null; } IEnumerable directReferencePaths = referencePaths From f65b590f43ba18f5bea26b5f8b52f07273eba2ff Mon Sep 17 00:00:00 2001 From: William Rall Date: Thu, 27 Oct 2022 01:49:31 -0700 Subject: [PATCH 4/8] Use other method for obtaining name --- src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs index 0cf305695608..034fdbd70669 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs @@ -69,13 +69,14 @@ bool lockFileContains(ITaskItem referencePath) return false; } - string fusionName = referencePath.GetMetadata("FusionName"); - if (string.IsNullOrEmpty(fusionName)) + string outputName = Path.GetFileName(referencePath.ItemSpec); + string projectName = Path.GetFileNameWithoutExtension(outputName); + if (string.IsNullOrEmpty(projectName)) { return true; } - return lockFileLookup.GetProject(fusionName) != null; + return lockFileLookup.GetProject(projectName) != null; } IEnumerable directReferencePaths = referencePaths From ed1d5f7b442b8255436b969c114133e13a64661a Mon Sep 17 00:00:00 2001 From: William Rall Date: Thu, 3 Nov 2022 02:09:55 -0700 Subject: [PATCH 5/8] Add some unit tests that help cover the scenario --- .../AuxLibrary/AuxLibrary.csproj | 58 +++++++++ .../AuxLibrary/Helper.cs | 15 +++ .../MainLibrary/Helper.cs | 16 +++ .../MainLibrary/MainLibrary.csproj | 61 ++++++++++ .../TestApp/Program.cs | 16 +++ .../TestApp/TestApp.csproj | 10 ++ ...ildAnAppWithTransitiveNonSdkProjectRefs.cs | 113 ++++++++++++++++++ ...ntToBuildAnAppWithTransitiveProjectRefs.cs | 4 +- 8 files changed, 292 insertions(+), 1 deletion(-) create mode 100644 src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/AuxLibrary.csproj create mode 100644 src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/Helper.cs create mode 100644 src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/Helper.cs create mode 100644 src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/MainLibrary.csproj create mode 100644 src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/Program.cs create mode 100644 src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/TestApp.csproj create mode 100644 src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs.cs diff --git a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/AuxLibrary.csproj b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/AuxLibrary.csproj new file mode 100644 index 000000000000..b7fdb1b2e28c --- /dev/null +++ b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/AuxLibrary.csproj @@ -0,0 +1,58 @@ + + + + + Debug + AnyCPU + 9a8d9cf9-379b-45c1-a95a-482ecb76a56c + Library + Properties + $(MSBuildProjectName) + $(MSBuildProjectName) + v4.6.2 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/Helper.cs b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/Helper.cs new file mode 100644 index 000000000000..65b9edb1719e --- /dev/null +++ b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/Helper.cs @@ -0,0 +1,15 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; + +namespace AuxLibrary +{ + public static class Helper + { + public static void WriteMessage() + { + Console.WriteLine("This string came from AuxLibrary!"); + } + } +} diff --git a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/Helper.cs b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/Helper.cs new file mode 100644 index 000000000000..f7f089c2d817 --- /dev/null +++ b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/Helper.cs @@ -0,0 +1,16 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; + +namespace MainLibrary +{ + public static class Helper + { + public static void WriteMessage() + { + Console.WriteLine("This string came from MainLibrary!"); + AuxLibrary.Helper.WriteMessage(); + } + } +} diff --git a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/MainLibrary.csproj b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/MainLibrary.csproj new file mode 100644 index 000000000000..69b9e61f4bea --- /dev/null +++ b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/MainLibrary.csproj @@ -0,0 +1,61 @@ + + + + + Debug + AnyCPU + a60a2336-1e2d-4c7a-85cb-a9e890e2558b + Library + Properties + $(MSBuildProjectName) + $(MSBuildProjectName) + v4.6.2 + 512 + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/Program.cs b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/Program.cs new file mode 100644 index 000000000000..b803e73560bd --- /dev/null +++ b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/Program.cs @@ -0,0 +1,16 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; + +namespace TestApp +{ + public class Program + { + public static void Main(string[] args) + { + Console.WriteLine("TestApp --depends on--> MainLibrary --depends on--> AuxLibrary"); + MainLibrary.Helper.WriteMessage(); + } + } +} diff --git a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/TestApp.csproj b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/TestApp.csproj new file mode 100644 index 000000000000..bb9d5646f68f --- /dev/null +++ b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/TestApp.csproj @@ -0,0 +1,10 @@ + + + Exe + $(CurrentTargetFramework) + false + + + + + \ No newline at end of file diff --git a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs.cs b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs.cs new file mode 100644 index 000000000000..fed0e50dd911 --- /dev/null +++ b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs.cs @@ -0,0 +1,113 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; +using Microsoft.DotNet.Cli.Utils; +using Microsoft.NET.TestFramework; +using Microsoft.NET.TestFramework.Assertions; +using Microsoft.NET.TestFramework.Commands; +using Xunit; +using FluentAssertions; +using System.Xml.Linq; +using System.Linq; +using System; +using Xunit.Abstractions; +using Microsoft.Extensions.DependencyModel; + +namespace Microsoft.NET.Build.Tests +{ + public class GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs : SdkTest + { + public GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs(ITestOutputHelper log) : base(log) + { + } + + [Fact] + public void It_builds_the_project_successfully() + { + // NOTE the project dependencies in AppWithTransitiveNonSdkProjectRefs: + // TestApp --depends on--> MainLibrary --depends on--> AuxLibrary (non-SDK) + // (TestApp transitively depends on AuxLibrary) + + var testAsset = _testAssetsManager + .CopyTestAsset("AppWithTransitiveNonSdkProjectRefs", "BuildAppWithTransitiveProjectRef") + .WithSource(); + + VerifyAppBuilds(testAsset); + } + + [Fact] + public void It_builds_deps_correctly_when_projects_do_not_get_restored() + { + // NOTE the project dependencies in AppWithTransitiveProjectRefs: + // TestApp --depends on--> MainLibrary --depends on--> AuxLibrary + // (TestApp transitively depends on AuxLibrary) + var testAsset = _testAssetsManager + .CopyTestAsset("AppWithTransitiveNonSdkProjectRefs", "BuildAppWithTransitiveNonSdkProjectRefsNoRestore") + .WithSource() + .WithProjectChanges( + (projectName, project) => + { + if (StringComparer.OrdinalIgnoreCase.Equals(Path.GetFileNameWithoutExtension(projectName), "AuxLibrary") || + StringComparer.OrdinalIgnoreCase.Equals(Path.GetFileNameWithoutExtension(projectName), "MainLibrary")) + { + var ns = project.Root.Name.Namespace; + + // indicate that project restore is not supported for these projects: + var target = new XElement(ns + "Target", + new XAttribute("Name", "_IsProjectRestoreSupported"), + new XAttribute("Returns", "@(_ValidProjectsForRestore)")); + + project.Root.Add(target); + } + }); + + string outputDirectory = VerifyAppBuilds(testAsset); + + using (var depsJsonFileStream = File.OpenRead(Path.Combine(outputDirectory, "TestApp.deps.json"))) + { + var dependencyContext = new DependencyContextJsonReader().Read(depsJsonFileStream); + + var projectNames = dependencyContext.RuntimeLibraries.Select(library => library.Name).ToList(); + projectNames.Should().BeEquivalentTo(new[] { "TestApp", "AuxLibrary", "MainLibrary" }); + } + } + + private string VerifyAppBuilds(TestAsset testAsset) + { + var buildCommand = new BuildCommand(testAsset, "TestApp"); + var outputDirectory = buildCommand.GetOutputDirectory(ToolsetInfo.CurrentTargetFramework); + + buildCommand + .Execute() + .Should() + .Pass(); + + outputDirectory.Should().OnlyHaveFiles(new[] { + "TestApp.dll", + "TestApp.pdb", + $"TestApp{EnvironmentInfo.ExecutableExtension}", + "TestApp.deps.json", + "TestApp.runtimeconfig.json", + "MainLibrary.dll", + "MainLibrary.pdb", + "AuxLibrary.dll", + "AuxLibrary.pdb", + }); + + new DotnetCommand(Log, Path.Combine(outputDirectory.FullName, "TestApp.dll")) + .Execute() + .Should() + .Pass() + .And + .HaveStdOutContaining("This string came from MainLibrary!") + .And + .HaveStdOutContaining("This string came from AuxLibrary!"); + + return outputDirectory.FullName; + } + } +} diff --git a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveProjectRefs.cs b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveProjectRefs.cs index c8086768b40f..479a30b06e9c 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveProjectRefs.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveProjectRefs.cs @@ -123,7 +123,9 @@ public void It_does_not_build_the_project_successfully() buildCommand .Execute("/p:DisableTransitiveProjectReferences=true") .Should() - .Fail(); + .Fail() + .And + .HaveStdOutContaining("CS0103"); } } } From ad8f1f9a5d5ed9271e627ee6447ddddeb43ba0be Mon Sep 17 00:00:00 2001 From: William Rall Date: Thu, 3 Nov 2022 02:14:51 -0700 Subject: [PATCH 6/8] Update name to be more clear --- src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs index 034fdbd70669..7736a43eea22 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs @@ -57,7 +57,7 @@ public static IEnumerable CreateDirectReferenceInfos( LockFileLookup lockFileLookup, Func isRuntimeAssembly) { - bool lockFileContains(ITaskItem referencePath) + bool lockFileContainsProject(ITaskItem referencePath) { if (lockFileLookup == null) { @@ -80,7 +80,7 @@ bool lockFileContains(ITaskItem referencePath) } IEnumerable directReferencePaths = referencePaths - .Where(r => !lockFileContains(r) && !IsNuGetReference(r) && isRuntimeAssembly(r)); + .Where(r => !lockFileContainsProject(r) && !IsNuGetReference(r) && isRuntimeAssembly(r)); return CreateFilteredReferenceInfos(directReferencePaths, referenceSatellitePaths); } From f0acd15c35c9a13f4a40d53d06f0251911de7b37 Mon Sep 17 00:00:00 2001 From: William Rall Date: Fri, 4 Nov 2022 16:16:41 -0700 Subject: [PATCH 7/8] PR feedback --- .../AuxLibrary/AuxLibrary.csproj | 58 ---------- .../AuxLibrary/Helper.cs | 15 --- .../MainLibrary/Helper.cs | 16 --- .../MainLibrary/MainLibrary.csproj | 61 ---------- .../TestApp/Program.cs | 16 --- .../TestApp/TestApp.csproj | 10 -- .../ReferenceInfo.cs | 9 +- ...ildAnAppWithTransitiveNonSdkProjectRefs.cs | 104 ++++++++++++++---- 8 files changed, 91 insertions(+), 198 deletions(-) delete mode 100644 src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/AuxLibrary.csproj delete mode 100644 src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/Helper.cs delete mode 100644 src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/Helper.cs delete mode 100644 src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/MainLibrary.csproj delete mode 100644 src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/Program.cs delete mode 100644 src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/TestApp.csproj diff --git a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/AuxLibrary.csproj b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/AuxLibrary.csproj deleted file mode 100644 index b7fdb1b2e28c..000000000000 --- a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/AuxLibrary.csproj +++ /dev/null @@ -1,58 +0,0 @@ - - - - - Debug - AnyCPU - 9a8d9cf9-379b-45c1-a95a-482ecb76a56c - Library - Properties - $(MSBuildProjectName) - $(MSBuildProjectName) - v4.6.2 - 512 - true - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - diff --git a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/Helper.cs b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/Helper.cs deleted file mode 100644 index 65b9edb1719e..000000000000 --- a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/AuxLibrary/Helper.cs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; - -namespace AuxLibrary -{ - public static class Helper - { - public static void WriteMessage() - { - Console.WriteLine("This string came from AuxLibrary!"); - } - } -} diff --git a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/Helper.cs b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/Helper.cs deleted file mode 100644 index f7f089c2d817..000000000000 --- a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/Helper.cs +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; - -namespace MainLibrary -{ - public static class Helper - { - public static void WriteMessage() - { - Console.WriteLine("This string came from MainLibrary!"); - AuxLibrary.Helper.WriteMessage(); - } - } -} diff --git a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/MainLibrary.csproj b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/MainLibrary.csproj deleted file mode 100644 index 69b9e61f4bea..000000000000 --- a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/MainLibrary/MainLibrary.csproj +++ /dev/null @@ -1,61 +0,0 @@ - - - - - Debug - AnyCPU - a60a2336-1e2d-4c7a-85cb-a9e890e2558b - Library - Properties - $(MSBuildProjectName) - $(MSBuildProjectName) - v4.6.2 - 512 - true - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/Program.cs b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/Program.cs deleted file mode 100644 index b803e73560bd..000000000000 --- a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/Program.cs +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; - -namespace TestApp -{ - public class Program - { - public static void Main(string[] args) - { - Console.WriteLine("TestApp --depends on--> MainLibrary --depends on--> AuxLibrary"); - MainLibrary.Helper.WriteMessage(); - } - } -} diff --git a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/TestApp.csproj b/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/TestApp.csproj deleted file mode 100644 index bb9d5646f68f..000000000000 --- a/src/Assets/TestProjects/AppWithTransitiveNonSdkProjectRefs/TestApp/TestApp.csproj +++ /dev/null @@ -1,10 +0,0 @@ - - - Exe - $(CurrentTargetFramework) - false - - - - - \ No newline at end of file diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs b/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs index 7736a43eea22..163c54d9d6ad 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/ReferenceInfo.cs @@ -69,11 +69,14 @@ bool lockFileContainsProject(ITaskItem referencePath) return false; } - string outputName = Path.GetFileName(referencePath.ItemSpec); - string projectName = Path.GetFileNameWithoutExtension(outputName); + string projectName = referencePath.GetMetadata(MetadataKeys.MSBuildSourceProjectFile); if (string.IsNullOrEmpty(projectName)) { - return true; + projectName = Path.GetFileNameWithoutExtension(referencePath.ItemSpec); + if (string.IsNullOrEmpty(projectName)) + { + return true; + } } return lockFileLookup.GetProject(projectName) != null; diff --git a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs.cs b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs.cs index fed0e50dd911..1e36e845b58d 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs.cs @@ -1,21 +1,18 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.Collections.Generic; -using System.Diagnostics; +using System; using System.IO; -using System.Runtime.InteropServices; -using Microsoft.DotNet.Cli.Utils; +using System.Linq; +using System.Xml.Linq; + +using FluentAssertions; +using Microsoft.Extensions.DependencyModel; using Microsoft.NET.TestFramework; using Microsoft.NET.TestFramework.Assertions; using Microsoft.NET.TestFramework.Commands; -using Xunit; -using FluentAssertions; -using System.Xml.Linq; -using System.Linq; -using System; +using Microsoft.NET.TestFramework.ProjectConstruction; using Xunit.Abstractions; -using Microsoft.Extensions.DependencyModel; namespace Microsoft.NET.Build.Tests { @@ -25,29 +22,26 @@ public GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs(ITestOutputHel { } - [Fact] + [WindowsOnlyFact] public void It_builds_the_project_successfully() { - // NOTE the project dependencies in AppWithTransitiveNonSdkProjectRefs: + // NOTE the projects created by CreateTestProject: // TestApp --depends on--> MainLibrary --depends on--> AuxLibrary (non-SDK) // (TestApp transitively depends on AuxLibrary) - var testAsset = _testAssetsManager - .CopyTestAsset("AppWithTransitiveNonSdkProjectRefs", "BuildAppWithTransitiveProjectRef") - .WithSource(); + .CreateTestProject(CreateTestProject()); VerifyAppBuilds(testAsset); } - [Fact] + [WindowsOnlyFact] public void It_builds_deps_correctly_when_projects_do_not_get_restored() { - // NOTE the project dependencies in AppWithTransitiveProjectRefs: + // NOTE the projects created by CreateTestProject: // TestApp --depends on--> MainLibrary --depends on--> AuxLibrary // (TestApp transitively depends on AuxLibrary) var testAsset = _testAssetsManager - .CopyTestAsset("AppWithTransitiveNonSdkProjectRefs", "BuildAppWithTransitiveNonSdkProjectRefsNoRestore") - .WithSource() + .CreateTestProject(CreateTestProject()) .WithProjectChanges( (projectName, project) => { @@ -76,6 +70,78 @@ public void It_builds_deps_correctly_when_projects_do_not_get_restored() } } + private TestProject CreateTestProject() + { + string targetFrameworkVersion = "v4.8"; + + var auxLibraryProject = new TestProject("AuxLibrary") + { + IsSdkProject = false, + TargetFrameworkVersion = targetFrameworkVersion + }; + auxLibraryProject.SourceFiles["Helper.cs"] = """ + using System; + + namespace AuxLibrary + { + public static class Helper + { + public static void WriteMessage() + { + Console.WriteLine("This string came from AuxLibrary!"); + } + } + } + """; + + var mainLibraryProject = new TestProject("MainLibrary") + { + IsSdkProject = false, + TargetFrameworkVersion = targetFrameworkVersion + }; + mainLibraryProject.ReferencedProjects.Add(auxLibraryProject); + mainLibraryProject.SourceFiles["Helper.cs"] = """ + using System; + + namespace MainLibrary + { + public static class Helper + { + public static void WriteMessage() + { + Console.WriteLine("This string came from MainLibrary!"); + AuxLibrary.Helper.WriteMessage(); + } + } + } + """; + + var testAppProject = new TestProject("TestApp") + { + IsExe = true, + TargetFrameworks = ToolsetInfo.CurrentTargetFramework + }; + testAppProject.AdditionalProperties["ProduceReferenceAssembly"] = "false"; + testAppProject.ReferencedProjects.Add(mainLibraryProject); + testAppProject.SourceFiles["Program.cs"] = """ + using System; + + namespace TestApp + { + public class Program + { + public static void Main(string[] args) + { + Console.WriteLine("TestApp --depends on--> MainLibrary --depends on--> AuxLibrary"); + MainLibrary.Helper.WriteMessage(); + } + } + } + """; + + return testAppProject; + } + private string VerifyAppBuilds(TestAsset testAsset) { var buildCommand = new BuildCommand(testAsset, "TestApp"); From 28569c109bfea88884a666eb2f0e44e0f19dcb17 Mon Sep 17 00:00:00 2001 From: William Rall Date: Fri, 4 Nov 2022 16:46:24 -0700 Subject: [PATCH 8/8] Cover scenario of assembly name not matching csproj file name --- ...ildAnAppWithTransitiveNonSdkProjectRefs.cs | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs.cs b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs.cs index 1e36e845b58d..b4ba4cf54cae 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildAnAppWithTransitiveNonSdkProjectRefs.cs @@ -12,6 +12,8 @@ using Microsoft.NET.TestFramework.Assertions; using Microsoft.NET.TestFramework.Commands; using Microsoft.NET.TestFramework.ProjectConstruction; + +using Xunit; using Xunit.Abstractions; namespace Microsoft.NET.Build.Tests @@ -31,11 +33,13 @@ public void It_builds_the_project_successfully() var testAsset = _testAssetsManager .CreateTestProject(CreateTestProject()); - VerifyAppBuilds(testAsset); + VerifyAppBuilds(testAsset, string.Empty); } - [WindowsOnlyFact] - public void It_builds_deps_correctly_when_projects_do_not_get_restored() + [WindowsOnlyTheory] + [InlineData("")] + [InlineData("TestApp.")] + public void It_builds_deps_correctly_when_projects_do_not_get_restored(string prefix) { // NOTE the projects created by CreateTestProject: // TestApp --depends on--> MainLibrary --depends on--> AuxLibrary @@ -45,11 +49,20 @@ public void It_builds_deps_correctly_when_projects_do_not_get_restored() .WithProjectChanges( (projectName, project) => { - if (StringComparer.OrdinalIgnoreCase.Equals(Path.GetFileNameWithoutExtension(projectName), "AuxLibrary") || - StringComparer.OrdinalIgnoreCase.Equals(Path.GetFileNameWithoutExtension(projectName), "MainLibrary")) + string projectFileName = Path.GetFileNameWithoutExtension(projectName); + if (StringComparer.OrdinalIgnoreCase.Equals(projectFileName, "AuxLibrary") || + StringComparer.OrdinalIgnoreCase.Equals(projectFileName, "MainLibrary")) { var ns = project.Root.Name.Namespace; + if (!string.IsNullOrEmpty(prefix)) + { + XElement propertyGroup = project.Root.Element(XName.Get("PropertyGroup", ns.NamespaceName)); + XElement assemblyName = propertyGroup.Element(XName.Get("AssemblyName", ns.NamespaceName)); + assemblyName.RemoveAll(); + assemblyName.Add("TestApp." + projectFileName); + } + // indicate that project restore is not supported for these projects: var target = new XElement(ns + "Target", new XAttribute("Name", "_IsProjectRestoreSupported"), @@ -59,14 +72,14 @@ public void It_builds_deps_correctly_when_projects_do_not_get_restored() } }); - string outputDirectory = VerifyAppBuilds(testAsset); + string outputDirectory = VerifyAppBuilds(testAsset, prefix); using (var depsJsonFileStream = File.OpenRead(Path.Combine(outputDirectory, "TestApp.deps.json"))) { var dependencyContext = new DependencyContextJsonReader().Read(depsJsonFileStream); var projectNames = dependencyContext.RuntimeLibraries.Select(library => library.Name).ToList(); - projectNames.Should().BeEquivalentTo(new[] { "TestApp", "AuxLibrary", "MainLibrary" }); + projectNames.Should().BeEquivalentTo(new[] { "TestApp", prefix + "AuxLibrary", prefix + "MainLibrary" }); } } @@ -142,7 +155,7 @@ public static void Main(string[] args) return testAppProject; } - private string VerifyAppBuilds(TestAsset testAsset) + private string VerifyAppBuilds(TestAsset testAsset, string prefix) { var buildCommand = new BuildCommand(testAsset, "TestApp"); var outputDirectory = buildCommand.GetOutputDirectory(ToolsetInfo.CurrentTargetFramework); @@ -158,10 +171,10 @@ private string VerifyAppBuilds(TestAsset testAsset) $"TestApp{EnvironmentInfo.ExecutableExtension}", "TestApp.deps.json", "TestApp.runtimeconfig.json", - "MainLibrary.dll", - "MainLibrary.pdb", - "AuxLibrary.dll", - "AuxLibrary.pdb", + prefix + "MainLibrary.dll", + prefix + "MainLibrary.pdb", + prefix + "AuxLibrary.dll", + prefix + "AuxLibrary.pdb", }); new DotnetCommand(Log, Path.Combine(outputDirectory.FullName, "TestApp.dll"))