2

I'm trying to run csc.exe (the C# compiler) from within an AppDomain created from my executable. However, what I'm seeing is that any argument passed to my executable also seems to be passed to csc.exe, even though I'm not providing any arguments.

Here's my complete application:

class Program
{
  static void Main(string[] args)
  {
    var cscPath = @"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\csc.exe";
    var appDomain = AppDomain.CreateDomain("csc");
    appDomain.ExecuteAssembly(cscPath);

    Console.ReadLine();
  }
}

When I run this without providing any arguments to my program, I see what I expect:

warning CS2008: No source files specified.
error CS1562: Outputs without source must have the /out option specified

However, when I add "-test" to the command line of my executable (using my project's Debug tab in Visual Studio), I get this output:

error CS2007: Unrecognized option: '-test'
warning CS2008: No source files specified.
error CS1562: Outputs without source must have the /out option specified

The "-test" now shows up as an error to csc.exe, and I don't know why it's even seeing that parameter.

Obviously, this is a contrived example, and I'm not really trying to call csc.exe without any parameters, but this is the minimal example where I can see the behavior.

If I call csc.exe using Process.Start, I don't see this behavior.

Any help would be appreciated!

Nathan
  • 1,591
  • 4
  • 17
  • 22
  • Why are you needing to run csc in an app domain? – Ron Beyer Apr 05 '18 at 02:48
  • `csc.exe` is not designed to be hosted within another process. It's starting up at its entry point and the first thing it does is inspect the arguments supplied to the *process*. Since there's only one process (the one that started to run your own assembly), yes, they share arguments. – Damien_The_Unbeliever Apr 05 '18 at 06:29
  • @RonBeyer The code I'm working with was already doing it, using an older version of the Roslyn compiler (C# 6 which has been working for a while). I've been trying to upgrade it to use the newer version of Csc (C# 7+). – Nathan Apr 05 '18 at 12:40
  • @Damien_The_Unbeliever Did it change? Prior versions of Csc currently work with this methodology. – Nathan Apr 05 '18 at 12:42
  • @Damien_The_Unbeliever Found the code that references what you're referring to (https://github.com/dotnet/roslyn/blob/a3f904c792fbe6b22fad1254b299b8c178340247/src/Compilers/Shared/BuildClient.cs#L268). Thanks for the pointer. – Nathan Apr 05 '18 at 12:58

0 Answers0