Bug 41979

Summary: CodeDom cannot call mcs because of invalid encoding configuration
Product: [Mono] Class Libraries Reporter: matthi.d
Component: SystemAssignee: Marek Safar <masafa>
Severity: normal CC: alkpli, jason.imison, masafa, mono-bugs+mono, tajinders
Priority: ---    
Version: 4.4.0 (C7)   
Target Milestone: (C7SR1)   
Hardware: PC   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: F# sample reproducing the problem

Description matthi.d 2016-06-19 10:24:34 UTC
Created attachment 16401 [details]
F# sample reproducing the problem

In mono 4.4.0 CodeDom cannot be used when the code to compile generates a warning.
Initial bug-report: https://github.com/fsprojects/ProjectScaffold/issues/252

It's easily reproduced by trying to compile some C# code with a warning (here simple F# sample trying to compile some C#)

1. git clone https://github.com/matthid/mono_codedom_encoding_bug.git
2. cd mono_codedom_encoding_bug
3. ./build.sh

This will run the following code:

    use prov = new CSharpCodeProvider()
    let source = @"
#warning Trigger Some Warning
namespace MyNamespace {
  public class MyClass {
    public static string MyMethod () { return ""data""; }
    let tempDirectory = Path.Combine(Path.GetTempPath(), "RazorEngine_" + Path.GetRandomFileName())
    Directory.CreateDirectory(tempDirectory) |> ignore
    let p = 
      new CompilerParameters(
        GenerateInMemory = false,
        GenerateExecutable = false,
        IncludeDebugInformation = true,
        TreatWarningsAsErrors = false,
        TempFiles = new TempFileCollection(tempDirectory, true),
        CompilerOptions = String.Format("/target:library /optimize /define:RAZORENGINE {0}", ""))
    let tempDir = p.TempFiles.TempDir
    let assemblyName = Path.Combine(tempDir, String.Format("{0}.dll", "MyNamespace"))
    p.TempFiles.AddFile(assemblyName, true)
    let results = prov.CompileAssemblyFromSource(p, [| source |])
    if isNull results.Errors |> not && results.Errors.HasErrors then
      printfn "Results: %A" results
      failwith "Compilation failed"
      printfn "Success"

It seems like the encoding of mcs has changed (because of using roslyn?) therefore CodeDom can no longer parse the text.

Related Code place: https://github.com/mono/mono/blob/master/mcs/class/System/Microsoft.CSharp/CSharpCodeCompiler.cs#L213


    Starting Target: Travis
    Codepage: SomeValue
    Finished Target: Travis



    Starting Target: Travis 
    Codepage: 65001
    Results: System.CodeDom.Compiler.CompilerResults

     - : (0, 0) 琯灭刯穡牯湅楧敮機牪欴礴⹶焹⽱攳攱㡡㍣〮挮⡳ⰳ⤰›慷湲湩⁧千〱〳›眣牡楮杮›呠楲杧牥匠浯⁥慗湲湩❧

    Decoded: /tmp/RazorEngine_jjr4k4yv.9qq/3e1ea8c3.0.cs(3,0): warning CS1030: #warning: `Trigger Some Warning'
    Native return value: 0
    Message: /usr/bin/mcs /target:library /debug+ /optimize- /out:"/tmp/RazorEngine_jjr4k4yv.9qq/3e1ea8c3.dll" /target:library /optimize /define:RAZORENGINE   -- "/tmp/RazorEngine_jjr4k4yv.9qq/3e1ea8c3.0.cs" 
    Message: 琯灭刯穡牯湅楧敮機牪欴礴⹶焹⽱攳攱㡡㍣〮挮⡳ⰳ⤰›慷湲湩⁧千〱〳›眣牡楮杮›呠楲杧牥匠浯⁥慗湲湩❧
    Exists: false

    Running build failed.
    System.Exception: Compilation failed


I thought setting the default encoding would be a workaround, but I couldn't make it work: https://travis-ci.org/matthid/mono_codedom_encoding_bug/jobs/138692237

  // Not working
  //try Console.OutputEncoding <- System.Text.Encoding.GetEncoding(1200) with _ -> ()
  //try Console.InputEncoding <- System.Text.Encoding.GetEncoding(1200) with _ -> ()

There are still some characters in the message which seem to prevent the message parser to succeed.
Therefore I don't have a known workaround.
Comment 1 Marek Safar 2016-06-21 17:53:37 UTC
Fixed in master and Mono 4.5.1
Comment 2 Alexander Köplinger [MSFT] 2016-06-30 01:16:45 UTC
As discussed with Marek, I filed the Trello card to backport this to a Mono 4.4 service release too and verifies it fixes the FAKE issue: https://trello.com/c/tA4vVbvw/60-mono-bug-41979-codedom-cannot-call-mcs-because-of-invalid-encoding-configuration