How does Visual Studio fill 'Dependencies' node in Solution Explorer - msbuild

I am writing custom MSBuild Target, that replaces some of <PackageReference> for <ProjectReference>, under specific conditions.
I have following msbuild target:
<Target Name="ApplyProjectRedirects"
BeforeTargets="_GenerateDotnetCliToolReferenceSpecs;_GenerateProjectRestoreGraphPerFramework;_GenerateRestoreGraphWalkPerFramework;CollectPackageReferences;BeforeResolveReferences;ResolveAssemblyReferences"
AfterTargets="PaketRestore">
<ItemGroup>
<!-- remove packages we are redirecting to projects, and include the projects -->
<PackageReference Remove="#(ExcludePackageReferences)" />
<ProjectReference Include="#(IncludeProjectPaths)" />
</ItemGroup>
<ItemGroup>
<!-- include the packages in visual studio 'nuget' display, so we know what we redirected -->
<PackageReference Include="#(ExcludePackageReferences)">
<ExcludeAssets>all</ExcludeAssets>
</PackageReference>
</ItemGroup>
</Target>
And my project references are invisible in Visual Studio Dependencies. Project compiles as intended, however.
I found out that <ItemGroup> with <PackageReference> inside target is respected, when the target is ran before CollectPackageReferences. However, I was unable to fill <ProjectReference> with any target, even the first one.
How does Visual Studio fill the Dependencies with solution projects?

Related

T4 TransformOnBuild, how to ensure dependent assemblies are copied into $(TargetDir)

I am trying to create a NuGet package that deploys some T4 .ttinclude files which in turn rely on a number of referenced assemblies. But I'm having issues in consuming projects when I try to enable
The .ttinclude files have been designed to reference required assemblies using the following syntax:
<## assembly name="$(TargetDir)WidgetDatabase.dll" #>
In my NuGet package's target file, I configure T4ParameterValues so that the $(TargetDir) is passed to the T4 text transformer:
<ItemGroup>
<T4ParameterValues Include="TargetDir">
<Value>$(TargetDir)</Value>
<Visible>false</Visible>
</T4ParameterValues>
</ItemGroup>
In the target .csproj, I have the following:
<PropertyGroup>
<TransformOnBuild>true</TransformOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="WidgetDatabase">
<Version>0.8.1-alpha</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v16.0\TextTemplating\Microsoft.TextTemplating.targets" />
The problem is that the T4 Text Transform occurs before the build, but this means that none of the referenced assemblies are copied into $(TargetDir). How can I force the dependencies to be copied into the $(TargetDir) prior to the text transform target running?
This question is related, but doesn't satisfactorily resolve this issue.
I was facing the same problem and I managed to solve this by referencing the assembly from the NuGet package folder instead of trying to copy it to $(TargetDir).
NuGet 5.0 provides a GeneratePathProperty property that you can add to the PackageReference. This will create a MSBuild property that points to the location from which the package will be consumed:
<ItemGroup>
<PackageReference Include="WidgetDatabase" GeneratePathProperty="true">
<Version>0.8.1-alpha</Version>
</PackageReference>
</ItemGroup>
This property can then be added to T4ParameterValues so it can be consumed from your T4 template:
<ItemGroup>
<T4ParameterValues Include="PkgWidgetDatabase">
<Value>$(PkgWidgetDatabase)</Value>
<Visible>false</Visible>
</T4ParameterValues>
</ItemGroup>
<## assembly name="$(PkgWidgetDatabase)\lib\netstandard2.0\WidgetDatabase.dll" #>
The nice thing about this approach is that it works in MSBuild and in Visual Studio design-time.

How to stop the localized Microsoft.CodeAnalysis.*.resources.dll files from getting published by ASP.NET Core?

