Bug 57130 - ICodeCompiler cannot target C#7 (Roslyn)
Summary: ICodeCompiler cannot target C#7 (Roslyn)
Status: RESOLVED DUPLICATE of bug 51609
Alias: None
Product: Class Libraries
Classification: Mono
Component: System ()
Version: master
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
URL:
: 57134 ()
Depends on:
Blocks:
 
Reported: 2017-06-03 13:50 UTC by oleg.shilo
Modified: 2017-09-13 13:36 UTC (History)
3 users (show)

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


Attachments
test VS project showing the problem (3.86 KB, application/x-zip-compressed)
2017-06-03 13:50 UTC, oleg.shilo
Details


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:
Status:
RESOLVED DUPLICATE of bug 51609

Description oleg.shilo 2017-06-03 13:50:31 UTC
Created attachment 22669 [details]
test VS project showing the problem

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.

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 22:58:12 UTC
Steps to reproduce: build executable from the test project and run it under mono.
Comment 2 oleg.shilo 2017-06-03 23:00:00 UTC
*** Bug 57134 has been marked as a duplicate of this bug. ***
Comment 3 Marek Safar 2017-09-13 13:36:10 UTC

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