microsoft / MSBuildLocator

An API to locate MSBuild assemblies from an installed Visual Studio location. Use this to ensure that calling the MSBuild API will use the same toolset that a build from Visual Studio or msbuild.exe would.
Other
216 stars 83 forks source link

[Question] Where to report an issue with MSBuildWorkspace together with MSBuildLocator? #104

Closed ceztko closed 3 years ago

ceztko commented 3 years ago

Sorry for leaving a comment in probably the inappropriate project. I'm having an issue with the latest Visual Studio 16.8.x where I'm not able to compile a csharp project with the MSBuildWorkspace anymore. The code is as follow:

            MSBuildLocator.RegisterDefaults();
            MSBuildWorkspace workspace = MSBuildWorkspace.Create();
            Project project = await workspace.OpenProjectAsync(@"D:\Test\SimpleLibrary\SimpleNETStandard20.csproj");
            var compilation = project.GetCompilationAsync().Result!;

With VisualStudio 16.8 and .NET 5.0 sdk trying to build a .NET standard 2.0 project the compilation fails with error cs5001 (missing static 'main' method, but the project is a library), meaning that probably the SDK is misconfigured. Trying to build a .NET Framework 4.x project it fails even worse basically being unable to locate a BCL library (it can't find most simple base types). Usually MSBuildLocator.RegisterDefaults() was the solution for this issues but with VS 16.8 it's not enough. I had to revert to VS 16.7.8 and the problem disappeared. Where I should report this issue? Maybe it isalready reported somewhere?

rainersigwald commented 3 years ago

This is probably as good a place as any. Can you share a repro project? Can you configure the workspace to collect a binlog and share that?

KirillOsenkov commented 3 years ago

@ceztko you can't have MSBuildLocator.RegisterDefaults() in the same method as other MSBuild APIs. By the time the method is JITted, the MSBuild assemlies will already have been loaded from the wrong location, so the MSBuild Locator is ineffective.

Try extracting the actual DoWork() method and only call MSBuildLocator.RegisterDefaults(); from Main() followed by a call to DoWork():

static void Main()
{
    MSBuildLocator.RegisterDefaults();
    DoWork();
}

Almost feels like maybe we should have a sample on the front page.

KirillOsenkov commented 3 years ago

See an example here: https://github.com/dotnet/msbuild/issues/3434#issuecomment-774280523

ceztko commented 3 years ago

@KirillOsenkov thank you very much for the suggestion. Actually the issue fixed itself building the same project targeting .NET Framework 4.5.2 after some workspace cleanup, but it's persisting trying to compile the same project targeting. I opened the issue https://github.com/dotnet/msbuild/issues/6283 for that (I can reproduce on an hello world class library), since I'm not sure if it's related to the MSBuildLocator or more generically to MSBuild. About your suggestion to move actual work to a DoWork method to be called after calling MSBuildLocator.RegisterDefaults(): why you don't also specify the method to not be inlined with [MethodImpl(MethodImplOptions.NoInlining)]? Without that the JIT compiler could be free to inline it. Anyway, I close this issue since possibly I don't have issues with MSBuildLocator.