使用dotnet-install脚本时global.json的rollForward属性未按预期生效
当使用dotnet-install脚本的--jsonfile参数时,我遇到了rollForward属性设为latestFeature的意外行为。
我的global.json内容如下:
{ "sdk": { "version": "6.0.416", "rollForward": "latestFeature" } }
截至2023年11月29日,.NET 6.0 SDK已有2023年11月14日发布的更新版本6.0.417。根据文档中的类似示例,我预期CI环境会安装6.0.417,但实际安装的是global.json中指定的6.0.416版本。
文档示例说明:
以下示例展示了如何使用特定主版本和次版本下已安装的最新功能带和补丁版本。所示JSON不允许任何早于3.1.102的SDK版本,允许3.1.102或任何更高的3.1.xxx版本,例如3.1.103或3.1.200。
对应的示例JSON:
{ "sdk": { "version": "3.1.102", "rollForward": "latestFeature" } }
CI环境执行输出:
[Container] 2023/11/29 18:06:38.363022 Running command /usr/local/bin/dotnet-install.sh --jsonfile global.json dotnet-install: Attempting to download using primary link https://dotnetcli.azureedge.net/dotnet/Sdk/6.0.416/dotnet-sdk-6.0.416-linux-x64.tar.gz dotnet-install: Remote file https://dotnetcli.azureedge.net/dotnet/Sdk/6.0.416/dotnet-sdk-6.0.416-linux-x64.tar.gz size is 186347163 bytes. dotnet-install: Extracting zip from https://dotnetcli.azureedge.net/dotnet/Sdk/6.0.416/dotnet-sdk-6.0.416-linux-x64.tar.gz dotnet-install: Downloaded file size is 186347163 bytes. dotnet-install: The remote and local file sizes are equal. dotnet-install: Installed version is 6.0.416 dotnet-install: Adding to current process PATH: `/root/.dotnet`. Note: This change will be visible only when sourcing script. dotnet-install: Note that the script does not resolve dependencies during installation. dotnet-install: To check the list of dependencies, go to https://learn.microsoft.com/dotnet/core/install, select your operating system and check the "Dependencies" section. dotnet-install: Installation finished successfully. [Container] 2023/11/29 18:06:47.220747 Running command dotnet --version 6.0.416
问题原因
dotnet-install脚本对global.json的rollForward支持存在局限性——它只会读取version字段确定要安装的SDK版本,不会自动应用rollForward策略查找并安装更高的兼容版本。文档中的示例描述的是dotnet CLI在已有SDK时的版本选择逻辑,而非dotnet-install脚本的安装行为。
解决方法
要让脚本安装指定主次要版本下的最新补丁版本,有两种可行方式:
使用
--channel参数替代--jsonfile
直接指定.NET频道版本,脚本会自动下载该频道下的最新SDK:/usr/local/bin/dotnet-install.sh --channel 6.0这会自动安装6.0分支下的最新SDK(即6.0.417)。
修改
global.json并配合脚本参数
如果必须使用global.json,可以将version设为通配符格式,同时在脚本中添加--version latest参数:{ "sdk": { "version": "6.0.x", "rollForward": "latestFeature" } }执行脚本时:
/usr/local/bin/dotnet-install.sh --jsonfile global.json --version latest
内容的提问来源于stack exchange,提问作者smk081

