Repository navigation
perf: emit source-generated test types after user code (Defender scan 5s → 0.2s at 10k tests) #6908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
a10c97f
perf: emit source-generated test types after user code to avoid slow …
thomhurst 562eb4d
Add packaged-consumer check that generated types are emitted after us…
thomhurst 2bd49fa
Put the TUnit.Generated namespace file first for in-repo test projects
thomhurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // <auto-generated/> | ||
| // Added to test projects by TUnit.Core.props so it compiles ahead of the project's own files. | ||
| // | ||
| // The C# compiler writes namespaces to the assembly in reverse order of their first declaration. | ||
| // Source-generated trees are always added after the project's files, so without this declaration | ||
| // the TUnit.Generated types (large static constructors registering every test) are written at the | ||
| // very start of the assembly. Antivirus scanners such as Microsoft Defender analyse code at the | ||
| // start of an assembly far more expensively, which added seconds to every build and first test run | ||
| // of large test projects on Windows. Declaring the namespace first moves the generated types to the | ||
| // end of the assembly instead. | ||
| namespace TUnit.Generated | ||
| { | ||
| } |
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
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
32 changes: 32 additions & 0 deletions
32
tools/tunit-nuget-tester/TUnit.NugetTester/TUnit.NugetTester/GeneratedTypeOrderTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Reflection; | ||
|
|
||
| namespace TUnit.NugetTester; | ||
|
|
||
| /// <summary> | ||
| /// TUnit.Core.props adds TUnit.Core.GeneratedNamespace.cs ahead of the project's own files so the compiler | ||
| /// emits the source-generated TUnit.Generated types after the user's types. Emitting them first made | ||
| /// Microsoft Defender scan large test assemblies for seconds on every build (see PR #6908). | ||
| /// This project consumes the packed packages, so it guards the real NuGet import path. | ||
| /// </summary> | ||
| public class GeneratedTypeOrderTests | ||
| { | ||
| [Test] | ||
| [UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Only inspects this assembly's own type definitions.")] | ||
| public async Task Generated_Types_Are_Emitted_After_User_Types() | ||
| { | ||
| var types = typeof(GeneratedTypeOrderTests).Assembly.GetTypes(); | ||
|
|
||
| var generatedTypes = types.Where(t => t.Namespace == "TUnit.Generated").ToArray(); | ||
| var userTypes = types.Where(t => !t.IsNested && t.Namespace == typeof(GeneratedTypeOrderTests).Namespace).ToArray(); | ||
|
|
||
| await Assert.That(generatedTypes).IsNotEmpty(); | ||
| await Assert.That(userTypes).IsNotEmpty(); | ||
|
|
||
| // TypeDef metadata tokens follow the order the compiler emitted the types in. | ||
| var firstGenerated = generatedTypes.Min(t => t.MetadataToken); | ||
| var lastUser = userTypes.Max(t => t.MetadataToken); | ||
|
|
||
| await Assert.That(firstGenerated).IsGreaterThan(lastUser); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.