eliminate-redundant-restores
它能做什么
这项技能在构建流水线中找出重复的 NuGet 还原步骤,并在不破坏构建的前提下移除不需要的步骤。适合 .NET 构建脚本中多次还原包的团队。
适用场景
- ✓当流水线运行多个构建步骤,并且每个步骤都还原包时。
- ✓当构建日志显示在已经完成还原之后又出现还原输出时。
- ✓当添加 no-restore 开关导致构建错误的时候。
技能链接
https://github.com/dotnet/skills/blob/main/plugins/dotnet-msbuild/skills/eliminate-redundant-restores/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我们的 CI 脚本在每个步骤中都还原 NuGet 包。请在不破坏构建的前提下,移除重复的还原。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会找出哪些构建命令执行还原,检查后续步骤是否已被更早的还原覆盖,然后只在安全的地方添加 no-restore 开关。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Eliminate Redundant Restores
A no-op restore still evaluates every project and checks restore state, so each extra restore in a multi-step build costs seconds to tens of seconds in a large repo. The cure is easy to get wrong: --no-restore on a step that no earlier restore actually covered turns a slow build into a failed one. This skill finds which invocations restore, proves which are already covered, and changes only those.
When to Use
- A pipeline or script runs several
dotnet/msbuildsteps and the log shows restore output in more than one of them - A restore step exists, yet later steps still print
Determining projects to restore... - A repository
Directory.Build.rspor the active MSBuild installation'sMSBuild.rspcontains-restore - Someone asks where
--no-restoreor-restore:falsecan safely be added NETSDK1004orNETSDK1047appeared after--no-restorewas added
When Not to Use
- The first restore is slow (feed latency, missing package cache): that is caching, not redundancy
- There is only one restore across all invocations: nothing to deduplicate
- Restore errors about versions, lock files or auditing: fix the restore itself
- Unranked "why is my build slow": start with
build-perf-baseline, thenbuild-perf-diagnostics
Inputs
Discover repository inputs in the working directory; do not ask the user for paths.
- CI definitions (
*.yml,.github/workflows,azure-pipelines*.yml,Jenkinsfile) and build scripts (build.cmd,build.sh,build.ps1,eng/) Directory.Build.rsp,Directory.Build.props/.targets,NuGet.config- Auto-loaded
MSBuild.rspbeside the active MSBuild executable, or response files passed explicitly with@file. A repository-localMSBuild.rspis not auto-loaded - Restore evidence if present: step logs or a binlog
Workflow
Step 1: Confirm restore is repeated and worth removing
- Log evidence:
Determining projects to restore...orAll projects are up-to-date for restore.once per invocation. - Binlog evidence: restore evaluations carry the global properties
MSBuildRestoreSessionIdandExcludeRestorePackageImports=true; the Target Performance Summary listsRestorewith N calls. - Quantify each no-op restore. If there is one restore, or the repeated ones total about a second, report "nothing worth removing" and stop without editing.
Step 2: Inventory every invocation in execution order
| Invocation | Restores? |
|---|---|
dotnet build, test, publish, pack |
Implicitly, unless --no-restore; also check inherited rsp flags |
dotnet run |
Implicitly before building; --no-restore can still inherit rsp restore on older SDKs. With --no-build, no build or restore runs |
dotnet test, publish, pack with --no-build |
No implicit restore, but an rsp -restore can still enable restore; add --no-restore -restore:false |
dotnet msbuild, msbuild.exe, CI MSBuild tasks |
With -restore or -t:Restore, including flags inherited from repository Directory.Build.rsp or installation-level MSBuild.rsp, even when a step looks build-only |
dotnet restore, nuget restore, msbuild -t:Restore |
Explicit restore |
| CI build task with a "restore packages" option | Check its default |
Starting with .NET SDK 9.0.1xx, --no-restore forwards -restore:false to override rsp flags; older SDKs can still inherit -restore despite skipping the CLI's implicit restore. Whenever an rsp enables restore, use the version-independent --no-restore -restore:false with dotnet build, test, publish or pack, even with --no-build. dotnet msbuild and msbuild.exe take -restore:false, not --no-restore.
dotnet run does not expose the MSBuild -restore:false override. With an rsp on older SDKs, first build using dotnet build --no-restore -restore:false, then run using dotnet run --no-build with the same project, configuration, framework and runtime inputs (or execute the built DLL directly). Passing -restore:false to dotnet run is not a restore-suppression strategy.
Step 3: Decide per invocation
Keep the first restore. Skip a later one only when all four checks pass:
| Check | If it fails |
|---|---|
Same project set. A restore covers its entry project or solution and that project's ProjectReference closure. It does not cover a project in another solution or .slnf, a tool built from another path, or a project an orchestrating .proj builds through the `MSBuil |
常见问题
这样能加快第一次缓慢的还原吗?
不能。它针对的是重复的还原,而不是缓慢的第一次还原,后者属于缓存问题。
为什么我会遇到 no-restore 错误?
如果在没有被更早还原覆盖的步骤中添加该开关,就会出错,因此这项技能会先检查覆盖情况。
它涵盖版本错误吗?
不涵盖。关于版本或锁文件的还原错误,需要在还原本身中修复。
详情
作者的原始描述
Remove repeated NuGet restores from multi-step .NET builds safely. USE FOR: build/test/publish steps that each restore, Directory.Build.rsp with -restore, safe restore suppression, NETSDK1004/NETSDK1047 after --no-restore. DO NOT USE FOR: slow first restore, only one restore, version or lock errors, unranked build slowness (use build-perf-baseline).
repo: dotnet/skills
热度
- GitHub 星标
- 5.8k
- 排名
- #14598
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
create-custom-agent
5.8k帮你的智能体创建 VS Code 自定义智能体文件,为每个智能体设置专属工具、指令以及向其他智能体的交接。
create-skill
5.8k帮你的智能体创建新的智能体技能,包括有效的名称、清晰的触发描述,以及符合标准的 SKILL.md 文件。
csharp-scripts
5.8k帮你的智能体通过 .NET 命令行工具以单个文件运行小型 C# 程序,无需创建完整项目。
dotnet-pinvoke
5.8k帮你的智能体在 .NET 中正确调用原生 C 或 C++ 库,涵盖函数签名、字符串处理、内存和安全句柄。