Modify RoslynCompilationService to generate portable pdbs

Fixes #4330
This commit is contained in:
Pranav K 2016-03-23 20:02:50 -07:00
parent cda1307848
commit b4310ac8a8
2 changed files with 11 additions and 75 deletions

View File

@ -32,7 +32,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Internal
/// </summary>
public class DefaultRoslynCompilationService : ICompilationService
{
private readonly Lazy<bool> _supportsPdbGeneration = new Lazy<bool>(SymbolsUtility.SupportsSymbolsGeneration);
private readonly ConcurrentDictionary<string, AssemblyMetadata> _metadataFileCache =
new ConcurrentDictionary<string, AssemblyMetadata>(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

View File

@ -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
{
/// <summary>
/// Utility type for determining if a platform supports symbol file generation.
/// </summary>
public class SymbolsUtility
{
private const string SymWriterGuid = "0AE2DEB0-F901-478b-BB9F-881EE8066788";
private static readonly Lazy<bool> _isMono = new Lazy<bool>(() => Type.GetType("Mono.Runtime") != null);
/// <summary>
/// Determines if the current platform supports symbols (pdb) generation.
/// </summary>
/// <returns><c>true</c> if pdb generation is supported; <c>false</c> otherwise.</returns>
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;
}
}
}