使用dotnet build --no-restore仍隐式执行restore,CI加/warnaserror遇问题
问题:
dotnet build --no-restore仍触发NuGet还原警告 使用dotnet build --no-restore命令时,本意是跳过NuGet包还原操作,但输出中仍出现NU1803还原相关警告。后续计划在CI环境中添加/warnaserror选项以确保项目无Stylecop问题,这会导致命令直接返回错误码,无法完成构建和代码检查。
命令输出示例:
PS C:\...> dotnet build --no-restore MSBuild version 17.6.0-preview-23122-01+4b5e303b3 for .NET C:\Program Files\dotnet\sdk\7.0.300-preview.23122.5\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(287,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy [C:\....Tests.csproj] C:\....Tests.csproj : warning NU1803: You are running the 'restore' operation with an 'HTTP' source, 'http://.../NuGetServer/nuget'. Non-HTTPS access will be removed in a future version. Consider migrating to an 'HTTPS' source. C:\Program Files\dotnet\sdk\7.0.300-preview.23122.5\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(287,5): message NETSDK1057: You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy [C:\....csproj] C:\....csproj : warning NU1803: You are running the 'restore' operation with an 'HTTP' source, 'http://.../NuGetServer/nuget'. Non-HTTPS access will be removed in a future version. Consider migrating to an 'HTTPS' source. BBLearnWrapper -> C:\....dll BBLearnWrapper.Tests -> C:\....Tests.dll Compilation compleated.
解决方案
1. 分离还原与构建步骤
先单独执行dotnet restore完成包还原,确认无问题后再执行dotnet build --no-restore。若仍出现警告,说明存在隐式触发还原的逻辑,需进一步排查。
2. 抑制NU1803警告
既然明确要跳过还原,可直接在构建命令中添加参数屏蔽该警告:
dotnet build --no-restore /warnaserror /p:NoWarn=NU1803
也可在项目文件(.csproj)中全局配置:
<PropertyGroup> <NoWarn>NU1803</NoWarn> </PropertyGroup>
3. 排查自定义MSBuild目标
检查项目是否引入第三方插件、自定义任务或.targets文件,这些内容可能会隐式触发NuGet还原操作。找到后修改相关目标逻辑,避免在构建阶段执行还原。
4. 升级到稳定版.NET SDK
当前使用的是预览版SDK(7.0.300-preview),预览版可能存在--no-restore参数不生效的bug。尝试升级到同版本的稳定版SDK,或更新至最新稳定版本。
内容的提问来源于stack exchange,提问作者Daniele Tentoni
相关产品推荐
相关产品推荐

