Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution build fails if coverlet.collector is already added to project #59

Open
FrediKats opened this issue May 12, 2024 · 3 comments
Open
Labels
question Further information is requested

Comments

@FrediKats
Copy link
Contributor

Actual behavior: dotnet-releaser add coverlet.collector to test projects during build step. If project already has reference to coverlet.collector, build finish with:

error NU1504: Warning As Error: Duplicate 'PackageReference' items found. Remove the duplicate items or use the Update functionality to ensure a consistent  restore behavior. The duplicate 'PackageReference' items are: coverlet.collector , coverlet.collector . 

Expected behavior: dotner-releaser must check coverlet.collector reference before adding it to test projects.

@FrediKats
Copy link
Contributor Author

@xoofx is this a good idea to remove reference (<PackageReference Remove="$(DotNetReleaserCoveragePackage)" />) before adding in this part of code? I can create PR but I'm not sure about this fix.

  <Choose>
    <When Condition="'$(ManagePackageVersionsCentrally)' == 'true' AND '$(CentralPackageVersionsFileImported)' == 'true' AND '$(DotNetReleaserCoverage)' == 'true' AND '$(IsTestProject)' == 'true'">
      <ItemGroup>
        <PackageReference Include="$(DotNetReleaserCoveragePackage)" VersionOverride="$(DotNetReleaserCoverageVersion)">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>
      </ItemGroup>
    </When>
    <When Condition="'$(DotNetReleaserCoverage)' == 'true' AND '$(IsTestProject)' == 'true'">
      <ItemGroup>
        <PackageReference Include="$(DotNetReleaserCoveragePackage)" Version="$(DotNetReleaserCoverageVersion)">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>
      </ItemGroup>
    </When>
  </Choose>

@xoofx xoofx added the question Further information is requested label May 12, 2024
@xoofx
Copy link
Owner

xoofx commented May 12, 2024

@xoofx is this a good idea to remove reference (<PackageReference Remove="$(DotNetReleaserCoveragePackage)" />) before adding in this part of code? I can create PR but I'm not sure about this fix.

Dunno, usually, I prefer to remove manually the package myself from the project so that there are less surprises. Could it that in some cases, it might be tricky to remove it (e.g via transitive references)

@FrediKats
Copy link
Contributor Author

Transitive references are not a problem. MSBuild can handle this. Moreover, this is common case for MSBuild when package added more than one time via transitive references: "Project A" uses SomePackage, "Project B" uses SomePackageand "Project B" uses "Project A".

MSBuild NU1504 is about treating duplicate references as accident. MSBuild can build successfully with duplicates with DisableCheckingDuplicateNuGetItems is set to true:

  <!--
    ============================================================
    CollectPackageReferences
    Gathers all PackageReference items from the project.
    This target may be used as an extension point to modify
    package references before NuGet reads them.
    ============================================================
  -->
  <Target Name="CollectPackageReferences" Returns="@(PackageReference)" >
    <!-- NOTE for design-time builds we need to ensure that we continue on error. -->
    <PropertyGroup>
      <CollectPackageReferencesContinueOnError>$(ContinueOnError)</CollectPackageReferencesContinueOnError>
      <CollectPackageReferencesContinueOnError Condition="'$(ContinueOnError)' == '' ">false</CollectPackageReferencesContinueOnError>
    </PropertyGroup>

    <CheckForDuplicateNuGetItemsTask
      Condition="'$(DisableCheckingDuplicateNuGetItems)' != 'true' "
      Items="@(PackageReference)"
      ItemName="PackageReference"
      LogCode="NU1504"
      MSBuildProjectFullPath="$(MSBuildProjectFullPath)"
      TreatWarningsAsErrors="$(TreatWarningsAsErrors)"
      WarningsAsErrors="$(WarningsAsErrors)"
      WarningsNotAsErrors="$(WarningsNotAsErrors)"
      NoWarn="$(NoWarn)"
      ContinueOnError="$(CollectPackageReferencesContinueOnError)"
      >
      <Output TaskParameter="DeduplicatedItems" ItemName="DeduplicatedPackageReferences" />
    </CheckForDuplicateNuGetItemsTask>

    <ItemGroup Condition="'@(DeduplicatedPackageReferences)' != ''">
      <PackageReference Remove="@(PackageReference)" />
      <PackageReference Include="@(DeduplicatedPackageReferences)" />
    </ItemGroup>

  </Target>

Now I have 3 ideas for fixing this case:

  • Remove PackageReference
  • Set DisableCheckingDuplicateNuGetItems=true
  • Add check that will return more useful error message: "Need to remove $(DotNetReleaserCoveragePackage) from solution. dotnet-release adds this package during executing" or smth like thiы

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants