dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.04k stars 4.03k forks source link

MSBuildWorkspace OpenProjectAsync fails under mono #20848

Open vicancy opened 7 years ago

vicancy commented 7 years ago

Version Used: package version: 2.3.0 Mono version: Mono JIT compiler version 5.0.1 (Visual Studio built mono) Steps to Reproduce:

  1. Create a new "Console App (.NET Framework)" ConsoleApp1 in Visual Studio 2017
  2. In Package Manager Console
    install-package Microsoft.Net.Compilers
    install-package Microsoft.CodeAnalysis
  3. Add following line:
    var path = args[0];
    var workspace = MSBuildWorkspace.Create();
    workspace.WorkspaceFailed += (s, e) =>
    {
       Console.WriteLine($"Workspace failed with: {e.Diagnostic}");
    };
    var project = workspace.OpenProjectAsync(path).Result;
  4. Also add bindingRedirect to the app according to https://github.com/dotnet/roslyn/issues/17401#issuecomment-294423882
  5. Create a test project dotnet new classlib -n testproj
  6. Run the code ConsoleApp1.exe testproj\testproj.csproj, works fine, prints 4
  7. Run under mono mono ConsoleApp1.exe testproj\testproj.csproj, fails with "

Expected Behavior: Should pass Actual Behavior: Error message: Workspace failed with: [Failure] Msbuild failed when processing the file 'E:\Repo2\ConsoleApp1\bin\Debug\debug.csproj' with message: Operation is not supported on this platform.

agc93 commented 7 years ago

Any update on this ( @DustinCampbell )? This breaks DocFX on Linux at the moment (unless you run from assemblies instead), which is a pretty big pain for otherwise cross-platform apps :(

abhijitparkhi1983 commented 7 years ago

@DustinCampbell , @Pilchie , @vicancy

Guys I am also having this exact problem. Do we have any update on this? @vicancy , please let me know if you have found any pointer to resolve this issue ?

abhijitparkhi1983 commented 7 years ago

@agc93 , can you please let me know what do u mean by unless you run from assemblies instead? How do we do it ? Please let me know your valuable inputs

agc93 commented 7 years ago

@abhijitparkhi1983 If you add your output dlls in the #/metadata/src key in your docfx.json it will build from your compiled assembly rather than the csproj. For an example, check https://github.com/agc93/Cake.Handlebars/blob/develop/docfx/docfx.json#L7

sttz commented 7 years ago

I'm also affected by this issue, trying to build documentation using docfx on macOS.

I tried building the docs from source but that doesn't work for me because I need to define some constants and that's apparently not possible without a csproj. I also tried building from assemblies but since it's a Unity project, the assemblies don't include documentation.

It seems #21609 is stuck waiting on a stack trace? Since that issue doesn't mention docfx, I post my stack trace here:

{System.PlatformNotSupportedException: Operation is not supported on this platform. at System.Runtime.InteropServices.Marshal.GetTypeFromCLSID (System.Guid clsid) [0x00000] in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/corlib/System.Runtime.InteropServices/Marshal.cs:725 at Microsoft.Build.Shared.VisualStudioLocationHelper.GetQuery () [0x0000a] in <5452a999769d4e6a81de7a3aa0b8f9f1>:0 at Microsoft.Build.Shared.VisualStudioLocationHelper.GetInstances () [0x00006] in <5452a999769d4e6a81de7a3aa0b8f9f1>:0 at Microsoft.Build.Shared.BuildEnvironmentHelper.TryFromSetupApi () [0x00016] in <5452a999769d4e6a81de7a3aa0b8f9f1>:0 at Microsoft.Build.Shared.BuildEnvironmentHelper.Initialize () [0x0007a] in <5452a999769d4e6a81de7a3aa0b8f9f1>:0 at Microsoft.Build.Shared.BuildEnvironmentHelper+BuildEnvironmentHelperSingleton..cctor () [0x00000] in <5452a999769d4e6a81de7a3aa0b8f9f1>:0 } base: {System.NotSupportedException}

System: macOS 10.12.6 (16G29) Visual Studio: 7.2 (build 636) Mono: 5.4.0.201 (2017-06/71277e78f6e) (64-bit)

agc93 commented 7 years ago

@DustinCampbell any movement on this?

maca88 commented 7 years ago

In order to make it work, you must set the MSBUILD_EXE_PATH environment variable. Add the following line before the lines from the third point:

Environment.SetEnvironmentVariable("MSBUILD_EXE_PATH", @"C:\Program Files (x86)\Mono\lib\mono\msbuild\15.0\bin\MSBuild.dll");

To get the correct msbuild path across various operating systems, you can use this helper method.

tzachshabtay commented 6 years ago

Hi, I was getting into the same error when loading my project from mono on mac. Using the answer from @maca88 I was able to get around that, only to get another error:

{[Failure] Msbuild failed when processing the file '/Users/zachi/Projects/MonoAGS/Source/Demo/DemoQuest.Desktop/DemoQuest.Desktop.csproj' with message: The type initializer for 'Microsoft.Build.Collections.MSBuildNameIgnoreCaseComparer' threw an exception.}

I tried to put a "catchpoint" in VS 4 Mac to break on all exceptions, but it doesn't break.

If I put a breakpoint in the "WorkspaceFailed" event, this is the stacktrace:

Microsoft.CodeAnalysis.Workspace.(System.EventHandler<Microsoft.CodeAnalysis.WorkspaceDiagnosticEventArgs> handler) in 
Roslyn.Utilities.EventMap.Registry<System.EventHandler<Microsoft.CodeAnalysis.WorkspaceDiagnosticEventArgs>>.Invoke(System.Action<System.EventHandler<Microsoft.CodeAnalysis.WorkspaceDiagnosticEventArgs>> invoker) in 
Roslyn.Utilities.EventMap.EventHandlerSet<System.EventHandler<Microsoft.CodeAnalysis.WorkspaceDiagnosticEventArgs>>.RaiseEvent(System.Action<System.EventHandler<Microsoft.CodeAnalysis.WorkspaceDiagnosticEventArgs>> invoker) in 
Microsoft.CodeAnalysis.Workspace.OnWorkspaceFailed(Microsoft.CodeAnalysis.WorkspaceDiagnostic diagnostic) in 
Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.OnWorkspaceFailed(Microsoft.CodeAnalysis.WorkspaceDiagnostic diagnostic) in 
Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.ReportFailure(Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.ReportMode mode, string message, System.Func<string,System.Exception> createException) in 
Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader.LoadProjectAsync() in 
System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader stateMachine) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1089
System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader state, bool preserveSyncCtx) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:957
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, Microsoft.CodeAnalysis.MSBuild.MSBuildProjectLoader state, bool preserveSyncCtx) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:904
System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1070
System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, System.Threading.Tasks.Task currentTask) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/TaskContinuation.cs:823
System.Threading.Tasks.Task.FinishContinuations() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:3624
System.Threading.Tasks.Task.FinishStageThree() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2366
System.Threading.Tasks.Task<Microsoft.CodeAnalysis.MSBuild.IProjectFile>.TrySetResult(Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader.CSharpProjectFile result) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Future.cs:488
System.Runtime.CompilerServices.AsyncTaskMethodBuilder<Microsoft.CodeAnalysis.MSBuild.IProjectFile>.SetResult(Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader.CSharpProjectFile result) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:608
Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader.LoadProjectFileAsync() in 
System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader stateMachine) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1089
System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader state, bool preserveSyncCtx) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:957
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader state, bool preserveSyncCtx) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:904
System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1070
System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, System.Threading.Tasks.Task currentTask) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/TaskContinuation.cs:823
System.Threading.Tasks.Task.FinishContinuations() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:3624
System.Threading.Tasks.Task.FinishStageThree() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2366
System.Threading.Tasks.Task<Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader.LoadedProjectInfo>.TrySetResult(Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader.LoadedProjectInfo result) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Future.cs:488
System.Runtime.CompilerServices.AsyncTaskMethodBuilder<Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader.LoadedProjectInfo>.SetResult(Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader.LoadedProjectInfo result) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:608
Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader.LoadProjectAsync() in 
System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader stateMachine) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1089
System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader state, bool preserveSyncCtx) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:957
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader state, bool preserveSyncCtx) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:904
System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1070
System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, System.Threading.Tasks.Task currentTask) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/TaskContinuation.cs:823
System.Threading.Tasks.Task.FinishContinuations() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:3624
System.Threading.Tasks.Task.FinishStageThree() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2366
System.Threading.Tasks.Task<System.IO.MemoryStream>.TrySetResult(System.IO.MemoryStream result) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Future.cs:488
System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.IO.MemoryStream>.SetResult(System.IO.MemoryStream result) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:608
Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader.ReadFileAsync() in 
System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader stateMachine) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1089
System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader state, bool preserveSyncCtx) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:957
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, Microsoft.CodeAnalysis.MSBuild.ProjectFileLoader state, bool preserveSyncCtx) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:904
System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1070
System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, System.Threading.Tasks.Task currentTask) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/TaskContinuation.cs:823
System.Threading.Tasks.Task.FinishContinuations() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:3624
System.Threading.Tasks.Task.FinishStageThree() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2366
System.Threading.Tasks.Task<int>.TrySetResult(int result) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Future.cs:488
System.Threading.Tasks.TaskFactory<int>.FromAsyncTrimPromise<System.IO.Stream>.Complete(System.IO.FileStream thisRef, System.Func<System.IO.Stream,System.IAsyncResult,int> endMethod, System.Runtime.Remoting.Messaging.AsyncResult asyncResult, bool requiresSynchronization) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/FutureFactory.cs:1444
System.Threading.Tasks.TaskFactory<int>.FromAsyncTrimPromise<System.IO.Stream>.CompleteFromAsyncResult(System.Runtime.Remoting.Messaging.AsyncResult asyncResult) in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/FutureFactory.cs:1414
System.Runtime.Remoting.Messaging.AsyncResult.Invoke() in 
System.Runtime.Remoting.Messaging.AsyncResult.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/corlib/System.Runtime.Remoting.Messaging/AsyncResult.cs:210
System.Threading.ThreadPoolWorkQueue.Dispatch() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:856
System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() in /Users/builder/data/lanes/4992/mono-mac-sdk/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:1211

My code is:

var workspace = MSBuildWorkspace.Create();
workspace.WorkspaceFailed += onWorkspaceFailed;
_project = await workspace.OpenProjectAsync(path);

And the project I'm trying to open is: https://github.com/tzachshabtay/MonoAGS/blob/master/Source/Demo/DemoQuest.Desktop/DemoQuest.Desktop.csproj

Please help, thanks.

tzachshabtay commented 6 years ago

So based on the @vicancy 's comment from https://github.com/dotnet/docfx/issues/2358, I tried to downgrade System.Runtime.InteropServices.RuntimeInformation from 4.3.0 to 4.0.0.

I'm now getting a new error and I want to cry:

[Failure] Msbuild failed when processing the file '/Users/zachi/Projects/MonoAGS/Source/Demo/DemoQuest.Desktop/DemoQuest.Desktop.csproj' with message: GetEnvironmentStrings

I'm guessing this has something to do with https://github.com/Microsoft/msbuild/issues/1987 ?

asoifer commented 6 years ago

I can't open any solution with mono either.. could someone?