英文:
How to compile+test for only installed target frameworks?
问题
这是一个确切的重复问题:
但我正在寻找一个可行的解决方案,因为以下解决方案对我不起作用:
我需要支持6.0图像:
%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
.NET的MSBuild版本为17.3.2+561848881
17.3.2.46306
以及7.0图像:
%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
.NET的MSBuild版本为17.6.3+07e294721
17.6.3.22601
换句话说,我应该用什么替代以下天真的尝试?
<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>
在microsoft.com
论坛上,我也找不到可行的解决方案:
从技术上讲,具有安装了net7.0的图像可以构建net6.0目标框架,但它未能通过测试步骤。复现问题的完整步骤:
%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
现在将其移动到net6.0并观察它可以构建,但测试不再有效:
# sed -e 's/net7.0/net6.0/' -i ./PrimeService/PrimeService.csproj
# sed -e 's/net7.0/net6.0/' -i ./PrimeService.Tests/PrimeService.Tests.csproj
给出:
# dotnet build
.NET的MSBuild版本为17.6.3+07e294721
确定要还原的项目...
已还原 /tmp/unit-testing-using-dotnet-test/PrimeService/PrimeService.csproj(在696毫秒内)。
已还原 /tmp/unit-testing-using-dotnet-test/PrimeService.Tests/PrimeService.Tests.csproj(在752毫秒内)。
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
构建成功。
0个警告
0个错误
经过时间00:00:03.71
但:
# dotnet test
确定要还原的项目...
所有项目都已为还原而升级。
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
/tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll的测试运行(.NETCoreApp,版本=v6.0)
Microsoft(R)测试执行命令行工具版本17.6.0(x64)
版权所有(c)Microsoft Corporation。保留所有权利。
开始测试执行,请稍候...
共有1个测试文件与指定的模式匹配。
为/source(s)/tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/PrimeService.Tests.dll'的测试宿主进程以错误退出:您必须安装或更新.NET才能运行此应用程序。
应用程序:/tmp/unit-testing-using-dotnet-test/PrimeService.Tests/bin/Debug/net6.0/testhost.dll
架构:x64
Framework:'Microsoft.NETCore.App',版本'6.0.0'(x64)
.NET位置:/usr/share/dotnet/
找到以下框架:
7.0.8位于[/usr/share/dotnet/shared/Microsoft.NETCore.App]
了解框架解析:
https://aka.ms/dotnet/app-launch-failed
要安装丢失的框架,请下载:
https://aka.ms/dotnet-core-applaunch?framework=Microsoft.NETCore.App&framework_version=6.0.0&arch=x64&rid=debian.11-x64
。请检查诊断日志以获取更多信息。
测试运行已中止。
英文:
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.
答案1
得分: 1
我找到了一个小技巧,你只需要在项目文件中有一个 PropertyGroup
,核心思想是使用 -p
参数来替换 Targetframeworks
属性的值,然后使用 for
来获取每个 SDK 版本并构建每个版本。
for /f "eol= tokens=1,2 delims=. " %i in ('dotnet --list-sdks') do
dotnet build -p:TargetFrameworks=netcoreapp%i.%j
英文:
I find a small trick, you need only one PropertyGroup
in the project file, the core idea is to use the -p
argument to replace the value of the Targetframeworks
property, and use for
to obtain each SDK versions and build each one.
for /f "eol= tokens=1,2 delims=. " %i in ('dotnet --list-sdks') do
dotnet build -p:TargetFrameworks=netcoreapp%i.%j
答案2
得分: 0
感谢@shingo的建议,我能够提供以下的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
同样的配置也适用于测试部分:
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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论