Bug 41979 - CodeDom cannot call mcs because of invalid encoding configuration
Summary: CodeDom cannot call mcs because of invalid encoding configuration
Status: VERIFIED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: 4.4.0 (C7)
Hardware: PC Mac OS
: --- normal
Target Milestone: (C7SR1)
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2016-06-19 10:24 UTC by matthi.d
Modified: 2016-07-08 12:22 UTC (History)
5 users (show)

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


Attachments
F# sample reproducing the problem (2.55 KB, text/plain)
2016-06-19 10:24 UTC, matthi.d
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:
VERIFIED FIXED

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"
    else 
      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

Expected:

    Starting Target: Travis
    Codepage: SomeValue
    Success
    Finished Target: Travis

Actual:

https://travis-ci.org/matthid/mono_codedom_encoding_bug/builds/138692622

    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: 琯灭刯穡牯湅楧敮機牪欴礴⹶焹⽱攳攱㡡㍣〮挮⡳ⰳ⤰›慷湲湩⁧千〱〳›眣牡楮杮›呠楲杧牥匠浯⁥慗湲湩❧
    ResultAssembly: 
    Exists: false

    Running build failed.
    Error:
    System.Exception: Compilation failed


Workarounds:

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