I'm currently trying to compile a solution during runtime execution of my own program. This solution is user defined and must have support for any number of projects and in these any nuget dependency the user wants to add. For example, a solution with a singular C# project that is defined like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>
</Project>
should compile if the user decides to use xunit or Newtonsoft.Json classes in their code.
I am currently compiling the projects with the following steps:
using var workspace = MSBuildWorkspace.Create();
var solution = await workspace.OpenSolutionAsync(solutionPath, cancellationToken: token);
foreach (var project in solution.Projects)
{
var compilation = await project.GetCompilationAsync(token);
// handle compilation
}
The issue I'm facing is that it seems that MSBuild is not resolving all nuget references. This is quite odd because Newtonsoft.Json can be used, and adding references to dlls using hint paths does work as well. However, xunit is never resolved and the compilation fails with the message that the namespace Xunit cannot be found. compilation.References only shows System references as well as for some reason always Newtonsoft.Json, even if it's not referenced. workspace.Diagnostics is empty and I'm at a loss.
How can I get MSBuild to include the nuget packages my users will reference?