NuGet / Home

Repo for NuGet Client issues
Other
1.5k stars 252 forks source link

PackageReference defined in .props file not appearing in project.assets.json when using dotnet restore #9132

Closed cmenzi closed 4 years ago

cmenzi commented 4 years ago

I have a tools.props file which contains some PackageReference's. When I import the file into a .csproj either via <Import Project="tools.props" /> or via a NuGet package where to 'tools.propsfile in packed into thebuildfolder, then all dependencies defined in the.propsare not appearing inproject.assets.json`.

The funny thing is, that when using VS 2019 and build it, then everything works and project.assets.json created correctly.

But when using dotnet restore command or msbuild /t:restore the dependency are not it project.assets.json.

tools.props

<Project>
  <ItemGroup>
    <PackageReference Include="trx2junit" Version="[1.3.0]" PrivateAssets="All" ExcludeAssets="All" />
    <PackageReference Include="GitVersion.Tool" Version="[5.1.1]" PrivateAssets="All" ExcludeAssets="All" />
    <PackageReference Include="dotnet-sonarscanner" Version="[4.7.1]" PrivateAssets="All" ExcludeAssets="All" />
    <PackageReference Include="ReportGenerator" Version="[4.4.6]" PrivateAssets="All" ExcludeAssets="All" />
    <PackageReference Include="NuGet.CommandLine" Version="[5.4.0]" PrivateAssets="All" ExcludeAssets="All" />
    <PackageReference Include="CycloneDX" Version="[0.9.0]" PrivateAssets="All" ExcludeAssets="All" />
  </ItemGroup>
</Project>

When I define the ItemGroup directly in the .csproj and not in the .props file, then it works aswell.

Why is the behavoir differnent in VS than in msbuild or dotnet restore?

Is there a hint, flag, property to pass, they dotnet restore should handle those package references same as they would be directly defined in the csproj?

nkolev92 commented 4 years ago

This is by design.

NuGet explicitly excludes those targets from affecting restore. If a package contains a build/package.targets file that contains a PackageReference the output of restore will change based on if the project had previously been restored.

See https://github.com/NuGet/Home/issues/3604.

Doc issue to clarify this: https://github.com/NuGet/docs.microsoft.com-nuget/issues/1872

The Visual Studio experience is different due to a project-system bug, see: https://github.com/dotnet/project-system/issues/3734

If your package has dependencies, it should declare them as such. Otherwise you might need to consider an msbuild sdk, like https://github.com/microsoft/MSBuildSdks