Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions .agents/skills/eliminate-redundant-restores/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
name: eliminate-redundant-restores
description: "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)."
license: MIT
---

# 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`/`msbuild` steps 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.rsp` or the active MSBuild installation's `MSBuild.rsp` contains `-restore`
- Someone asks where `--no-restore` or `-restore:false` can safely be added
- `NETSDK1004` or `NETSDK1047` appeared after `--no-restore` was 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`, then `build-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.rsp` beside the active MSBuild executable, or response files passed explicitly with `@file`. A repository-local `MSBuild.rsp` is 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...` or `All projects are up-to-date for restore.` once per invocation.
- Binlog evidence: restore evaluations carry the global properties `MSBuildRestoreSessionId` and `ExcludeRestorePackageImports=true`; the Target Performance Summary lists `Restore` with 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 `MSBuild` task (`-restore` on the orchestrator leaves those unrestored and they fail with NETSDK1004) | Keep restore on that step, or add the project to the explicit restore step |
| **Same restore inputs.** `-r`/`--runtime`, RID-producing `-a`/`--arch`, `--os`, `--ucr`/`--use-current-runtime`, `TargetFramework(s)`, `RuntimeIdentifier(s)`, `PublishAot`, package sources (`--source`, `--configfile`, `RestoreSources`), `RestorePackagesPath`, and any `-p:` value a PackageReference condition reads. Architecture/OS options combine with the SDK's current RID; `--use-current-runtime` selects it. Missing RID targets fail with NETSDK1047 (`Assets file ... doesn't have a target for 'net10.0/linux-x64'`) | Match the effective RID in the explicit restore (for example `publish --os linux --arch x64` requires `restore -r linux-x64`), or keep restore on the later step. A generic restore does not cover these aliases merely because there is no `-r`. Do not require `SelfContained=true` on restore solely because publish is self-contained: RID-only restore covers ordinary SDK projects. Match `Configuration` or `SelfContained` only if repository conditions change package inputs |
| **Same available restore state.** Earlier `project.assets.json` files must exist where the later invocation looks for them. Match `--artifacts-path`, `UseArtifactsOutput`/`ArtifactsPath`, and `MSBuildProjectExtensionsPath`/`BaseIntermediateOutputPath`; another job or workspace does not automatically share these files | Align the explicit restore's output layout with build/test, or retain restore in the later workspace. For `build --artifacts-path artifacts --no-restore`, use `restore --artifacts-path artifacts` first; ordinary `obj/` assets do not cover that layout |
| **Nothing changes packages in between.** A step that generates or edits a project, runs `dotnet add package`, rewrites `Directory.Packages.props` or `NuGet.config` invalidates the earlier restore | Move the explicit restore after that step |

### Step 4: Apply the smallest change

- One explicit restore carrying every restore input, then `--no-restore`, `-restore:false` or `--no-build` on later steps.
- Several `dotnet test` steps over one solution: build once, then `dotnet test --no-build` on each. Add `--no-restore -restore:false` if an rsp enables restore, including on .NET 8 and earlier. `--no-restore` alone still rebuilds.
- Keep self-contained publish settings. A `publish --no-build` still needs a prior build with matching configuration, framework, RID and self-contained output; restore compatibility does not prove that build outputs exist.
- Scope the change to CI and scripts. Leave `-restore` in `Directory.Build.rsp`, because developers rely on it; pass `-restore:false` per invocation instead.
- Never remove the first restore. If it is a `--locked-mode` gate, keep it there.
- Do not bundle static graph restore or package caching into this change.

```bash
# One restore carries every input later steps use
dotnet restore MyApp.sln -r linux-x64
# Explicit overrides also work when an rsp enables restore on older SDKs
dotnet build MyApp.sln -c Release --no-restore -restore:false
dotnet test MyApp.sln -c Release --no-build --no-restore -restore:false
dotnet publish src/Api -c Release -r linux-x64 --no-restore -restore:false
# msbuild invocations need the explicit switch if an rsp file adds -restore
dotnet msbuild Packaging.proj -t:Pack -restore:false
```

### Step 5: Validate and report truthfully

1. Run the changed pipeline or script end to end in a clean temporary copy (no `obj/`, no `project.assets.json`). A project that was never restored only fails there. YAML syntax or flag-presence checks do not validate restore coverage or test execution. If you cannot run it, say "not validated" and list the steps and projects to confirm.
2. Success evidence: every later step passed; `Restore` calls (binlog) or `Determining projects to restore` lines fell from N to the expected count; for a time claim, before/after medians of at least three runs.
3. Report a restore failure (NETSDK1004, NETSDK1047) separately from a build failure, and never claim success after either.

## Output

Explain which steps restored, which earlier restore covers them, what changed, and what remains unvalidated. State whether redundant restores were removed or no change was needed, keeping the required first restore. Use prose or a findings table as appropriate; no fixed table headings or verdict syntax are required.

## Common Pitfalls

| Pitfall | Solution |
|---|---|
| Adding `--no-restore` to every command | Apply the Step 3 checks per step |
| Missing that an rsp adds `-restore` to "build-only" msbuild steps | Search for `*.rsp` before concluding a step does not restore |
| Deleting `-restore` from `Directory.Build.rsp` | Override per CI invocation; the rsp serves developers |
| Orchestrator `.proj` restored, children not | Restore each child explicitly, or keep restore on the step that builds it |
| Publishing with `-r`, architecture/OS options, or `--use-current-runtime` after a generic restore | Restore with the same effective runtime identifier, including SDK-host defaults used by the aliases |
| A generator step edits PackageReferences after restore | Restore after the generator |
21 changes: 13 additions & 8 deletions .copilot/curate/manifest.lock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
version: 1
managedBy: gh-copilot-curate
toolVersion: 0.6.0 (commit 87f35bd, built 2026-06-04T11:24:56Z)
generatedAt: 2026-10-16T09:36:39.3863289Z
manifestHash: sha256:2bc8ac83a1bf89c62658151d10dc811a52bef10cccc97e7be174e7a7c67a7573
generatedAt: 2026-10-19T18:49:07.1553576Z
manifestHash: sha256:4bb31905c11fbf6fce9075a9650c8b272df696a030a4bb4ad6ff90aa20377622
plugins:
- id: dotnet
source:
Expand All @@ -13,7 +13,7 @@ plugins:
owner: dotnet
repo: skills
requestedRef: main
resolvedRef: 19bb55d785cd88ce47a1b43f9a21d0192c412dac
resolvedRef: 10d85e25fb42d5f65c568d5e979a005ee69eecb3
layout: dotnet-skills
install:
mode: summary
Expand Down Expand Up @@ -115,7 +115,7 @@ plugins:
owner: dotnet
repo: skills
requestedRef: main
resolvedRef: 9de888b8ce55da34e76a113bd7b2d463d6b3e1e6
resolvedRef: 9386ed7c1148a7bef970aa0502371c982effa01c
layout: dotnet-skills
install:
mode: summary
Expand Down Expand Up @@ -150,6 +150,11 @@ plugins:
upstreamHash: sha256:a5f0b84a3138e5fdf1fdf103268493006756a1c86312952926f3a10c3e5dd738
localHash: sha256:a5f0b84a3138e5fdf1fdf103268493006756a1c86312952926f3a10c3e5dd738
mode: "0666"
- path: .agents/skills/eliminate-redundant-restores/SKILL.md
upstreamPath: plugins/dotnet-msbuild/skills/eliminate-redundant-restores/SKILL.md
upstreamHash: sha256:178c6b44ae9d5a4b5aedefa73b1366be7d389e07a3c855e2bdceeb54cc435e74
localHash: sha256:178c6b44ae9d5a4b5aedefa73b1366be7d389e07a3c855e2bdceeb54cc435e74
mode: "0666"
- path: .agents/skills/including-generated-files/SKILL.md
upstreamPath: plugins/dotnet-msbuild/skills/including-generated-files/SKILL.md
upstreamHash: sha256:8216b42f0b12361abc85ad4c439a4c90f728ceccaf7ff184898ff9a99b2336b9
Expand Down Expand Up @@ -197,8 +202,8 @@ plugins:
mode: "0666"
- path: .github/agents/build-perf.agent.md
upstreamPath: plugins/dotnet-msbuild/agents/build-perf.agent.md
upstreamHash: sha256:ad7f5870b1f999889ed564523af609b3b7cda14bab5f6ddda80767c747871d92
localHash: sha256:ad7f5870b1f999889ed564523af609b3b7cda14bab5f6ddda80767c747871d92
upstreamHash: sha256:622f219a43d65e3023337206b63ae13df2242e4deb2cfe16958dbd51fbf40ede
localHash: sha256:622f219a43d65e3023337206b63ae13df2242e4deb2cfe16958dbd51fbf40ede
mode: "0666"
- path: .github/agents/msbuild-code-review.agent.md
upstreamPath: plugins/dotnet-msbuild/agents/msbuild-code-review.agent.md
Expand All @@ -207,8 +212,8 @@ plugins:
mode: "0666"
- path: .github/agents/msbuild.agent.md
upstreamPath: plugins/dotnet-msbuild/agents/msbuild.agent.md
upstreamHash: sha256:7ffa223129d65e3fc5159dfdb79cb90f0c1b383fa404286d092c067f076f0565
localHash: sha256:7ffa223129d65e3fc5159dfdb79cb90f0c1b383fa404286d092c067f076f0565
upstreamHash: sha256:680cff3b89d6586575d44d3a222c53da27b81ed0016a30073c9f6edc8586d6aa
localHash: sha256:680cff3b89d6586575d44d3a222c53da27b81ed0016a30073c9f6edc8586d6aa
mode: "0666"
- id: dotnet-test
source:
Expand Down
1 change: 1 addition & 0 deletions .copilot/curate/manifest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ plugins:
- skills/build-parallelism
- skills/check-bin-obj-clash
- skills/directory-build-organization
- skills/eliminate-redundant-restores
- skills/including-generated-files
- skills/item-management
- skills/msbuild-antipatterns/references
Expand Down
1 change: 1 addition & 0 deletions .github/agents/build-perf.agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Load these skills for detailed guidance on specific optimization areas:
- `eval-performance` — Evaluation performance
- `check-bin-obj-clash` — Output path conflicts
- `copy-to-output-directory` — Removing the `Always` copy perf hit (`IfDifferent`, `$(SkipUnchangedFilesOnCopyAlways)`)
- `eliminate-redundant-restores` — Removing repeated restores across pipeline or script steps safely (`--no-restore`, `-restore:false`, rsp `-restore`)
Comment thread
Evangelink marked this conversation as resolved.

## Important Notes
- Always use `/bl` to generate binlogs for data-driven analysis
Expand Down
1 change: 1 addition & 0 deletions .github/agents/msbuild.agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ This agent has access to a comprehensive set of troubleshooting and optimization
- `incremental-build` — Incremental build optimization
- `build-parallelism` — Parallelism and graph build
- `eval-performance` — Evaluation performance
- `eliminate-redundant-restores` — Removing repeated restores across pipeline or script steps safely

### Code Quality Skills
- `msbuild-antipatterns` — Anti-pattern catalog with detection rules and fix recipes
Expand Down
1 change: 1 addition & 0 deletions .github/instructions/copilot-curate.instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Run `gh copilot-curate list` to see installed plugins; run `gh copilot-curate up

