roslyn: MSBuildWorkspace.OpenProjectAsync throws InvalidOperationException when invoked from custom MsBuild task

I havent found anywhere you wouldn’t support this scenario, and even though its a bit on the exotic side, I’ll report it in case you want to dig into it.

Steps to Reproduce:

  1. Create a custom MsBuild task in a standard C# library, e.g. similar to this.
  2. In Execute(), add the code below.
  3. Invoke it from a project (standard Console Application will do fine).

In Task.Execute()

var ws = MSBuildWorkspace.Create();
var project = ws.OpenProjectAsync("C:\path\to\some\standard\project.csproj").Result;

Expected Behavior: A new Project instance.

Actual Behavior: An (aggregated) InvalidOperationException:

System.AggregateException was unhandled by user code
  HResult=-2146233088
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
       at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
       at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
       at System.Threading.Tasks.Task`1.get_Result()
       at App1.ProjectWeaver..ctor(String projectFilePath) in C:\Users\Per\proj\RoslynWorkspaceLabs\App1\ProjectWeaver.cs:line 89
       at App1.ProjectFileWeaver.Execute() in C:\Users\Per\proj\RoslynWorkspaceLabs\App1\ProjectWeaver.cs:line 28
       at WeaverTasks.WeaverProtoTask.Execute() in C:\Users\Per\proj\RoslynWorkspaceLabs\WeaverTasks\WeaverProtoTask.cs:line 74
       at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
       at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()
  InnerException: 
       HResult=-2146233079
       Message=Cannot open project 'C:\Users\Per\proj\RoslynWorkspaceLabs\ConsoleApplication1\ConsoleApplication1.csproj' because the language 'C#' is not supported.
       Source=Microsoft.CodeAnalysis.Workspaces.Desktop
       StackTrace:
            at Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.ReportFailure(ReportMode mode, String message, Func`2 createException)
            at Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.TryGetLoaderFromProjectPath(String projectFilePath, ReportMode mode, IProjectFileLoader& loader)
            at Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.<LoadProjectInfoAsync>d__20.MoveNext()
         --- End of stack trace from previous location where exception was thrown ---
            at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
            at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
            at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.<OpenProjectAsync>d__19.MoveNext()
       InnerException: 

More: If I do the same in a separate process / standard application, it works like a charm.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 19 (7 by maintainers)

Most upvoted comments

AdHocWorkspace should work cross-platform. We don’t currently have MSBuildWorkspace or CommandLineProject working cross-platform.

I expect it’s something that we will do at some point, but I’m not exactly sure how that will shake out yet, since both use some Windows specific code (like DesktopAssemblyIdentityComparer in CommandLineProject and we don’t currently use the .NET Core version of MSBuild in MSBuildWorkspace.

@per-samuelsson instead of using MsbuildWorkspace you can use CommandLineProject with AdHocWorkspace. You’ll have to pass all the same arguments into your target that the csc target has and convert them to command line arguments. The result will be the same as if you used MsbuildWorkspace but without using msbuild recursively.