When I publish an ASP.NET Core 3.0 project, I get a few localized folders where the 4 assemblies shown are in each of these folders. I am not sure why these folders and files get included. None of my packages reference a CodeAnalysis package.
I added <PreserveCompilationContext>false</PreserveCompilationContext> in the csproj file but it didn't help. Is there a way to exclude them?
Add this:
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
to the .csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
</PropertyGroup>
As suggested, you can use none to exclude all of them:
<SatelliteResourceLanguages>none</SatelliteResourceLanguages>
and taking consideration languages do you want like english and spanish:
<SatelliteResourceLanguages>en;es</SatelliteResourceLanguages>
Works with VS2019 and other versions
UPDATE 2021/2022:
Still working with Visual Studio 2022 and .NET 6
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
You get a lot of language folders containing CodeAnalysis.dll files in your published output if you have a project reference to Microsoft.VisualStudio.Web.CodeGeneration.Design, which is needed for scaffolding controllers. If that is true for your project, change the package reference in your .csproj file to include ExcludeAssets="all"
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" ExcludeAssets="All" />
For example, old *.csproj file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UserSecretsId>aspnet-foo-4E53EF45-B3BE-4943-81BE-2449DC5AA2BC</UserSecretsId>
<BlazorLinkOnBuild>false</BlazorLinkOnBuild>
</PropertyGroup>
<ItemGroup>
<!-- ... -->
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design"
Version="3.0.0" />
</ItemGroup>
<ItemGroup>
<!-- ... -->
</ItemGroup>
</Project>
New file *.csproj should be
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UserSecretsId>aspnet-foo-4E53EF45-B3BE-4943-81BE-2449DC5AA2BC</UserSecretsId>
<BlazorLinkOnBuild>false</BlazorLinkOnBuild>
</PropertyGroup>
<ItemGroup>
<!-- ... -->
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design"
Version="3.0.0"
ExcludeAssets="All" />
</ItemGroup>
<ItemGroup>
<!-- ... -->
</ItemGroup>
</Project>
In my case, the source of these localized folders was from the package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation. It has a dependency on Microsoft.CodeAnalysis.Razor. You can read more about the purpose of the package here: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.1
You cannot just exclude an asset when trying to take advantage of the package. My work-around was to conditionally include the package reference whenever the project is in debug mode.
<ItemGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="3.1.1" />
</ItemGroup>
I then used an #if pre-processor directive to conditionally run the code that enables razor runtime compilation.
#if DEBUG
services.AddRazorPages().AddRazorRuntimeCompilation();
#else
services.AddRazorPages();
#endif
Please note: You may need to delete your bin folder to see the folders removed after a build. Also, make sure you are building under the correct solution configuration.
I was able to find a Github issue describing this exact scenario, but unfortunately it was never resolved. https://github.com/dotnet/extensions/issues/2247

msbuild PackageReference.PrivateAssets = All does not seem to work

