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
212 stars 83 forks source link

upgrade to net6.0 version + add explicit apphost loading for specific platforms #224

Closed YuliiaKovalova closed 11 months ago

YuliiaKovalova commented 11 months ago

Problem

Fixes: #https://github.com/dotnet/msbuild/issues/9038

MSBuildLocator expects that hostfxr lib is already loaded in the process - that is true for Windows load library, but not for all platforms (macOS, for example). Some of our customers face an issue like:


07/17/2023 15:48:47 Greenwich Mean Time: Error : 9543 :!!Resolve NET Core SDK: Failed
System.DllNotFoundException: Unable to load shared library 'hostfxr' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: 
dlopen(/usr/local/share/dotnet/shared/Microsoft.NETCore.App/7.0.9/hostfxr.dylib, 1): image not found
dlopen(/Users/timjroberts/.vscode/extensions/ms-dotnettools.csdevkit-0.2.100-darwin-x64/components/CPS/platforms/darwin-x64/node_modules/@microsoft/visualstudio-projectsystem-buildhost.darwin-x64/hostfxr.dylib, 1): image not found
dlopen(hostfxr.dylib, 1): image not found
dlopen(/usr/local/share/dotnet/shared/Microsoft.NETCore.App/7.0.9/libhostfxr.dylib, 1): image not found
dlopen(/Users/timjroberts/.vscode/extensions/ms-dotnettools.csdevkit-0.2.100-darwin-x64/components/CPS/platforms/darwin-x64/node_modules/@microsoft/visualstudio-projectsystem-buildhost.darwin-x64/libhostfxr.dylib, 1): image not found
dlopen(libhostfxr.dylib, 1): image not found
dlopen(/usr/local/share/dotnet/shared/Microsoft.NETCore.App/7.0.9/hostfxr, 1): image not found
dlopen(/Users/timjroberts/.vscode/extensions/ms-dotnettools.csdevkit-0.2.100-darwin-x64/components/CPS/platforms/darwin-x64/node_modules/@microsoft/visualstudio-projectsystem-buildhost.darwin-x64/hostfxr, 1): image not found
dlopen(hostfxr, 1): image not found
dlopen(/usr/local/share/dotnet/shared/Microsoft.NETCore.App/7.0.9/libhostfxr, 1): image not found
dlopen(/Users/timjroberts/.vscode/extensions/ms-dotnettools.csdevkit-0.2.100-darwin-x64/components/CPS/platforms/darwin-x64/node_modules/@microsoft/visualstudio-projectsystem-buildhost.darwin-x64/libhostfxr, 1): image not found
dlopen(libhostfxr, 1): image not found

Solution

Microsoft.NETCore.DotNetAppHost is a metadata package that loads a platform-specific assembly in runtime. Specifying reference to it explicitly <PackageReference Include="runtime.$(RuntimeIdentifier).Microsoft.NETCore.DotNetAppHost" Version="7.0.9" /> helps to copy the content in project/bin folder , so missed library can be resolved.

YuliiaKovalova commented 11 months ago

@ladipro , @rainersigwald is it ok that we have started to generate this folder in output? image

runtime content image

How will it be shipped?

ladipro commented 11 months ago

@YuliiaKovalova this looks expected, the native libraries should not be taking too much extra space.

How will it be shipped?

It should become part of the Microsoft.Build.Locator package.

ladipro commented 11 months ago

Also, is it possible to cover the P/Invoke with a test? Something that would have previously failed on Mac or Linux.

Actually, it should still be failing because we're currently not making the call to nethost!get_hostfxr_path. Or are the current changes enough to fix the issue? I hope I'm not misunderstanding the changes.

YuliiaKovalova commented 11 months ago

Also, is it possible to cover the P/Invoke with a test? Something that would have previously failed on Mac or Linux.

Actually, it should still be failing because we're currently not making the call to nethost!get_hostfxr_path. Or are the current changes enough to fix the issue? I hope I'm not misunderstanding the changes.

It works as expected now because the source issue with the missed lib is resolved. Previously, it was failing here https://github.com/YuliiaKovalova/MSBuildLocator/blob/a7e81c92e079900d3403e71542c59d6cc767daaf/src/MSBuildLocator/DotNetSdkLocationHelper.cs#L116C36-L116C56 with

07/17/2023 15:48:47 Greenwich Mean Time: Error : 9543 :!!Resolve NET Core SDK: Failed System.DllNotFoundException: Unable to load shared library 'hostfxr' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(/usr/local/share/dotnet/shared/Microsoft.NETCore.App/7.0.9/hostfxr.dylib, 1): image not found dlopen(/Users/timjroberts/.vscode/extensions/ms-dotnettools.csdevkit-0.2.100-darwin-x64/components/CPS/platforms/darwin-x64/node_modules/@microsoft/visualstudio-projectsystem-buildhost.darwin-x64/hostfxr.dylib, 1): image not found dlopen(hostfxr.dylib, 1): image not found dlopen(/usr/local/share/dotnet/shared/Microsoft.NETCore.App/7.0.9/libhostfxr.dylib, 1): image not found dlopen(/Users/timjroberts/.vscode/extensions/ms-dotnettools.csdevkit-0.2.100-darwin-x64/components/CPS/platforms/darwin-x64/node_modules/@microsoft/visualstudio-projectsystem-buildhost.darwin-x64/libhostfxr.dylib, 1): image not found dlopen(libhostfxr.dylib, 1): image not found dlopen(/usr/local/share/dotnet/shared/Microsoft.NETCore.App/7.0.9/hostfxr, 1): image not found dlopen(/Users/timjroberts/.vscode/extensions/ms-dotnettools.csdevkit-0.2.100-darwin-x64/components/CPS/platforms/darwin-x64/node_modules/@microsoft/visualstudio-projectsystem-buildhost.darwin-x64/hostfxr, 1): image not found dlopen(hostfxr, 1): image not found dlopen(/usr/local/share/dotnet/shared/Microsoft.NETCore.App/7.0.9/libhostfxr, 1): image not found dlopen(/Users/timjroberts/.vscode/extensions/ms-dotnettools.csdevkit-0.2.100-darwin-x64/components/CPS/platforms/darwin-x64/node_modules/@microsoft/visualstudio-projectsystem-buildhost.darwin-x64/libhostfxr, 1): image not found dlopen(libhostfxr, 1): image not found

There is no need to request the path.

ladipro commented 11 months ago

That's odd, it looks like we're not even loading the nethost library so it's not clear how it makes the call to hostfxr succeed 🤔