Skip to content

Conditional test filtering framework for PR builds - #55166

Merged
MichaelSimons merged 38 commits into
mainfrom
michaelsimons-conditionalize-illinker-tests
Jul 21, 2026
Merged

MichaelSimons merged 38 commits into
mainfrom
michaelsimons-conditionalize-illinker-tests

Conversation

@MichaelSimons

@MichaelSimons MichaelSimons commented Jul 7, 2026 •

Copy link
Copy Markdown
Member

Summary

Introduces a framework for conditionally running tests on PR builds based on what source files changed. Tests that are unrelated to the PR's changes can be skipped, reducing Helix compute consumption and improving PR validation times. All tests continue to run unconditionally on CI builds (main, release branches).

This is posted as a draft to seek feedback on the approach. The implementation is included to illustrate the direction — it is intentionally simple and small despite addressing a complex problem.

Received positive feedback on the approach — proceeding with the full implementation of conditional test filtering.

Design

See documentation/project-docs/pr-test-filtering.md for the full motivation, mechanisms, and instructions for adding new scopes.

What's included

  • Design document with motivation, mechanisms, and instructions for adding scopes
  • C# evaluation script
  • Central config file (ConditionalTests.props) with TemplateEngine as the first scope
  • MSBuild + pipeline integration
  • Agent instructions update
  • Infrastructure tests (Microsoft.NET.Infrastructure.Tests) validating both the evaluation script and the MSBuild removal target — covers scope evaluation logic, glob matching, CI/dependency-flow detection, global trigger paths, branch filtering, and configuration validation

Related to #55203

Introduces a framework for conditionally running tests on PR builds
based on what source files changed, reducing Helix compute consumption.

- Design document (documentation/project-docs/pr-test-filtering.md)
- C# evaluation script (scripts/EvaluateConditionalTestScopes.cs)
- Central config (test/ConditionalTests.props) with TemplateEngine scope
- MSBuild + pipeline integration
- Agent instructions update

Co-authored-by: Copilot App <[email protected]>

@nagilson nagilson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, I'm in favor of the proposal.

However, I want to poke on two points:

  1. How will we define TriggerPaths?
    I think this should be more than the folder containing relevant source code for that repo - e.g. dotnet-watch project should not be conditioned on just dotnet-watch but also include the ProjectReference(s) such as ..\..\Cli\Microsoft.DotNet.Cli.Definitions\Microsoft.DotNet.Cli.Definitions.csproj. Changes to PackageReference(s) such as Microsoft.Build.Locator" may also result in a break.

dotnetup already uses conditional test filtering to a degree but we've seen more breaks than expected when running all of the tests. So, there is a risk involved in disabling these tests though on average I think it's the right approach. We should try to minimize that risk.

There have been times where we caught product bugs (e.g. system command line, roslyn, and others) because tests in other areas started failing even when roslyn's tests were all passing. But we shouldn't rely on test coverage of others like that.

  1. When can we ignore 'CoditionalTests' - I think we should be able to ignore that. We can still get a lot of the time saving on codeflow or certain dev PRs... but I think for example in dotnet/dotnet flow we will still want to run all of the tests so we get full filtering.

I'd propose to have the conditional tests condition test runs out on code flow for specific repos - e.g. a change to dotnet/templating code flow shouldn't need to run the watch tests. If we are all encompassing on dependencies with 1 then maybe we can be looser on that?

- Added 'Choosing trigger paths' subsection covering dependency awareness
- Moved dependency flow RunAlways future inline with the metadata description
- Consistent with how class/method-level futures are described inline

Co-authored-by: Copilot App <[email protected]>
@MichaelSimons

Copy link
Copy Markdown
Member Author

@noahfalk, thanks for the insightful feedback.

  1. How will we define TriggerPaths?

You are correct that defining just the src path for the affected area is often not sufficient. I did not elaborate on this in the documentation but should call it out in order to help devs and AI make better decisions when defining new test scopes.

So, there is a risk involved in disabling these tests though on average I think it's the right approach. We should try to minimize that risk.

Yes this is in ways a big hammer so it must be used correctly. Having good documentation calling these gotchas is imperative.

  1. When can we ignore 'CoditionalTests'

I do see it valuable to be able to specify a RunAlways setting to be able to force tests to run on code flows. Given the vmr, it might not provide substantive value in providing fined grained support.

I updated the doc in d518adb in response to this feedback.

@MichaelSimons
MichaelSimons requested a review from Evangelink July 7, 2026 19:07
@marcpopMSFT
marcpopMSFT requested review from JoeRobich, sbomer and tmat July 7, 2026 19:16
Simplifies the evaluation script by requiring the caller to pass the
props file path explicitly. Removes the FindPropsFile fallback. The
pipeline YAML now passes the full path via Build.SourcesDirectory.

