Repository navigation
Batch WithSource project-file rewrites into a single pass - #55269
Merged
Merged
Conversation
TestAsset.WithSource applied ~14 property/package-version substitutions by calling UpdateProjProperty/ReplacePackageVersionVariable in a loop, each of which reloaded and rewrote every project file in the asset. That meant ~14 XDocument load+save round-trips per project file during the setup of every test that calls WithSource (the standard setup path for hundreds of SDK integration tests). Extract the per-document mutation bodies into private static helpers and apply all substitutions in one WithProjectChanges pass, so each project file is loaded and saved once. Behavior is unchanged: verified byte-identical output across all 352 test-asset csproj files. Co-authored-by: Copilot App <[email protected]> Copilot-Session: a22eead4-88ca-4142-a6d8-a7199db73289
|
Azure Pipelines: Successfully started running 1 pipeline(s). 2 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces setup overhead in SDK integration tests by batching TestAsset.WithSource() project-file rewrites into a single load/mutate/save pass per project file, instead of reloading and rewriting the same files once per substitution.
Changes:
- Batched all
WithSourceproperty and package-version substitutions into a singleWithProjectChanges(...)call per asset. - Extracted the XML mutation bodies into private helpers (
ApplyUpdateProjProperty,ApplyReplacePackageVersionVariable) and made the public APIs delegate to them.
Show a summary per file
| File | Description |
|---|---|
| test/Microsoft.NET.TestFramework/TestAsset.cs | Refactors WithSource to apply all substitutions in one WithProjectChanges pass; introduces shared helper methods for property and package-version mutations. |
Copilot's findings
- Files reviewed: 1/1 changed files
- Comments generated: 1
WithSource copied every source file to the destination and then, in the substitution pass, reloaded and re-saved each project/.xml file with the target-framework, runtime-identifier, and package-version substitutions applied. That meant every project file was written twice: once by File.Copy and again by the substitution save that immediately overwrote it. Skip the File.Copy for files that the substitution pass rewrites; instead load those directly from the source and write the substituted result straight to the destination once. Non-project files are still copied verbatim. Because File.Copy is an exact byte copy, loading from the source is equivalent to loading from the copied destination, so output is unchanged: verified byte-identical across all 257 test assets (1439 files, 0 differences) while eliminating 404 redundant writes (~22% of WithSource's file writes). Co-authored-by: Copilot App <[email protected]> Copilot-Session: a22eead4-88ca-4142-a6d8-a7199db73289
Address review feedback: use OfType<XAttribute>() to filter out null attributes returned by the LINQ chain instead of a redundant inner null-check. Behavior is unchanged. Co-authored-by: Copilot App <[email protected]> Copilot-Session: a22eead4-88ca-4142-a6d8-a7199db73289
Evangelink
enabled auto-merge
July 17, 2026 13:22
MichaelSimons
approved these changes
Jul 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
TestAsset.WithSource()is the standard setup path for hundreds of SDK integration tests: it copies a test asset onto disk and then rewrites the project files to substitute the current target framework, the latest runtime identifiers, and every package-version variable. Because this runs during the setup of everyWithSource-based test — including fast ones — any wasted work here is pure overhead paid across the whole suite.Two sources of avoidable work were baked into that setup:
Substitutions re-parsed and re-wrote every project file ~14 times.
WithSourceapplied its ~14 substitutions by callingUpdateProjProperty/ReplacePackageVersionVariablein a loop, and each of those independently reloaded and re-serialized every project file in the asset. A singleWithSourcecall performed on the order of 14XDocument.Load+File.Saveround-trips per project file.Every project file was written twice.
WithSourcefirstFile.Copy'd every source file to the destination, and then the substitution pass immediately overwrote each project/.xmlfile with a freshSave— so the copy's write was thrown away.Neither is a headline perf win on its own — real test time is dominated by the
dotnetbuild/restore subprocesses each test spawns. But this is avoidable setup work and, more importantly, avoidable write amplification. Fewer file writes matters most on CI/Helix, where each write is scanned by antivirus and hits slower storage — exactly the environments where test wall-clock is most visible.What
ApplyUpdateProjProperty,ApplyReplacePackageVersionVariable) and apply all property/package-version substitutions in one load/mutate/save per project file instead of once per substitution. The publicUpdateProjProperty/ReplacePackageVersionVariablemethods delegate to the same helpers, so external callers are unchanged.File.Copyfor the project/.xmlfiles that the substitution pass rewrites; load those straight from the source and write the substituted result to the destination once. Non-project files are still copied verbatim.No change to what the test framework produces.
Impact
For a project file in a
WithSourceasset, the file writes during setup drop from ~15 (1 copy + ~14 substitution saves) to 1. Measured over the full test-asset corpus, the redundant-copy removal alone eliminates 404 writes (~22%) ofWithSource's file writes.Validation
Microsoft.NET.TestFramework)..csprojfiles (0 mismatches).File.Copy(source→dest)then load-from-dest is equivalent to loading from source.