diff --git a/README.md b/README.md index 6f9a58c..4dbe262 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ That additional build logic is distributed with Visual Studio, with Visual Studi Loading MSBuild from Visual Studio also ensures that your application gets the same view of projects as `MSBuild.exe`, `dotnet build`, or Visual Studio, including bug fixes, feature additions, and performance improvements that may come from a newer MSBuild release. -## How Locator searches for .NET SDK? +## How does Locator search for the .NET SDK? MSBuild.Locator searches for the locally installed SDK based on the following priority: diff --git a/docs/diagnostics/MSBL001.md b/docs/diagnostics/MSBL001.md new file mode 100644 index 0000000..2c2ab5b --- /dev/null +++ b/docs/diagnostics/MSBL001.md @@ -0,0 +1,66 @@ +# MSBL001 - MSBuild runtime package is referenced without Runtime Assets excluded + +## Error Message + +> A PackageReference to the package '{PackageId}' at version '{Version}' is present in this project without ExcludeAssets="runtime" set. This can cause errors at run-time due to MSBuild assembly-loading. + +## Cause + +This error occurs when your project references MSBuild NuGet packages (such as `Microsoft.Build`, `Microsoft.Build.Framework`, `Microsoft.Build.Utilities.Core`, etc.) without excluding their runtime assets. When you use Microsoft.Build.Locator, you want MSBuildLocator to load MSBuild assemblies from an installed Visual Studio or .NET SDK instance, not from the NuGet packages in your output directory. + +## Why This Is a Problem + +When MSBuild runtime assemblies are copied to your application's output directory, your application may load these assemblies instead of the MSBuild installation that MSBuildLocator registered. This can lead to several runtime issues: + +1. **Assembly version conflicts**: Your application loads MSBuild assemblies from your output directory while MSBuildLocator tries to load from a different MSBuild installation +2. **Missing SDKs and build logic**: The MSBuild assemblies in your output directory don't include the SDKs, targets, and build logic needed to build real projects +3. **Inconsistent behavior**: Your application may behave differently than `MSBuild.exe`, `dotnet build`, or Visual Studio when evaluating projects + +## Example Runtime Error + +Without `ExcludeAssets="runtime"`, you may encounter errors like: + +``` +System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. +``` + +Or: + +``` +System.InvalidOperationException: SDK 'Microsoft.NET.Sdk' could not be resolved. The SDK resolver "Microsoft.DotNet.MSBuildSdkResolver" returned null. +``` + +This happens because your application loads MSBuild assemblies from your bin folder (e.g., version 15.5.180) while MSBuildLocator has registered a different MSBuild installation (e.g., version 17.0) to use at runtime. The .NET runtime gets confused about which assemblies to use, leading to version conflicts and missing dependencies. + +## Solution + +Add `ExcludeAssets="runtime"` to all MSBuild PackageReferences in your project file: + +```xml + + + + + ... + +``` + +This tells NuGet to use these packages only for compilation, not at runtime. At runtime, MSBuildLocator will load MSBuild assemblies from the registered Visual Studio or .NET SDK installation. + +## Alternative: Disable the Check (Not Recommended) + +If you need to distribute MSBuild assemblies with your application (not recommended), you can disable this check by setting the following property in your project file: + +```xml + + true + +``` + +**Warning**: Disabling this check means you must distribute all of MSBuild and its associated toolset with your application, which is generally not recommended. The MSBuild team does not support this scenario, and you may encounter issues with SDK resolution and build logic. + +## Related Documentation + +- [Use Microsoft.Build.Locator](https://learn.microsoft.com/visualstudio/msbuild/updating-an-existing-application#use-microsoftbuildlocator) +- [MSBuildLocator on GitHub](https://github.com/microsoft/MSBuildLocator) + diff --git a/src/MSBuildLocator/Microsoft.Build.Locator.csproj b/src/MSBuildLocator/Microsoft.Build.Locator.csproj index 758800a..318078f 100644 --- a/src/MSBuildLocator/Microsoft.Build.Locator.csproj +++ b/src/MSBuildLocator/Microsoft.Build.Locator.csproj @@ -12,6 +12,7 @@ MSBuild Locator Package that assists in locating and using a copy of MSBuild installed as part of Visual Studio 2017 or higher or .NET Core SDK 2.1 or higher. msbuildlocator;locator;buildlocator + README.md true 1.6.1 true @@ -25,14 +26,11 @@ - - Never - build\ - - - Never - build\ - + + + + + diff --git a/src/MSBuildLocator/README.md b/src/MSBuildLocator/README.md new file mode 100644 index 0000000..65bca12 --- /dev/null +++ b/src/MSBuildLocator/README.md @@ -0,0 +1,57 @@ +# Microsoft.Build.Locator + +Microsoft.Build.Locator helps you locate and register MSBuild assemblies provided with Visual Studio or the .NET SDK. This is essential when you need to use MSBuild APIs in your application to evaluate or build projects. + +## Why do I need this? + +When using MSBuild's .NET API to load and build projects, you need access to the SDKs and build logic distributed with Visual Studio or the .NET SDK, not just the MSBuild APIs. MSBuildLocator helps you find these installations and set up your application to use them, ensuring your code gets the same view of projects as `MSBuild.exe`, `dotnet build`, or Visual Studio. + +## Quick Start + +Before using any MSBuild APIs, register MSBuild assemblies: + +```csharp +using Microsoft.Build.Locator; +using Microsoft.Build.Evaluation; + +// Register defaults before using any MSBuild types +MSBuildLocator.RegisterDefaults(); + +// Now you can safely use MSBuild APIs. +// NOTE: due the the way that the CLR loads assemblies, you MUST +// register MSBuild through Locator before any types from +// the MSBuild assemblies are used in your application. +// The safest way to ensure this is to put any MSBuild API +// access into a separate method. +LoadProject(); + +void LoadProject() +{ + var project = new Project("MyProject.csproj"); + ... +} +``` + +For more control over which MSBuild instance to use: + +```csharp +using Microsoft.Build.Locator; + +// Query available MSBuild instances +var instances = MSBuildLocator.QueryVisualStudioInstances().ToArray(); + +// Register a specific instance +var instance = instances.OrderByDescending(i => i.Version).First(); +MSBuildLocator.RegisterInstance(instance); +``` + +## Documentation + +For complete documentation, see [Use Microsoft.Build.Locator](https://learn.microsoft.com/visualstudio/msbuild/updating-an-existing-application#use-microsoftbuildlocator) on Microsoft Learn. + +## Samples + +See the [BuilderApp](https://github.com/microsoft/MSBuildLocator/blob/a349ee7ffd889cd7634d3fd8b413bf9f29244b50/samples/BuilderApp) sample for a full +exploration of the MSBuildLocator library and capabilities. + + diff --git a/src/MSBuildLocator/build/Microsoft.Build.Locator.targets b/src/MSBuildLocator/build/Microsoft.Build.Locator.targets index 35d55c3..6cbe80d 100644 --- a/src/MSBuildLocator/build/Microsoft.Build.Locator.targets +++ b/src/MSBuildLocator/build/Microsoft.Build.Locator.targets @@ -1,25 +1,26 @@  - + - + <_MSBuildAssembliesCopyLocalItems Include="@(RuntimeCopyLocalItems->WithMetadataValue('NuGetPackageId', 'Microsoft.Build'))" /> + <_MSBuildAssembliesCopyLocalItems Include="@(RuntimeCopyLocalItems->WithMetadataValue('NuGetPackageId', 'Microsoft.Build.Framework'))" /> + <_MSBuildAssembliesCopyLocalItems Include="@(RuntimeCopyLocalItems->WithMetadataValue('NuGetPackageId', 'Microsoft.Build.Utilities.Core'))" /> + <_MSBuildAssembliesCopyLocalItems Include="@(RuntimeCopyLocalItems->WithMetadataValue('NuGetPackageId', 'Microsoft.Build.Tasks.Core'))" /> + <_MSBuildAssembliesCopyLocalItems Include="@(RuntimeCopyLocalItems->WithMetadataValue('NuGetPackageId', 'Microsoft.Build.Engine'))" /> + <_MSBuildAssembliesCopyLocalItems Include="@(RuntimeCopyLocalItems->WithMetadataValue('NuGetPackageId', 'Microsoft.Build.Conversion.Core'))" /> + <_MSBuildAssembliesCopyLocalItems Include="@(RuntimeCopyLocalItems->WithMetadataValue('NuGetPackageId', 'Microsoft.Build.Runtime'))" /> + <_MSBuildAssembliesCopyLocalItems Include="@(RuntimeCopyLocalItems->WithMetadataValue('NuGetPackageId', 'Microsoft.Build.Localization'))" /> + <_MSBuildAssembliesCopyLocalItems Include="@(RuntimeCopyLocalItems->WithMetadataValue('NuGetPackageId', 'Microsoft.Build.Engine'))" /> + <_MSBuildAssembliesCopyLocalItems Include="@(RuntimeCopyLocalItems->WithMetadataValue('NuGetPackageId', 'Microsoft.NET.StringTools'))" /> + <_MSBuildAssembliesCopyLocalItems Include="@(RuntimeCopyLocalItems->WithMetadataValue('NuGetPackageId', 'NuGet.Frameworks'))" /> + + <_DistinctMSBuildPackagesReferencedPoorly Include="@(MSBuildAssembliesCopyLocalItems->Metadata('NuGetPackageId')->Distinct())" /> + + Condition="@(_DistinctMSBuildPackagesReferencedPoorly->Count()) > 0" + Code="MSBL001" + Text="A PackageReference to the package '%(_DistinctMSBuildPackagesReferencedPoorly.NuGetPackageId)' at version '%(_DistinctMSBuildPackagesReferencedPoorly.NuGetPackageVersion)' is present in this project without ExcludeAssets="runtime" set. This can cause errors at run-time due to MSBuild assembly-loading." + HelpLink="https://aka.ms/msbuild/locator/diagnostics/MSBL001" + />