Skip to content

Commit 44b8f1e

Browse files
Copilotbaronfel
andauthored
Update MSBL001 to recommend ExcludeAssets="runtime" and PrivateAssets="all" (#366)
* Initial plan * Update MSBL001 diagnostic to suggest both ExcludeAssets="runtime" and PrivateAssets="all" Co-authored-by: baronfel <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: baronfel <[email protected]>
1 parent 0a8ee99 commit 44b8f1e

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

docs/diagnostics/MSBL001.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
## Error Message
44

5-
> 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.
5+
> A PackageReference to the package '{PackageId}' at version '{Version}' is present in this project without ExcludeAssets="runtime" and PrivateAssets="all" set. This can cause errors at run-time due to MSBuild assembly-loading.
66
77
## Cause
88

9-
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.
9+
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 and marking them as private. 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.
1010

1111
## Why This Is a Problem
1212

@@ -16,9 +16,11 @@ When MSBuild runtime assemblies are copied to your application's output director
1616
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
1717
3. **Inconsistent behavior**: Your application may behave differently than `MSBuild.exe`, `dotnet build`, or Visual Studio when evaluating projects
1818

19+
Additionally, without `PrivateAssets="all"`, downstream projects that reference your project may still get these runtime assets transitively, causing the same issues in consuming projects.
20+
1921
## Example Runtime Error
2022

21-
Without `ExcludeAssets="runtime"`, you may encounter errors like:
23+
Without the proper asset exclusions, you may encounter errors like:
2224

2325
```
2426
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.
@@ -34,18 +36,19 @@ This happens because your application loads MSBuild assemblies from your bin fol
3436

3537
## Solution
3638

37-
Add `ExcludeAssets="runtime"` to all MSBuild PackageReferences in your project file:
39+
Add `ExcludeAssets="runtime"` and `PrivateAssets="all"` to all MSBuild PackageReferences in your project file:
3840

3941
```xml
4042
<ItemGroup>
41-
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" />
42-
<PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="runtime" />
43-
<PackageReference Include="Microsoft.Build.Utilities.Core" ExcludeAssets="runtime" />
43+
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" PrivateAssets="all" />
44+
<PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="runtime" PrivateAssets="all" />
45+
<PackageReference Include="Microsoft.Build.Utilities.Core" ExcludeAssets="runtime" PrivateAssets="all" />
4446
...
4547
</ItemGroup>
4648
```
4749

48-
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.
50+
- `ExcludeAssets="runtime"` 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.
51+
- `PrivateAssets="all"` prevents the package reference metadata from flowing to downstream projects, ensuring that projects referencing your library don't inadvertently get runtime assets from these packages.
4952

5053
## Alternative: Disable the Check (Not Recommended)
5154

src/MSBuildLocator/build/Microsoft.Build.Locator.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<Error
2020
Condition="@(_DistinctMSBuildPackagesReferencedPoorly->Count()) > 0"
2121
Code="MSBL001"
22-
Text="A PackageReference to the package '%(_DistinctMSBuildPackagesReferencedPoorly.NuGetPackageId)' at version '%(_DistinctMSBuildPackagesReferencedPoorly.NuGetPackageVersion)' is present in this project without ExcludeAssets=&quot;runtime&quot; set. This can cause errors at run-time due to MSBuild assembly-loading."
22+
Text="A PackageReference to the package '%(_DistinctMSBuildPackagesReferencedPoorly.NuGetPackageId)' at version '%(_DistinctMSBuildPackagesReferencedPoorly.NuGetPackageVersion)' is present in this project without ExcludeAssets=&quot;runtime&quot; and PrivateAssets=&quot;all&quot; set. This can cause errors at run-time due to MSBuild assembly-loading."
2323
HelpLink="https://aka.ms/msbuild/locator/diagnostics/MSBL001"
2424
/>
2525
</Target>

0 commit comments

Comments
 (0)