Co-authored-by: Copilot App <[email protected]>
MichaelSimons and others added 2 commits July 7, 2026 15:08
- Added one-third minimum goal for scope coverage
- Clarified 'initial candidates' framing for example scopes
- Added 'Choosing trigger paths' section covering dependency awareness
- Inlined dependency flow RunAlways future with metadata description

Co-authored-by: Copilot App <[email protected]>
Notes AI-based test selection and incremental build improvements as
aspirational alternatives, framing PR velocity as the primary driver
for the current pragmatic approach.

Co-authored-by: Copilot App <[email protected]>
@noahfalk

noahfalk commented Jul 8, 2026

Copy link
Copy Markdown
Member

@noahfalk, thanks for the insightful feedback.

I do love free compliments but I think you meant to tag a different Noah (@nagilson) :)

MichaelSimons and others added 7 commits July 8, 2026 13:17
The script: task runs via cmd.exe on Windows where relative paths like
.dotnet/dotnet don't resolve. Use Build.SourcesDirectory for the full path.
Also update display name to purple dot convention.

Co-authored-by: Copilot App <[email protected]>
When no scopes match, the script now outputs '__none__' instead of an
empty string. This distinguishes 'script ran, nothing active' (remove
scoped tests) from 'variable never set' (safe fallback, run all tests).

Co-authored-by: Copilot App <[email protected]>
Logs build reason, target branch, changed file count (with file list
for small diffs), and per-scope active/inactive decisions with reasons.
Makes CI failures diagnosable from the step log.

Co-authored-by: Copilot App <[email protected]>
Changes to shared test infrastructure (Microsoft.NET.TestFramework,
TestFramework.MSTest, evaluation script) now force all conditional
scopes to run, since these are dependencies of all test projects.

GlobalTriggerPaths is defined in ConditionalTests.props as a PropertyGroup
and checked before per-scope evaluation.

Co-authored-by: Copilot App <[email protected]>
…tting

- Invert variable semantics: list scopes to skip instead of scopes that are active
- Rename ActiveConditionalTestScopes -> SkippedTestScopes (clearer intent)
- Use __all__ sentinel when all scopes should be skipped
- Global triggers now simply don't set the variable (natural safe default)
- Restructure ConditionalTests.props with section headers for clarity
- Use if/else chain in evaluation script instead of repeated !shouldRun guards
- Update documentation decision flow and safe defaults

Co-authored-by: Copilot App <[email protected]>
Include test/TestAssets/TestPackages/TemplateEngine/** so changes to
template engine test packages also trigger the TemplateEngine scope.

Co-authored-by: Copilot App <[email protected]>
@MichaelSimons
MichaelSimons force-pushed the michaelsimons-conditionalize-illinker-tests branch from 90f3554 to 35b65d6 Compare July 8, 2026 20:16
Validates that all TriggerPaths and GlobalTriggerPaths in ConditionalTests.props
reference directories that actually exist in the repo. Catches stale paths after
folder renames or refactors.

Co-authored-by: Copilot App <[email protected]>
@nagilson

nagilson commented Jul 8, 2026

Copy link
Copy Markdown
Member

You are correct that defining just the src path for the affected area is often not sufficient. I did not elaborate on this in the documentation but should call it out in order to help devs and AI make better decisions when defining new test scopes.

@MichaelSimons Perhaps ambitious, but as a starting point would we want to consider a one-time build of the msbuild project dependency graph - and rebuild it whenever the project file changes? This would give us a better idea of what other TriggerPaths definitions to include without requiring the consumer to have to do that.

@MichaelSimons
MichaelSimons force-pushed the michaelsimons-conditionalize-illinker-tests branch 2 times, most recently from d8c0efc to 25bf3eb Compare July 8, 2026 20:32
@MichaelSimons

Copy link
Copy Markdown
Member Author

Perhaps ambitious, but as a starting point would we want to consider a one-time build of the msbuild project dependency graph - and rebuild it whenever the project file changes? This would give us a better idea of what other TriggerPaths definitions to include without requiring the consumer to have to do that.

@nagilson - Dependency graph resolution is an interesting idea. For now though, I'd like to get a few scopes defined and validate the realized filtering gains before investing in additional infrastructure.

It's also worth keeping in mind that this doesn't have to be 100% perfect — these tests still run on every CI build. We're only filtering on PR validation. If we start seeing that PR validation is frequently missing things that CI catches, that's the signal to adjust/invest in more tooling. But I don't think we need that up front.

@MichaelSimons
MichaelSimons force-pushed the michaelsimons-conditionalize-illinker-tests branch from 25bf3eb to 4b56d28 Compare July 8, 2026 21:08
Move path validation into EvaluateConditionalTestScopes.cs which runs in the
pipeline with full repo access. Emits ##[warning] for stale paths. Remove the
Microsoft.NET.Infra.Tests project since Helix payloads don't include the source
tree needed for directory existence checks.

Co-authored-by: Copilot App <[email protected]>
MichaelSimons and others added 10 commits July 20, 2026 13:13
Validation failures now emit ##[error] and return exit code 1.
With continueOnError: true, the step shows as failed/orange but the
pipeline continues with all tests running (safe default).

Co-authored-by: Copilot App <[email protected]>
Ensures the variable always exists even if the evaluation step fails,
preventing AzDO from passing literal unsubstituted text to MSBuild.

Co-authored-by: Copilot App <[email protected]>
Use String.Replace to prepend RepoRoot to each entry after splitting,
so test/Foo/**;test/Bar/** becomes C:\repo\test/Foo/**;C:\repo\test/Bar/**
and MSBuild resolves each glob independently.

Co-authored-by: Copilot App <[email protected]>
…aths

By default, git enables rename detection (diff.renames=true since git 2.9).
With rename detection, --name-only only shows the destination path, which
means renaming a file OUT of a trigger path wouldn't trigger the associated
tests. Using --no-renames ensures both the old and new paths appear in the
diff output, so trigger paths match correctly in all rename scenarios.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 4b54c4d1-6f40-4b47-9736-a7cf279126ff
The previous implementation converted ** to .* but left the literal /
after it, requiring at least one directory separator. Patterns like
src/**/*.cs would fail to match src/Foo.cs (zero intermediate segments).