I have a test project which reference NUnit3TestAdapter. I do not this reference to be copied over to the projects that depend on this one.
I thought setting PrivateAssets = All would do it, but apparently I misunderstand how it works, because it does not have the desired effect.
Here is the code:
Rollup\Rollup.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\UITests\UITests.csproj"/>
</ItemGroup>
</Project>
UITests\UITests.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NUnit3TestAdapter" Version="3.11.2">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
Directory.Build.rsp
.\Rollup.sln /restore /v:m
After I run msbuild all is built, but I can see NUnit3TestAdapter is in the bin folder for Rollup.
What am I missing?
(https://github.com/Microsoft/msbuild/issues/3996)
PrivateAssets works as expected but the NUnit test adapter NuGet package adds an MSBuild target to the build that adds a few dll files as content items to the project, which then flow transitively through the build - this has the same effect as if you added a text file and set its "Copy to Output Directory" property.
The NUnit3TestAdapter.props contains definitions like:
<Content Include="$(MSBuildThisFileDirectory)NUnit3.TestAdapter.dll">
<Link>NUnit3.TestAdapter.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Visible>False</Visible>
</Content>
You should see these files if you click the "Show All Files" in the Visual Studio solution explorer.
Note that test projects aren't really supposed to be packaged or referenced. They should be leaf projects. The test project templates even contain an <IsPackable>false</…> definition and XUnit's core package also adds it as an imported MSBuild file. The test frameworks expect you to use their abstraction libraries and not runtime assemblies / test adapter packages for projects that share tests or test logic.

.NET Core 1.1 - getting "Duplicate 'Content' items were included"

I've updated my VS2017 to latest 15.3.0 and installed .NET Core SDK 2.0 (I would like to upgrade an existing .NET 1.1 application to 2.0).
Now when I open my project that was compiling fine (didn't change anything in it yet) and I try to compile I get:
Duplicate 'Content' items were included.
The .NET SDK includes 'Content' items from your project directory by default.
You can either remove these items from your project file, or set the 'EnableDefaultContentItems' property to 'false' if you want to explicitly include them in your project file.
For more information, see https://aka.ms/sdkimplicititems. The duplicate items were: 'wwwroot\index.html'
Under problematic file it's pointing to C:\Program Files\dotnet\sdk\2.0.0\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.DefaultItems.targets
I've read online and I'm able to solve this by adding <EnableDefaultContentItems>false</EnableDefaultContentItems> to my .csproj file. But it wasn't there before and I'm not sure what adding this line means.
Once thing that really bothers me is that the source file it's pointing to is in dotnet\sdk\2.0.0 - and as I mentioned the project is still .NET Core 1.1. All I did so far was to install the update for VS2017 and the 2.0 SDK.
How do I solve this? I would like my original project to compile before I upgrade it to 2.0.
EDIT
My csproj file:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Content Include="wwwroot\index.html" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="IdentityServer4" Version="1.5.2" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.4.1" />
</ItemGroup>
<ItemGroup>
<Content Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="web.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<None Update="NLog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
As mentioned if I add <EnableDefaultContentItems>false</EnableDefaultContentItems> to PropertyGroup it works. But I don't know what is the meaning of this or why it's needed all of a sudden...
Remove the <ItemGroup> element containing
<Content Include="wwwroot\index.html" />
This item is already included by the Microsoft.NET.Sdk.Web and is therefore defined twice.
Necromancing.
Alternatively, do the following:
Click 'Show All Files' in Solution Explorer
Right click over 'wwwroot' select 'Exclude From Project'
Right click over 'wwwroot' select 'Include in Project'
The error is now gone.
Much safer than editing by hand.

How to copy files to output directory from a referenced NuGet package in .NET Core csproj?

I'm trying to use PhantomJS NuGet package in .NET core csproj application. But I think it is not possible using new PackageReference syntax for NuGet.
When I reference the PhantomJS package like this:
<PackageReference Include="PhantomJS" Version="2.1.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>
It does not do anything when I run dotnet build.
I'd expect it to copy the files inside PhantomJS package to the output directory (or anywhere in the project) so I could use the binary file provided by the PhantomJS package.
Is there another way to copy the contents of PhantomJS NuGet package to the output directory with MSBuild?
I think you want to use:
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
in the main <PropertyGroup>, which causes all dependencies to be copied to the output folder. This means every single dependency gets copied though so this can be quite a mess in some situations.
If you then want to exclude specific assemblies or packages:
<ItemGroup>
<-- won't copy to output folder -->
<PackageReference Include="MahApps.Metro" version="1.6.5">
<IncludeAssets>compile</IncludeAssets>
</PackageReference>
<PackageReference Include="Dragablz" version="0.0.3.203">
<IncludeAssets>compile</IncludeAssets>
</PackageReference>
...
<-- normal will copy to output folder -->
<PackageReference Include="xmlrpcnet" version="3.0.0.266" />
<PackageReference Include="YamlDotNet" version="6.0.0" />
</ItemGroup>
<ItemGroup>
<!-- keep assembly reference from copying to output -->
<Reference Include="$(SolutionDir)MarkdownMonster\bin\$(Configuration)\$(TargetFramework)\MarkdownMonster.exe">
<Private>false</Private>
</Reference>
</ItemGroup>
compile in this context means they are available for compilation, but aren't copied to the output folder.
There are two solutions:
1:
<ItemGroup>
<PackageReference Include="PhantomJS" Version="1.0.8" GeneratePathProperty="true" />
</ItemGroup>
<Target Name="CopyPdfExe" AfterTargets="Build">
<Copy SourceFiles="(PkgPhantomJS)\tools\phantomjs\phantomjs.exe" DestinationFolder="$(OutDir)" />
</Target>
2:
<ItemGroup>
<PackageReference Include="PhantomJS" Version="1.0.8" GeneratePathProperty="true" />
</ItemGroup>
<ItemGroup>
<None Include="$(PkgPhantomJS)\tools\phantomjs\phantomjs.exe" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
prefer the #2 since if this project is referenced by another one, the .exe can also be copied to the output folder
The <PackageReference> syntax in NuGet uses transitive dependencies, just like the project.json syntax. As such, the same rules apply. See this NuGet v3 which talks about what does and doesn't work between packages.config and the newer syntax. Specifically
You cannot rely on install.ps1 or uninstall.ps1 to function. These files will execute when using packages.config, but will be ignored in v3. So your package needs to be usable without them running. Init.ps1 will still run on NuGet 3.
To get files to copy to the output directory, the PhantomJS NuGet package needs to be changed to use contentFiles.
The tag names are misleading. Try
<PackageReference Include="PhantomJS" Version="2.1.1">
<IncludeAssets>none</IncludeAssets>
</PackageReference>
or
<PackageReference Include="PhantomJS" Version="2.1.1">
<ExcludeAssets>all</ExcludeAssets>
</PackageReference>
instead in order to get the referenced assemblies and other files copied to the build output. See
https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files
Try dotnet publish
dotnet publish [<PROJECT>] [-c|--configuration] [-f|--framework] [--force] [--manifest] [--no-dependencies] [--no-restore] [-o|--output] [-r|--runtime] [--self-contained] [-v|--verbosity] [--version-suffix]
dotnet publish [-h|--help]
See https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-publish?tabs=netcore2x
I know this is an old question, but what worked for me was to set IncludeAssets to "runtime"
Actually what is claimed to not work in the original question is what worked for me in .NET6:
<PackageReference Include="PhantomJS" Version="2.1.1">
<IncludeAssets>all</IncludeAssets>
</PackageReference>