From b4310ac8a80217ca908fb1d41fa354c28c0bef88 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 23 Mar 2016 20:02:50 -0700 Subject: [PATCH] Modify RoslynCompilationService to generate portable pdbs Fixes #4330 --- .../DefaultRoslynCompilationService.cs | 40 +++++----------- .../Internal/SymbolsUtility.cs | 46 ------------------- 2 files changed, 11 insertions(+), 75 deletions(-) delete mode 100644 src/Microsoft.AspNetCore.Mvc.Razor/Internal/SymbolsUtility.cs diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs index e8bed94b27..beacc472da 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/DefaultRoslynCompilationService.cs @@ -32,7 +32,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal /// public class DefaultRoslynCompilationService : ICompilationService { - private readonly Lazy _supportsPdbGeneration = new Lazy(SymbolsUtility.SupportsSymbolsGeneration); private readonly ConcurrentDictionary _metadataFileCache = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); private readonly IFileProvider _fileProvider; @@ -112,13 +111,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal path: assemblyName, options: _parseOptions); - var references = _applicationReferences.Value; - var compilation = CSharpCompilation.Create( assemblyName, options: _compilationOptions, syntaxTrees: new[] { syntaxTree }, - references: references); + references: _applicationReferences.Value); compilation = Rewrite(compilation); @@ -126,20 +123,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal _compilationCallback(compilationContext); compilation = compilationContext.Compilation; - using (var ms = new MemoryStream()) + using (var assemblyStream = new MemoryStream()) { - using (var pdb = new MemoryStream()) + using (var pdbStream = new MemoryStream()) { - EmitResult result; - - if (_supportsPdbGeneration.Value) - { - result = compilation.Emit(ms, pdbStream: pdb); - } - else - { - result = compilation.Emit(ms); - } + var result = compilation.Emit( + assemblyStream, + pdbStream, + options: new EmitOptions(debugInformationFormat: DebugInformationFormat.PortablePdb)); if (!result.Success) { @@ -162,19 +153,10 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal result.Diagnostics); } - Assembly assembly; - ms.Seek(0, SeekOrigin.Begin); - - if (_supportsPdbGeneration.Value) - { - pdb.Seek(0, SeekOrigin.Begin); - assembly = LoadStream(ms, pdb); - } - else - { - assembly = LoadStream(ms, assemblySymbols: null); - } + assemblyStream.Seek(0, SeekOrigin.Begin); + pdbStream.Seek(0, SeekOrigin.Begin); + var assembly = LoadStream(assemblyStream, pdbStream); var type = assembly.GetExportedTypes().FirstOrDefault(a => !a.IsNested); _logger.GeneratedCodeToAssemblyCompilationEnd(fileInfo.RelativePath, startTimestamp); @@ -186,7 +168,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal private Assembly LoadStream(MemoryStream ms, MemoryStream assemblySymbols) { #if NET451 - return Assembly.Load(ms.ToArray(), assemblySymbols?.ToArray()); + return Assembly.Load(ms.ToArray(), assemblySymbols.ToArray()); #else return _razorLoadContext.Load(ms, assemblySymbols); #endif diff --git a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/SymbolsUtility.cs b/src/Microsoft.AspNetCore.Mvc.Razor/Internal/SymbolsUtility.cs deleted file mode 100644 index 2edb56e75e..0000000000 --- a/src/Microsoft.AspNetCore.Mvc.Razor/Internal/SymbolsUtility.cs +++ /dev/null @@ -1,46 +0,0 @@ -// 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.Runtime.InteropServices; - -namespace Microsoft.AspNetCore.Mvc.Razor.Internal -{ - /// - /// Utility type for determining if a platform supports symbol file generation. - /// - public class SymbolsUtility - { - private const string SymWriterGuid = "0AE2DEB0-F901-478b-BB9F-881EE8066788"; - private static readonly Lazy _isMono = new Lazy(() => Type.GetType("Mono.Runtime") != null); - - /// - /// Determines if the current platform supports symbols (pdb) generation. - /// - /// true if pdb generation is supported; false otherwise. - public static bool SupportsSymbolsGeneration() - { - if (_isMono.Value) - { - return false; - } - - try - { - // Check for the pdb writer component that roslyn uses to generate pdbs - var type = Marshal.GetTypeFromCLSID(new Guid(SymWriterGuid)); - if (type != null) - { - // This line will throw if pdb generation is not supported. - Activator.CreateInstance(type); - return true; - } - } - catch - { - } - - return false; - } - } -} \ No newline at end of file