Fix by handling **/ as a unit, converting it to (.+/)? which optionally
matches one-or-more chars ending in /. Trailing ** (e.g. src/**) still
uses .* and works as before.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 4b54c4d1-6f40-4b47-9736-a7cf279126ff
MichaelSimons added a commit that referenced this pull request Jul 20, 2026
test/AGENTS.md:
- Add SdkTest, SdkTestContext, and test asset placement to Conventions
- Add Helix deployment section for TestExecutionDirectoryFiles and
  when to validate in a simulated Helix layout

.github/copilot-instructions.md:
- Add Target framework properties table to Coding Style covering
  SdkTargetFramework, .NET Framework multi-targeting, and
  CurrentTargetFramework for test assets

Learnings from PR #55166 where tests were initially written without
Helix compatibility.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 76a86ef3-d73c-451c-8f6e-cef56bf3509c
@MichaelSimons
MichaelSimons force-pushed the michaelsimons-conditionalize-illinker-tests branch 2 times, most recently from 8ddd077 to 3177691 Compare July 20, 2026 23:02
Add Microsoft.NET.Infrastructure.Tests project with end-to-end tests for
both the EvaluateConditionalTestScopes script and the
RemoveSkippedConditionalTestProjects MSBuild target.

Tests cover: scope evaluation logic, glob matching, CI/dependency-flow
detection, global trigger paths, __all__/__none__ semantics, branch
filtering, and validation of required metadata (TriggerPaths, TestProjects,
base directory existence).

Uses .NET 11 Process.RunAndCaptureTextAsync API for clean subprocess
invocation with surgical env var removal (MSBUILD_EXE_PATH, MSBuildSDKsPath,
MSBuildExtensionsPath) to avoid test framework interference.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 4b54c4d1-6f40-4b47-9736-a7cf279126ff
@MichaelSimons
MichaelSimons force-pushed the michaelsimons-conditionalize-illinker-tests branch from a6b1fbd to eec0218 Compare July 20, 2026 23:06
MichaelSimons and others added 2 commits July 21, 2026 08:29
The test project must be in the solution so the initial build (without
RuntimeIdentifier) produces the DLL at the non-RID output path. The Helix
test infrastructure's CreateHelixTestWorkItems task reads the DLL via
GetTargetPath (which does not pass RuntimeIdentifier) to extract method
metadata for partitioning.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 4b54c4d1-6f40-4b47-9736-a7cf279126ff
Replace custom temp directory creation in TestEnvironment with the
existing TestAssetsManager.CreateTestDirectory() infrastructure. The
directory is named after the test class for easier traceability, and
uses the framework's standard test execution directory layout.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 4b54c4d1-6f40-4b47-9736-a7cf279126ff
@MichaelSimons
MichaelSimons requested a review from mthalman July 21, 2026 15:07
@MichaelSimons
MichaelSimons merged commit b2fa628 into main Jul 21, 2026
32 checks passed
@MichaelSimons
MichaelSimons deleted the michaelsimons-conditionalize-illinker-tests branch July 21, 2026 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants