docfx crashes for trivial .NETStandard lib
Operating System: Linux
DocFX Version Used: 2.56.1.0
Template used:
Steps to Reproduce:
- cd $(mktemp -d)
- mono /path/to/docfx.exe init -q
- cd src
- dotnet new classlib
- cd ..
- mono /path/to/docfx.exe docfx.json
Expected Behavior:
The documentation will be created
Actual Behavior:
docfx crashes
# docfx docfx.json
[20-11-23 06:41:57.476]Info:[MetadataCommand.ExtractMetadata]Using mono /usr/lib/mono/msbuild/15.0/bin/MSBuild.dll as inner compiler.
[20-11-23 06:41:57.628]Info:[MetadataCommand.ExtractMetadata]Loading projects...
[20-11-23 06:41:58.113]Warning:[MetadataCommand.ExtractMetadata](/posix/docfx_project/src/src.csproj)Error opening project /posix/docfx_project/src/src.csproj: The type initializer for 'Microsoft.Build.Collections.MSBuildNameIgnoreCaseComparer' threw an exception.. Ignored.
[20-11-23 06:41:58.120]Info:[MetadataCommand]Completed Scope:MetadataCommand in 673.7855 milliseconds.
[20-11-23 06:41:58.120]Error:Error extracting metadata for /posix/docfx_project/src/src.csproj: System.NullReferenceException: Object reference not set to an instance of an object
at Microsoft.DocAsCode.Metadata.ManagedReference.ExtractMetadataWorker.SaveAllMembersFromCacheAsync () [0x00380] in <2892d9caf6b8486aa4cae3e30d450aa8>:0
at Microsoft.DocAsCode.Metadata.ManagedReference.ExtractMetadataWorker.ExtractMetadataAsync () [0x000c0] in <2892d9caf6b8486aa4cae3e30d450aa8>:0
[20-11-23 06:41:58.120]Info:Completed in 676.799 milliseconds
Build failed.
[20-11-23 06:41:58.123]Warning:[MetadataCommand.ExtractMetadata](/posix/docfx_project/src/src.csproj)Error opening project /posix/docfx_project/src/src.csproj: The type initializer for 'Microsoft.Build.Collections.MSBuildNameIgnoreCaseComparer' threw an exception.. Ignored.
[20-11-23 06:41:58.123]Error:Error extracting metadata for /posix/docfx_project/src/src.csproj: System.NullReferenceException: Object reference not set to an instance of an object
at Microsoft.DocAsCode.Metadata.ManagedReference.ExtractMetadataWorker.SaveAllMembersFromCacheAsync () [0x00380] in <2892d9caf6b8486aa4cae3e30d450aa8>:0
at Microsoft.DocAsCode.Metadata.ManagedReference.ExtractMetadataWorker.ExtractMetadataAsync () [0x000c0] in <2892d9caf6b8486aa4cae3e30d450aa8>:0
1 Warning(s)
1 Error(s)
It looks like it's similar to #5508, but I'm not sure.
I'm using the docker container https://hub.docker.com/r/nunitdocs/docfx-action for https://github.com/nunit/docfx-action to run it.
We are experiencing something similar with a .NET Standard lib and have pinpointed the issue to whether or not the build agent has .NET Framework 4.8 installed on it or not. If we install Visual Studio Build Tools 2019 with the following load:
./vs_BuildTools.exe --add Microsoft.VisualStudio.Workload.MSBuildTools --add Microsoft.Component.PythonTools --add Microsoft.Component.PythonTools.Web --add Microsoft.VisualStudio.Workload.NetCoreBuildTools --add Microsoft.Net.Component.4.6.1.SDK --add Microsoft.Net.Component.4.6.1.TargetingPack --add Microsoft.Net.Component.4.7.SDK --add Microsoft.Net.Component.4.7.TargetingPack --add Microsoft.Net.Component.4.7.1.TargetingPack --add Microsoft.Net.Component.4.7.1.SDK --add Microsoft.VisualStudio.Workload.WebBuildTools --add Microsoft.VisualStudio.Workload.OfficeBuildTools --add Microsoft.Net.Component.4.8.SDK --add Microsoft.Net.Component.4.8.TargetingPack --quiet --wait --includeRecommended --norestart --force | Out-Null
we get similar errors to above, whereas if we remove the .NET 4.8 SDK and TargetingPack, it happily produces the documentation. The error obtained is:
[21-01-18 10:52:54.041]Info:[MetadataCommand]Completed Scope:MetadataCommand in 9288.7782 milliseconds.
[21-01-18 10:52:54.043]Error:Error extracting metadata for C:/Jenkins/Workspace/workspace/projects.csproj: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.DocAsCode.Metadata.ManagedReference.ExtractMetadataWorker.<SaveAllMembersFromCacheAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.DocAsCode.Metadata.ManagedReference.ExtractMetadataWorker.<ExtractMetadataAsync>d__11.MoveNext()
[21-01-18 10:52:54.043]Info:Completed in 9299.9922 milliseconds
The build log also shows the following warning when building the docs:
[21-01-18 10:52:54.084]Warning:[MetadataCommand.ExtractMetadata]Fail to find MSBuild >= 15.0 on machine. Please install Visual Studio 2017 or above with MSBuild >= 15.0: https://visualstudio.microsoft.com/vs/
which it doesn't when the .NET 4.8 pack is not installed.
Hi all - the initial issue was logged end of November, is there anyone looking at this?
@rlordcardano As my experience with Microsoft OpenSource is, the stalebot will close this issue 12 months after the last comment without any other notice by Microsoft.
My hope is that docfx will support .NET Core 3.1 or .NET 5.0 extremely soon.
@carstencodes - do you reckon it would work with .NET Core 3.0? We had it working with .NET Core 3.1 for a while, generating documentation for a few .NET Standard DLLs, but noticed that as soon as .NET Framework 4.8 was installed on the build agent, it stopped to work with similar errors to the ones you reported.
@rlordcardano IMHO they support .NET Core, but not as a runtime for the generator, this has to be the full framework or mono.
I don't know if it's useful information or not, but just running the "extract metadata" step on the C# code in https://github.com/protocolbuffers/protobuf works on my dev machine which does have .NET 4.8 installed on it, but fails on a CI machine (where I don't currently know what's installed). I'm still looking into it...
The issue @rlordcardano mentioned is fixed now.
TLDR: setting environment variables fixes the issue, I assume this is a workaround but why we need it is still a mystery to me.
VSINSTALLDIR="C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools"
VisualStudioVersion="16.0"
Full version:
Our jenkins agents don't have Visual Studio installed, but we have build tools 2019. Somehow docfx build was showing wrong msbuild path
[21-02-03 07:17:51.110]Info:[MetadataCommand.ExtractMetadata]Using msbuild C:\Program Files (x86)\Microsoft Visual Studio\2019\TestAgent\MSBuild\Current\Bin as inner compiler.
I managed to reproduce the issue using docker image mcr.microsoft.com/dotnet/framework/sdk:4.8 not locally but on the build agent. I guess this is due to the injected environment variables as this is the only common part between execution on agent and in the container.
setting MSBUILD_EXE_PATH also hides the problem due to
https://github.com/dotnet/docfx/blob/920dd053453999acfd93b4d171f55074058a9aff/src/Microsoft.DocAsCode.Metadata.ManagedReference/MsBuildEnvironmentScope.cs#L29-L33
In our case below code was not finding any instances of MSBuild https://github.com/dotnet/docfx/blob/920dd053453999acfd93b4d171f55074058a9aff/src/Microsoft.DocAsCode.Metadata.ManagedReference/MsBuildEnvironmentScope.cs#L64
Interestingly if we set VSINSTALLDIR and VisualStudioVersion as above everything is back to normal. https://github.com/dotnet/docfx/blob/920dd053453999acfd93b4d171f55074058a9aff/src/Microsoft.DocAsCode.Metadata.ManagedReference/MsBuildEnvironmentScope.cs#L75-L78
I had to debug docfx on our build agent to reveal more information about the problem with some added error handling. Original docfx nicely hides all the useful Roselyn errors. I would suggest loader exception messages to be returned as part of the exception.
I guess better error handling in the below method may help others as well: https://github.com/dotnet/docfx/blob/920dd053453999acfd93b4d171f55074058a9aff/src/Microsoft.DocAsCode.Metadata.ManagedReference/ExtractMetadataWorker.cs#L769-L795
I don't know if it's useful information or not, but just running the "extract metadata" step on the C# code in https://github.com/protocolbuffers/protobuf works on my dev machine which does have .NET 4.8 installed on it, but fails on a CI machine (where I don't currently know what's installed). I'm still looking into it...
Looks like similar issue we were having, @ferdaarikan solved it for us, see above.
Ok, does anyone have an idea what to do on Linux / mono?
@carstencodes you can get the docfx source code and run/debug under your environment. I had a look at the docfx code here and you may also have an issue with msbuild version/location.
The code for mono is looking for msbuild 15.0, to understand the issue check if you have correct msbuild in the location below
var assembly = typeof(System.Runtime.GCSettings).Assembly;
var assemblyDirectory = Path.GetDirectoryName(assembly.Location);
var monoDir = new DirectoryInfo(assemblyDirectory).Parent.FullName; // get mono directory
var msbuildBasePath = Path.Combine(monoDir, "msbuild", "15.0", "bin");
var msbuildPath = Path.Combine(msbuildBasePath, "MSBuild.dll");
I guess you can safely run above snippet in a console app in your environment
According to the log above, this is already the case - and it crashes
Since this is not picked up yet I will try to help as a user of docfx.
Yes @carstencodes I didn't see that in your logs before it indeed says /usr/lib/mono/msbuild/15.0/bin/MSBuild.dll. I am not sure if you are using newer language features that aren't supported by msbuild 15.0
I guess you could try to narrow down the problem to its source. I would try to see if there is an actual problem with the project /posix/docfx_project/src/src.csproj or other components.
You can do this by using a simpler csproj from the solution or create a new almost empty project. If you can ensure that the loading error occurs with any project then it must still be somehow msbuild/roslyn issue. Such as failing to load a dependency.
You can easily checkout docfx source compile it and execute the docfx binary with your docfx.json if you are using one. Make sure you have a better error handling in the part I mention in my previous comment (assuming yours also throws there). Make sure you handle loader errors and go through messages. That will give a better pointer on what is wrong.
Hi @ferdaarikan
Yeah, I tried it at first with my project and my solution.
Unfortunately, it crashed. So I narrowed it down to the most simple project which is an empty .NET Standard classlib.
As you can see, it does not work.
If it is working for Windows based machines (I don't have one to test it), it might be a mono specific issue. If this is the case, the docfx team should not tell, that docfx runs with mono. It obviously does not.
Or this is an issue in docfx that needs to be fixed.
So maybe the only chance is to wait for .NET core 3.1 / .NET 5.0 or .NET 6.0 revision of docfx.
Maybe you already did but how about compiling docfx in your own environment. That should tell you exactly what the problem is as you can handle the hidden error messages I mention in my above comment.
I don't use mono but still I tried to see what happens out of curiosity and seems to be fine for me. I tested this on Ubuntu 20.04 with msbuild for mono.

@yufeih Is there an associate pull request ?
The upgrade to .NET core in the latest releases fixed the problems of loading SDK style projects.