From 4e16cfd4daf0158e52f6293b0fbfa37091e09eff Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 26 Jun 2019 11:18:54 -0700 Subject: [PATCH] Use LogErrorFromException when FindAssembliesWithReferencesTo throws (dotnet/aspnetcore-tooling#702) Currently FindAssembliesWithReferencesTo produces a stacktrace when P2P builds fail - for instance if the referenced project has a compiler error. This changes it to use LogErrorFromException. In addition, there was a typo in the property that disabled the feature. Fixes https://github.com/aspnet/AspNetCore/issues/11226\n\nCommit migrated from https://github.com/dotnet/aspnetcore-tooling/commit/87c12a116c724696783b62e5552d1ea4862e5715 --- ...erences.cs => FindAssembliesWithReferencesTo.cs} | 13 ++++++++++--- ...T.Sdk.Razor.MvcApplicationPartsDiscovery.targets | 4 ++-- .../ApplicationPartDiscoveryIntegrationTest.cs | 11 +++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) rename src/Razor/Microsoft.NET.Sdk.Razor/src/{ResolveAssemblyWithReferences.cs => FindAssembliesWithReferencesTo.cs} (85%) diff --git a/src/Razor/Microsoft.NET.Sdk.Razor/src/ResolveAssemblyWithReferences.cs b/src/Razor/Microsoft.NET.Sdk.Razor/src/FindAssembliesWithReferencesTo.cs similarity index 85% rename from src/Razor/Microsoft.NET.Sdk.Razor/src/ResolveAssemblyWithReferences.cs rename to src/Razor/Microsoft.NET.Sdk.Razor/src/FindAssembliesWithReferencesTo.cs index 3727e01088..c936eb4c9b 100644 --- a/src/Razor/Microsoft.NET.Sdk.Razor/src/ResolveAssemblyWithReferences.cs +++ b/src/Razor/Microsoft.NET.Sdk.Razor/src/FindAssembliesWithReferencesTo.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -45,9 +46,15 @@ namespace Microsoft.AspNetCore.Razor.Tasks var targetAssemblyNames = TargetAssemblyNames.Select(s => s.ItemSpec).ToList(); var provider = new ReferenceResolver(targetAssemblyNames, referenceItems); - var assemblyNames = provider.ResolveAssemblies(); - - ResolvedAssemblies = assemblyNames.ToArray(); + try + { + var assemblyNames = provider.ResolveAssemblies(); + ResolvedAssemblies = assemblyNames.ToArray(); + } + catch (Exception ex) + { + Log.LogErrorFromException(ex); + } return !Log.HasLoggedErrors; } diff --git a/src/Razor/Microsoft.NET.Sdk.Razor/src/build/netstandard2.0/Microsoft.NET.Sdk.Razor.MvcApplicationPartsDiscovery.targets b/src/Razor/Microsoft.NET.Sdk.Razor/src/build/netstandard2.0/Microsoft.NET.Sdk.Razor.MvcApplicationPartsDiscovery.targets index 669cfd40c9..bcd4e91e22 100644 --- a/src/Razor/Microsoft.NET.Sdk.Razor/src/build/netstandard2.0/Microsoft.NET.Sdk.Razor.MvcApplicationPartsDiscovery.targets +++ b/src/Razor/Microsoft.NET.Sdk.Razor/src/build/netstandard2.0/Microsoft.NET.Sdk.Razor.MvcApplicationPartsDiscovery.targets @@ -17,9 +17,9 @@ Copyright (c) .NET Foundation. All rights reserved. Condition="'$(RazorSdkBuildTasksAssembly)' != ''" /> - true + true - + _DiscoverMvcApplicationParts; $(CoreCompileDependsOn); diff --git a/src/Razor/Microsoft.NET.Sdk.Razor/test/IntegrationTests/ApplicationPartDiscoveryIntegrationTest.cs b/src/Razor/Microsoft.NET.Sdk.Razor/test/IntegrationTests/ApplicationPartDiscoveryIntegrationTest.cs index 1f07c59299..45a70364ee 100644 --- a/src/Razor/Microsoft.NET.Sdk.Razor/test/IntegrationTests/ApplicationPartDiscoveryIntegrationTest.cs +++ b/src/Razor/Microsoft.NET.Sdk.Razor/test/IntegrationTests/ApplicationPartDiscoveryIntegrationTest.cs @@ -37,6 +37,17 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests Assert.AssemblyHasAttribute(result, Path.Combine(OutputPath, "AppWithP2PReference.dll"), "Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartAttribute"); } + [Fact] + [InitializeTestProject("AppWithP2PReference", additionalProjects: "ClassLibrary")] + public async Task Build_ProjectWithDependencyThatReferencesMvc_DoesNotGenerateAttributeIfFlagIsReset() + { + var result = await DotnetMSBuild("Build /p:GenerateMvcApplicationPartsAssemblyAttributes=false"); + + Assert.BuildPassed(result); + + Assert.FileDoesNotExist(result, IntermediateOutputPath, "AppWithP2PReference.MvcApplicationPartsAssemblyInfo.cs"); + } + [Fact] [InitializeTestProject("SimpleMvc")] public async Task Build_ProjectWithoutMvcReferencingDependencies_DoesNotGenerateAttribute()