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

.net 5 works poorly with latest preview prerelase. #103

Closed powercode closed 3 years ago

powercode commented 3 years ago

When using a XUnit test project, System.Numerics.Vectors and System.Runtime.CompilerServices.Unsafe are already loaded in the process when the a module initializer is run. That is as early as can be.

    public static class ModInit
    {
        [ModuleInitializer]
        public static void Init()
        {
            try
            {               
                if (!MSBuildLocator.IsRegistered)
                {
                    MSBuildLocator.RegisterDefaults();
                }
            }
            catch (InvalidOperationException ex)
            {
                System.Diagnostics.Trace.WriteLine(ex);
            }
        }
    }
Microsoft.Build.Locator.MSBuildLocator.RegisterInstance was called, but MSBuild assemblies were already loaded.
Ensure that RegisterInstance is called before any method that directly references types in the Microsoft.Build namespace has been called.
This dependency arises from when a method is just-in-time compiled, so if it breaks even if the reference to a Microsoft.Build type has not been executed.
For more details, see aka.ms/RegisterMSBuildLocator
Loaded MSBuild assemblies: System.Numerics.Vectors, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Runtime.CompilerServices.Unsafe, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

So having these assemblies in the vector

        private static readonly string[] s_msBuildAssemblies =
        {
            "Microsoft.Build",
            "Microsoft.Build.Engine",
            "Microsoft.Build.Framework",
            "Microsoft.Build.Tasks.Core",
            "Microsoft.Build.Utilities.Core",
            "System.Runtime.CompilerServices.Unsafe",
            "System.Numerics.Vectors"
        };

seems like a bad idea.

If they are needed as a dependency, that should be separated from if there are msbuild assemblies loaded in the process.

powercode commented 3 years ago

94 Should be reverted and fixed in some other way.

It must be OK to call Microsoft.Build.Locator.MSBuildLocator.RegisterInstance with System.Runtime.CompilerServices.Unsafe already loaded, which that change disallowed.

@EWSoftware

yaakov-h commented 3 years ago

On Visual Studio v16.8.2 now it crashes failing to load System.Collections.Immutable v5.0.0.0.

An alternative fix to #94 needs to take into account that the assemblies we need to load changes dynamically with different releases of VS.