dotnet / core

.NET news, announcements, release notes, and more!
https://dot.net
MIT License
20.93k stars 4.9k forks source link

dotnet test result an error CS0579: Duplicate 'System.Reflection.AssemblyCompanyAttribute' #4837

Closed NicolasReyDotNet closed 4 years ago

NicolasReyDotNet commented 4 years ago

dotnet test result an error CS0579: Duplicate 'System.Reflection.AssemblyCompanyAttribute'

Hi, When building my project under Azure Devops yaml pipeline, if I launch unit test in my Dockerfile, I get the following : error CS0579: Duplicate 'System.Reflection.AssemblyCompanyAttribute'

General

The project is like organised this way image

So my Unit test (xUnit) project is under tests folder of my main project within the same repository.

My Dockerfile used for my CICD is

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["ITF.Microservices.Ordering.csproj", "ITF.Microservices.Ordering/"]
COPY NuGet.config "ITF.Microservices.Ordering/"
RUN dotnet restore "ITF.Microservices.Ordering/ITF.Microservices.Ordering.csproj"
WORKDIR "src/ITF.Microservices.Ordering"
COPY . .
RUN dotnet build "ITF.Microservices.Ordering.csproj" -c Release -o /app/build
RUN dotnet test "tests/ITF.Microservices.Ordering.UnitTests/ITF.Microservices.Ordering.UnitTests.csproj" -c Release --logger "trx;LogFileName=testresults.trx"

FROM build AS publish
RUN dotnet publish "ITF.Microservices.Ordering.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "ITF.Microservices.Ordering.dll"]

I both ITF.Microservices.Ordering.csproj and ITF.Microservices.Ordering.UnitTests.csproj I added the following to get rid of Duplicate errors.

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>

    <IsPackable>false</IsPackable>
      <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
      <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
      <GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
      <GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>  
      <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
      <GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
      <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
      <GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>    
  </PropertyGroup>
  <ItemGroup>

Under Azure Devops I use the following pipeline

trigger:
- master
- develop

resources:
- repo: self

variables:
  # Container registry service connection established during pipeline creation
  dockerRegistryServiceConnection: '$(dockerRegistryCnx)'
  containerRegistry: '$(containerRegistry)'
  tag: | 
     $(Build.BuildId)
     latest
  imageRepository: 'itfmicroservicesordering'
  imageName: 'ITF.Microservices.Ordering'
  dockerfilePath: '$(Build.SourcesDirectory)/CICD/Dockerfile'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

stages:
- template: build_deploy.yml
  parameters:
    vmImageName: $(vmImageName)
    path: '$(Build.Repository.LocalPath)'
    imageRepository: 'itfmicroservicesordering'
    dockerfilePath: '$(Build.SourcesDirectory)/CICD/Dockerfile'
    dockerRegistryServiceConnection: $(dockerRegistryServiceConnection)
    tag: '$(tag)'

And the following build_deploy.yml template

# azure-pipelines.yml/build_deploy.yml
parameters:
  vmImageName: ''
  path: ''
  imageRepository: ''
  dockerfilePath: ''
  dockerRegistryServiceConnection: ''
  tag: ''

stages:
- stage: Build
  displayName: Build and push stage
  condition: eq(variables['Build.SourceBranch'], 'refs/heads/master')
  jobs:  
  - job: Build   
    displayName: Build
    pool:
      vmImage: ${{ parameters.vmImageName }}
    steps:
    - task: Docker@2 
      displayName: Build and push an image to container registry
      inputs:
        buildContext: ${{ parameters.path }}
        command: buildAndPush
        repository: ${{ parameters.imageRepository }}
        dockerfile: ${{ parameters.dockerfilePath }}
        containerRegistry: ${{ parameters.dockerRegistryServiceConnection }}
        tags: |
          ${{ parameters.tag }}

The pipeline does build and publish the release correctly, but when I require dotnet test instruction from the Dockerfile, the tests are played and published image

But it ended with an error image

Full error line here obj/Release/netcoreapp3.1/.NETCoreApp,Version=v3.1.AssemblyAttributes.cs(4,12): error CS0579: Duplicate 'global::System.Runtime.Versioning.TargetFrameworkAttribute' attribute [/src/src/ITF.Microservices.Ordering/ITF.Microservices.Ordering.csproj]

When I remove the dotnet test from the Dockerfile instruction, everything works fine image

image

Some people seem to face the same problem as me https://stackoverflow.com/a/62021598/4734707

NicolasReyDotNet commented 4 years ago

Well the problem is solved, the problem was because, I guess, the Test project was inside the main project.

Now I have a folder structure like this

MyProject
   src/MyProject.csproj
   tests/MyTestProject.csproj
kduraiswami commented 1 year ago

If you are like me and changing the project structure is not realistic you can use a combination of this:

https://helloglenn.me/blog/dotnet-clean-bin-obj/

 <Target Name="SpicNSpan" AfterTargets="Test">
    <RemoveDir Directories="$(BaseOutputPath)" />
    <RemoveDir Directories="$(BaseIntermediateOutputPath)" /> 
  </Target>

I included it in both my normal csproj and test csproj to be safe