Repository navigation
fix(rollback): Protect directives during roll back process - #3817
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The updated trivia-preservation logic and test refactor contain correctness/reliability issues (directive trivia can still be stripped in some block shapes, and a helper compilation test can be order-dependent/flaky).
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR improves Stryker.NET’s C# rollback behavior so that rolling back statement/block mutations preserves directive trivia (notably #pragma warning), preventing “restored” code from failing to compile after a rollback.
Changes:
- Adjust rollback/instrumentation handling to better preserve trivia by wrapping/unwrapping blocks and carrying trivia through revert paths.
- Add a
HasSignificantTriviaRoslyn helper and centralize helperSyntaxTreecreation for injection with consistent parse options. - Extend/refactor unit tests to cover directive preservation during rollback and helper compilation across newer C# language versions.
File summaries
| File | Description |
|---|---|
| src/Stryker.Core/Stryker.Core/Mutants/CsharpNodeOrchestrators/BaseFunctionOrchestrator.cs | Unwraps nested single-statement blocks during instrumentation removal to better restore original shapes/trivia. |
| src/Stryker.Core/Stryker.Core/Instrumentation/IfInstrumentationEngine.cs | Adds block annotation + updated block handling intended to preserve trivia through inject/revert. |
| src/Stryker.Core/Stryker.Core/InjectedHelpers/CodeInjection.cs | Introduces a helper to produce helper SyntaxTrees with consistent encoding/options. |
| src/Stryker.Core/Stryker.Core/Initialisation/CsharpProjectComponentsBuilder.cs | Uses the new CodeInjection helper to inject helper syntax trees. |
| src/Stryker.Core/Stryker.Core/Helpers/RoslynHelper.cs | Adds HasSignificantTrivia to detect directive/comment trivia. |
| src/Stryker.Core/Stryker.Core.UnitTest/Mutants/CsharpMutantOrchestratorTests.cs | Adds a directive preservation regression test that compiles, triggers rollback, then asserts compilation succeeds. |
| src/Stryker.Core/Stryker.Core.UnitTest/InjectedHelpers/InjectedHelperTests.cs | Refactors helper compilation tests and expands language-version coverage. |
| src/Stryker.Core/Stryker.Core.UnitTest/Compiling/CSharpRollbackProcessTests.cs | Updates test references to align with memory-mapped-file helper usage. |
| src/Stryker.Core/Stryker.Core.UnitTest/AssertExtensions.cs | Improves syntax tree assertion output and removes an unused file assertion helper. |
Review details
- Files reviewed: 9/9 changed files
- Comments generated: 3
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
dff8454 to
79b2b6e
Compare
79b2b6e to
2a927f6
Compare
|



Improve statement/block mutations rollback logic to protect existing directives
Fixes #3799