- [Custom Target Authoring Patterns](../../.agents/skills/target-authoring/SKILL.md) — _skill_ — Canonical patterns from `Microsoft.Common.CurrentVersion.targets` in the MSBuild repository.
- [Detecting OutputPath and IntermediateOutputPath Clashes](../../.agents/skills/check-bin-obj-clash/SKILL.md) — _skill_ — This skill helps identify when multiple MSBuild project evaluations share the same `OutputPath` or `IntermediateOutputPath`. This is a common source of build failures including:
- [Eliminate Redundant Restores](../../.agents/skills/eliminate-redundant-restores/SKILL.md) — _skill_ — 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.
- [Including Generated Files Into Your Build](../../.agents/skills/including-generated-files/SKILL.md) — _skill_ — Files generated during the build are generally ignored by the build process. This leads to confusing results such as: - Generated files not being included in the output directory - Generated source files not being compiled - Globs not capturing files created during the build
- [MSBuild Item Management Patterns](../../.agents/skills/item-management/SKILL.md) — _skill_ — Canonical patterns for working with item groups, from `Microsoft.Common.CurrentVersion.targets`.
- [MSBuild Modernization: Legacy to SDK-style Migration](../../.agents/skills/msbuild-modernization/SKILL.md) — _skill_ — **Legacy indicators:**
Expand Down
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Run `gh copilot-curate list` to see installed plugins; run `gh copilot-curate up

- [Custom Target Authoring Patterns](.agents/skills/target-authoring/SKILL.md) — _skill_ — Canonical patterns from `Microsoft.Common.CurrentVersion.targets` in the MSBuild repository.
- [Detecting OutputPath and IntermediateOutputPath Clashes](.agents/skills/check-bin-obj-clash/SKILL.md) — _skill_ — This skill helps identify when multiple MSBuild project evaluations share the same `OutputPath` or `IntermediateOutputPath`. This is a common source of build failures including:
- [Eliminate Redundant Restores](.agents/skills/eliminate-redundant-restores/SKILL.md) — _skill_ — 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.
- [Including Generated Files Into Your Build](.agents/skills/including-generated-files/SKILL.md) — _skill_ — Files generated during the build are generally ignored by the build process. This leads to confusing results such as: - Generated files not being included in the output directory - Generated source files not being compiled - Globs not capturing files created during the build
- [MSBuild Item Management Patterns](.agents/skills/item-management/SKILL.md) — _skill_ — Canonical patterns for working with item groups, from `Microsoft.Common.CurrentVersion.targets`.
- [MSBuild Modernization: Legacy to SDK-style Migration](.agents/skills/msbuild-modernization/SKILL.md) — _skill_ — **Legacy indicators:**
Expand Down
Loading