Home General

Unable to use Microsoft.CodeAnalysis.Workspaces.MSBuild package

Trying to analyze my solution using the code analyzers. So wrote up a simple script:

async Task Main()
{
    var solutionPath = @"path to .sln file";
    await AnalyzeSolution(solutionPath);
}

async Task AnalyzeSolution(string solutionPath)
{
    var progress = SimpleProgress.Create((ProjectLoadProgress p) =>
    {
        $"{p.Operation}: {p.FilePath} ({p.TargetFramework})".Dump();
    });

    using var ws = MSBuildWorkspace.Create();
    var sln = await ws.OpenSolutionAsync(solutionPath, progress);
    ...
}

public static class SimpleProgress
{
    public static SimpleProgress<T> Create<T>(Action<T> report) => new(report);
}

public class SimpleProgress<T>(Action<T> report) : IProgress<T>
{
    public void Report(T value) => report?.Invoke(value);
}

Trying to run it, if fails with an error:

The build host could not be found at 'C:\Users\jeff.mercado\.nuget\packages\Microsoft.CodeAnalysis.Workspaces.MSBuild\4.13.0\lib\net9.0\BuildHost-netcore\Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.dll'

I've looked through the code and it uses the assembly's path to determine where the build host is which works in a regular app since the assemblies are put in a same place, including the build host binaries.

Since LINQPad is referencing the files directly from the nuget cache, I guess a build step needs to be performed to put the files there (which I can do to work around this).

Is this something that could be fixed?

Comments

Sign In or Register to comment.