Search code examples
.netazure-devopsyamlazure-pipelinesarchetypes

Azure Devops - YAML - not reading .net version from props file


I have a .net \ cs proj file that is using a common props file. Everything works, builds and runs in VS2022 and Rider.

An example of my csporj file is here

    <Project Sdk="Microsoft.NET.Sdk">
        <Import Project="$(SolutionDir)/Archetypes/TestArchetype.props" />
    
        <PropertyGroup>
            <ImplicitUsings>enable</ImplicitUsings>
            <Nullable>enable</Nullable>
    
            <IsPackable>false</IsPackable>
            <IsTestProject>true</IsTestProject>
        </PropertyGroup>
    
        <ItemGroup>
            <ProjectReference Include="..\..\..\Shared\xxx.Tests.Shared\xxx.Tests.Shared.csproj" />
            <ProjectReference Include="..\xxx.Admin.Api\xxx.Admin.Api.csproj" />
        </ItemGroup>
    
    </Project>

The props file I am trying to use is

    <?xml version="1.0" encoding="utf-8"?>
    <Project>
        <PropertyGroup>
            <TargetFramework Condition="'$(TargetFramework)' == ''">net8.0</TargetFramework>
        </PropertyGroup>
    
        <ItemGroup>
            <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.0" />
            <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
            <PackageReference Include="Moq" Version="4.18.4" />
            <PackageReference Include="MSTest.TestAdapter" Version="3.0.2" />
            <PackageReference Include="MSTest.TestFramework" Version="3.0.2" />
            <PackageReference Include="coverlet.collector" Version="3.2.0" />
            <PackageReference Include="Shouldly" Version="4.2.1" />
        </ItemGroup>
    </Project>

The Task this is failing on is the tests

    jobs:
    - job: APIGatewayBuild
        displayName: Build API Gateway
        pool: Default
        steps:
        - task: UseDotNet@2
            displayName: "Install .NET 8 SDK"
            inputs:
            packageType: "sdk"
            version: "8.x"
            installationPath: $(Agent.ToolsDirectory)/dotnet
    
        - task: DotNetCoreCLI@2
            displayName: "Run unit tests"
            inputs:
            command: "test"
            arguments: '-c $(BuildConfiguration) --filter TestCategory!~Integration -property:NoWarn="1701%3B1702%3B1591"'
            projects: |
                ApiGateway/**/*.Tests.csproj
                Shared/**/*.Tests.csproj

The pipline gets to the test step and then gives this error

API Gateway - Build Run unit tests

View raw log Starting: Run unit tests
==============================================================================
 Task         : .NET Core Description  : Build, test, package, or
publish a dotnet application, or run a custom dotnet command Version  
: 2.240.0 Author       : Microsoft Corporation Help         :
https://docs.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
==============================================================================
 C:\WINDOWS\system32\chcp.com 65001 Active code page: 65001 Info: .NET
Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been
removed from all hosted agents. If you're using these SDK/runtimes on
hosted agents, kindly upgrade to newer versions which are not EOL, or
else use UseDotNet task to install the required version.
D:\DevOpsAgents\yyy\_tool\dotnet\dotnet.exe test
D:\DevOpsAgents\yyy\1016\s\ApiGateway\Api\Consumer.HooksApi.Tests\Consumer.Hooks.Api.Tests.csproj
> --logger trx --results-directory D:\DevOpsAgents\yyy\_temp -c Release --filter TestCategory!~Integration -property:NoWarn=1701%3B1702%3B1591   Determining projects to restore...
D:\DevOpsAgents\yyy\_tool\dotnet\sdk\8.0.302\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets(96,5):
error NETSDK1013: The TargetFramework value '' was not recognized. It
may be misspelled. If not, then the TargetFrameworkIdentifier and/or
TargetFrameworkVersion properties must be specified explicitly.
[D:\DevOpsAgents\yyy\1016\s\ApiGateway\Api\Consumer.HooksApi.Tests\Consumer.Hooks.Api.Tests.csproj]
##[error]Error: The process 'D:\DevOpsAgents\yyy\_tool\dotnet\dotnet.exe' failed with exit code 1
D:\DevOpsAgents\yyy\_tool\dotnet\dotnet.exe test
D:\DevOpsAgents\yyy\1016\s\ApiGateway\Api\xxx.Admin.Api.Tests\xxx.Admin.Api.Tests.csproj
--logger trx --results-directory D:\DevOpsAgents\yyy\_temp -c Release --filter TestCategory!~Integration -property:NoWarn=1701%3B1702%3B1591   Determining projects to restore...

I would be grateful to understand where I am going wrong as I dont really wish to backout the archetypes because I have a solution of 82 projects and was a pain to upgrade due to a large number of downgrate detection errors.


Solution

  • You should not specify the TargetFramework property using the MSBuild .props file, as this property affects restore and will be automatically excluded. See "Guidance for the content of MSBuild props and targets".

    You can specify the TargetFramework property to the project via any of the following ways:

    1. Directly specify in the project file (e.g., myproject.csproj).

      <Project Sdk="Microsoft.NET.Sdk">
      
        <PropertyGroup>
          <TargetFramework>net8.0</TargetFramework>
          . . .
        </PropertyGroup>
      
      </Project>
      
      
    2. Add the MSBuild argument "-p:TargetFramework=net8.0" to the dotnet command to specify the TargetFramework or overwrite the one specified in the project file.

       jobs:
       - job: APIGatewayBuild
           displayName: Build API Gateway
           pool: Default
           steps:
           - task: UseDotNet@2
               displayName: "Install .NET 8 SDK"
               inputs:
               packageType: "sdk"
               version: "8.x"
               installationPath: $(Agent.ToolsDirectory)/dotnet
      
           - task: DotNetCoreCLI@2
               displayName: "Run unit tests"
               inputs:
               command: "test"
               arguments: '-c $(BuildConfiguration) --filter TestCategory!~Integration -p:NoWarn="1701%3B1702%3B1591";TargetFramework=net8.0'
               projects: |
                   ApiGateway/**/*.Tests.csproj
                   Shared/**/*.Tests.csproj