Search code examples
c#msbuildgitlab-citarget-framework

How to compile+test for only installed target frameworks?


This is an exact duplicate of:

But I am looking for a working solution, since the following does not work for me:

I need to support 6.0 image:

% docker run -t mcr.microsoft.com/dotnet/sdk:6.0 dotnet --list-sdks
6.0.410 [/usr/share/dotnet/sdk]
% docker run -t mcr.microsoft.com/dotnet/sdk:6.0 dotnet build --version
MSBuild version 17.3.2+561848881 for .NET
17.3.2.46306

as well as 7.0 image:

% docker run -t mcr.microsoft.com/dotnet/sdk:7.0 dotnet --list-sdks
7.0.305 [/usr/share/dotnet/sdk]
% docker run -t mcr.microsoft.com/dotnet/sdk:7.0 dotnet build --version
MSBuild version 17.6.3+07e294721 for .NET
17.6.3.22601

In other word, what should I replace the following naive attempt with ?

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup Condition="'$(VisualStudioVersion)'=='16.0'">
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <PropertyGroup Condition="'$(VisualStudioVersion)'=='17.0'">
    <TargetFrameworks>net7.0</TargetFrameworks>
  </PropertyGroup>

I could not find no working solution either on microsoft.com forum:


Technically the image with net7.0 installed can build net6.0 target framework, however it is failing the test step. Full steps to reproduce issue:

% docker run -it mcr.microsoft.com/dotnet/sdk:7.0
# cd /tmp
# dotnet new sln -o unit-testing-using-dotnet-test
# cd unit-testing-using-dotnet-test
# dotnet new classlib -o PrimeService
# mv PrimeService/Class1.cs PrimeService.cs
# dotnet sln add ./PrimeService/PrimeService.csproj
# dotnet new xunit -o PrimeService.Tests
# dotnet add ./PrimeService.Tests/PrimeService.Tests.csproj reference ./PrimeService/PrimeService.csproj
# dotnet sln add ./PrimeService.Tests/PrimeService.Tests.csproj

now move it to net6.0 and observe it builds fine, but test do not work anymore:

# sed -e 's/net7.0/net6.0/' -i ./PrimeService/PrimeService.csproj
# sed -e 's/net7.0/net6.0/' -i ./PrimeService.Tests/PrimeService.Tests.csproj

gives:

# dotnet build
MSBuild version 17.6.3+07e294721 for .NET
  Determining projects to restore...
  Restored /tmp/unit-testing-using-dotnet-test/PrimeService/PrimeService.csproj (in 696 ms).
  Restored /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/PrimeService.Tests.csproj (in 752 ms).
  PrimeService -> /tmp/unit-testing-using-dotnet-test/PrimeService/bin/Debug/net6.0/PrimeService.dll
  PrimeService.Tests -> /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.71

but:

# dotnet test
  Determining projects to restore...
  All projects are up-to-date for restore.
  PrimeService -> /tmp/unit-testing-using-dotnet-test/PrimeService/bin/Debug/net6.0/PrimeService.dll
  PrimeService.Tests -> /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll
Test run for /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll (.NETCoreApp,Version=v6.0)
Microsoft (R) Test Execution Command Line Tool Version 17.6.0 (x64)
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Testhost process for source(s) '/tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll' exited with error: You must install or update .NET to run this application.
App: /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/testhost.dll
Architecture: x64
Framework: 'Microsoft.NETCore.App', version '6.0.0' (x64)
.NET location: /usr/share/dotnet/
The following frameworks were found:
  7.0.8 at [/usr/share/dotnet/shared/Microsoft.NETCore.App]
Learn about framework resolution:
https://aka.ms/dotnet/app-launch-failed
To install missing framework, download:
https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=6.0.0&arch=x64&rid=debian.11-x64
. Please check the diagnostic logs for more information.

Test Run Aborted.

Solution

  • Thanks to @shingo suggestion I was able to come up with the following in my gitlab-ci:

    build:
      stage: build
      extends: .build-matrix
      image: $IMAGE
      script:
        - |
          for sdk in $(dotnet --list-sdks | cut -f1-2 -d'.'); do
            dotnet build -p:TargetFrameworks=net${sdk}
          done
    

    and same goes for test:

    tests:
      stage: test
      extends: .build-matrix
      image: $IMAGE
      script:
        - |
          for sdk in $(dotnet --list-sdks | cut -f1-2 -d'.'); do
            dotnet test -p:TargetFrameworks=net${sdk}
          done