Search code examples
c#msbuilddocfx

Trying to use DocFX results in error "No instances of MSBuild could be detected"


I've created a docFx project using the commandline wizard. When I try to render the documentation with docfx ./docfx.json I get the following error:

ExtractMetadataException: No instances of MSBuild could be detected.
Try calling RegisterInstance or RegisterMSBuildPath to manually register one.
     InvalidOperationException: No instances of MSBuild could be detected.
     Try calling RegisterInstance or RegisterMSBuildPath to manually register one.
       at VisualStudioInstance RegisterDefaults()
       at void EnsureMSBuildLocator() in DotnetApiCatalog.cs:126
  at void EnsureMSBuildLocator() in DotnetApiCatalog.cs:134
  at async Task Exec(MetadataJsonConfig config, DotnetApiOptions options, string configDirectory, string outputDirectory) in DotnetApiCatalog.cs:60
  at void <Execute>b__0() in DefaultCommand.cs:45
  at int Run(LogOptions options, Action run) in CommandHelper.cs:48
  at int Execute(CommandContext context, Options options) in DefaultCommand.cs:31
  at Task<int> Execute(CommandContext context, CommandSettings settings) in CommandOfT.cs:40
  at async Task<int> Execute(CommandTree leaf, CommandTree tree, CommandContext context, ITypeResolver resolver, IConfiguration configuration) in CommandExecutor.cs:166

This is the output of dotnet --list-sdks:

7.0.403 [C:\Program Files\dotnet\sdk]

The target project uses .NET 7.0, and it compiles (it's a newly created project, empty save for the default "Hello world!" print)

This is the docfx.json file:

{
  "metadata": [
    {
      "src": [
        {
          "src": "C:/Users/my_user/Repos/Tests/docfx_test/dotnet_project/dotnet_project",
          "files": [
            "**/*.csproj"
          ]
        }
      ],
      "dest": ""
    }
  ],
  "build": {
    "content": [
      {
        "files": [
          "**/*.{md,yml}"
        ],
        "exclude": [
          "_site/**"
        ]
      }
    ],
    "resource": [
      {
        "files": [
          "images/**"
        ]
      }
    ],
    "output": "_site",
    "template": [
      "default",
      "modern"
    ],
    "globalMetadata": {
      "_appName": "TestDocumentation",
      "_appTitle": "TestDocumentation",
      "_enableSearch": true,
      "pdf": true
    }
  }
}

Solution

  • It works now, but I'm not sure why. I tried uninstalling all .NET runtimes except 6.0, it didn't work. I then reinstalled version 7.0, it still didn't work. I installed 8.0, didn't work.

    After that I uninstalled and reinstalled docfx itself and now it works. I'm not sure which combination of reinstalls did the trick, but clearly something had not been properly initialized when I first installed docfx.