Bug 57134 - ICodeCompiler cannot target C#7 (Roslyn)
Summary: ICodeCompiler cannot target C#7 (Roslyn)
Status: RESOLVED DUPLICATE of bug 57130
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: 5.0 (2017-02)
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2017-06-03 22:54 UTC by oleg.shilo
Modified: 2017-06-03 23:00 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description oleg.shilo 2017-06-03 22:54:46 UTC
Affected: Mono JIT compiler version 5.0.1 (Visual Studio built mono)

Possibly related to https://bugzilla.xamarin.com/show_bug.cgi?id=51609

Invoking `ICodeCompiler.CompileAssemblyFromFile` unconditionally produces the security exception for CSharpCodeProvider form Microsoft.CodeDom.Providers.DotNetCompilerPlatform v1.0.4 (https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform). This nuget package supposed to be the only way to use ICodeCompiler bound to Roslyn compilers. The test project is attached. 

Steps to reproduce: build exe from the test project and launch it under mono.

The problem affects both Windows and Linux and effectively it means that on Mono ICodeCompiler targeting C# 7 (Roslyn) cannot be used at all. 

The exception "System.Security.SecurityException: Couldn't impersonate token." is always thrown from System.dll (v4.0.0.0):

public static int ExecWaitWithCapture(IntPtr userToken, string cmd, string currentDir, TempFileCollection tempFiles, ref string outputName, ref string errorName)
using (WindowsIdentity.Impersonate(userToken))
    return InternalExecWaitWithCapture(cmd, currentDir, tempFiles, ref outputName, ref errorName);
By using Reflection I managed to bypass WindowsIdentity.Impersonate and call InternalExecWaitWithCapture directly and it does solve the problem. Well at least the 'CompileAssemblyFromFile' succeeded.

This work around is very similar to the one that Razor team resorted to:  https://github.com/Antaris/RazorEngine/blob/master/src/source/RazorEngine.Core/Compilation/DirectCompilerServiceBase.cs (line 323)
Comment 1 oleg.shilo 2017-06-03 23:00:00 UTC
Accidental duplicate of #57130

*** This bug has been marked as a duplicate of bug 57130 ***