There is sample:
using System;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
namespace TestR
{
class MainClass
{
public static void Main (string[] args)
{
var code = @"
namespace TestR
{
class MainClass
{
public static void AAA (string[] args)
{
}
}
}
";
var syntaxTree = CSharpSyntaxTree.ParseText(code);
var references = new [] {
MetadataReference.CreateFromFile (typeof(Type).Assembly.Location)
};
var compilation = CSharpCompilation.Create(
"assembly",
new[] {syntaxTree},
references,
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
using (var ms = new MemoryStream ())
using (var pdbs = new MemoryStream ())
{
var result = compilation.Emit (ms, pdbs);
if (!result.Success)
{
throw new Exception ("Not compiled: " + result.Diagnostics[0]);
}
Console.WriteLine ("Compiled and saved");
}
}
}
}
But if I remove pdbs from compilation.Emit (ms, pdbs) that will compile successfully.
Running without mono all works.
What OS are you running on?
Also, could you run under the debugger and share a stack trace of the first-chance exception?
Running on OS X 10.11.1.
Roslyn do not throw any exception. I throw this manually.
throw new Exception ("Not compiled: " + result.Diagnostics[0]);
By default Roslyn produces Windows-specific PDBs to ensure backward compatibility. These PDBs enable the full feature set of Windows debuggers (including stepping, expression evaluation, Edit and Continue, etc.). However, these PDBs are not possible to read or write on non-Windows systems.
To generate cross-platform Portable PDB format you can set DebugInformationFormat emit option
http://source.roslyn.io/#Microsoft.CodeAnalysis/Emit/EmitOptions.cs,61
to PortablePdb.
Note that this format is new and there is currently a limited support for it. I believe the latest Mono build runtime supports it.
I am getting this error too.
I'm sorry to be an idiot, but where do you set the DebugInformationFormat option? Is this in the csproj somewhere?
@golfguy0082 I had the same problem with a .NET 4.6 solution compiling on macOS. For this, I found the document "Portable PDB's" in the OmniSharp repo which explains this a bit.
Basically put, you can set this in the .csproj file:
<DebugType>portable</DebugType>
Or, it if's a dotnet core project, set it in the project.json:
"buildOptions": {
"debugType": "portable"
}
portablepdb's seems to be a new thing introduced with dotnet core, so results may very depending on your IDE.
Thank you very much @hanssens! Very helpful!
@hanssens I did that and i ended up with this error.
Error CS1902: Invalid option 'portable' for /debug; must be full or pdbonly (CS1902)"
Any thoughts on this?
@dharanesh03 no, unfortunately not. It seems that the compiler, or IDE, you're using doesn't support the "portable" option just yet. As mentioned in the Omnisharp docs this is pretty new.
@dharanesh03 @hanssens the same thing :(. I couldn't compile the project
The original issue is about using Roslyn emit APIs to produce a binary and a PDB on OS X. The answer to that is:
To generate cross-platform Portable PDB format you can set DebugInformationFormat emit option http://source.roslyn.io/#Microsoft.CodeAnalysis/Emit/EmitOptions.cs,61 to PortablePdb.
That's related but different issue from not being able to build a .csproj on OS X/Linux.
@castrojr913 @dharanesh03 What is the version of the compiler and IDE you're using?
@tmat IDE: Visual Studio Preview 4 (7.0 Build 1566). OS: Mac OSX 10.12.3 (Sierra).
@castrojr913 Does setting debug type in .csproj fix the issue?
<DebugType>portable</DebugType>
Didn't fix it for me. Same error.
@castrojr913 Does setting debug type in .csproj fix the issue?
<DebugType>portable</DebugType>
It's 2020 now and this type is still not supported by the latest version of VS for MAC
Most helpful comment
I'm sorry to be an idiot, but where do you set the DebugInformationFormat option? Is this in the csproj somewhere?