Guard TestOutputHelper.WriteLine calls against lifetime exceptions #732
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.
Fix XUnit TestOutputHelper Lifetime Issue ✅
Solution Implemented:
Created a centralized
Helpersmodule with a singlesafeWriteLinefunction that wrapsTestOutputHelper.WriteLine()calls in try-catch blocks to handleInvalidOperationExceptionwhen the test has completed and the helper is no longer active. This prevents the error "There is no currently active test" when closures that capture the TestOutputHelper are called after the test lifetime ends.Root Cause:
The TestOutputHelper was being captured in closures for the
EveryandEveryShrinkcallbacks when verbose mode is enabled. These closures can outlive the test's lifetime, especially when tests run in parallel or sequentially. When XUnit 2.9.0+ introduced stricter lifetime management for TestOutputHelper, these lingering references would throwInvalidOperationExceptionwhen accessed.Test Results:
Files Changed:
src/FsCheck.Xunit/Helpers.fs- New internal module with centralized safeWriteLine helpersrc/FsCheck.Xunit/FsCheck.Xunit.fsproj- Added Helpers.fs to compilation ordersrc/FsCheck.Xunit/PropertyAttribute.fs- Updated to use Helpers.safeWriteLinesrc/FsCheck.Xunit/CheckExtensions.fs- Updated to use Helpers.safeWriteLinesrc/FsCheck.Xunit/Runner.fs- Updated to use Helpers.safeWriteLinetests/FsCheck.Test/Fscheck.XUnit/PropertyAttributeTests.fs- Added F# lifetime testsDirectory.Packages.props- Removed implicit Microsoft.NETFramework.ReferenceAssemblies package referenceOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.