- Notifications
You must be signed in to change notification settings - Fork8.1k
Respect $VerbosePreference in ShouldProcess#26511
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Respect $VerbosePreference in ShouldProcess#26511
Conversation
ShouldProcess was only emitting verbose output when the -Verboseparameter was explicitly specified, ignoring the $VerbosePreferencevariable setting.Changed to use IsWriteVerboseEnabled() instead of checking theVerbose flag directly in both DoShouldProcess() andCalculatePossibleShouldProcessOptimization(). This method properlyrespects both the -Verbose parameter and $VerbosePreference variable.
iSazonov commentedNov 22, 2025
From#12148 (comment)
@yotsuda Could you please add test for |
| Context"VerbosePreference variable" { | ||
| It"Emits verbose output when VerbosePreference is Continue" { | ||
| $VerbosePreference='Continue' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Please save and then restore the value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Thanks for the review! Fixed in5d8a7d1. All tests now save and restore $VerbosePreference using try-finally blocks.
- Add test case for -Verbose:$false taking precedence over $VerbosePreference- Save and restore $VerbosePreference in all test cases using try-finally blocks- Clean up whitespace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Pull request overview
This PR fixes a bug whereShouldProcess was only emitting verbose output when the-Verbose parameter was explicitly specified, completely ignoring the$VerbosePreference variable. This makes the behavior consistent with how other cmdlets handle verbose output by properly checking both the parameter and the preference variable.
Key Changes:
- Changed two locations in
DoShouldProcess()andCalculatePossibleShouldProcessOptimization()from checkingthis.Verboseto callingIsWriteVerboseEnabled() - Added comprehensive test coverage for both
$VerbosePreferencevariable and-Verboseparameter scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/System.Management.Automation/engine/MshCommandRuntime.cs | Fixed two method calls to useIsWriteVerboseEnabled() instead ofthis.Verbose to properly respect both-Verbose parameter and$VerbosePreference variable |
test/powershell/Language/Scripting/ShouldProcess.Tests.ps1 | Added new test file with comprehensive coverage forShouldProcess verbose output behavior with both preference variable and parameter scenarios |
Comments suppressed due to low confidence (1)
src/System.Management.Automation/engine/MshCommandRuntime.cs:1649
- Both branches of this 'if' statement return - consider using '?' to express intent better.
if (IsWriteVerboseEnabled()) { return ShouldProcessPossibleOptimization.AutoYes_CanCallShouldProcessAsynchronously; } else { return ShouldProcessPossibleOptimization.AutoYes_CanSkipShouldProcessCall; }💡Add Copilot custom instructions for smarter, more guided reviews.Learn how to get started.
iSazonov commentedNov 24, 2025
@yotsuda Please look test errors. |
- Add ShouldEmitVerboseFromShouldProcess() method that checks only -Verbose parameter and $VerbosePreference variable- Excludes -Debug parameter effect to avoid unintended side effects- Update History.Tests.ps1 expected count from 12 to 13 to reflect the correct behavior when $VerbosePreference is 'Continue'
yotsuda commentedNov 26, 2025
Fixed ince12d77. The original implementation using Created a dedicated method
Also updated |
iSazonov commentedNov 26, 2025
Hmm, |
yotsuda commentedNov 27, 2025
@iSazonov You're right that TheWG decision specifies only |
iSazonov commentedNov 29, 2025
Thanks for clarify! I don't see that WG discussed the influence of |
SteveL-MSFT commentedDec 17, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Cmdlet-WG already reviewed and approved the issue, so this is good from a behavior perspective |
iSazonov commentedDec 17, 2025
@SteveL-MSFT It is not clear should we take into account the influence of -Debug switch presence or ignore it as implemented in the PR? |
PR Summary
Fix
ShouldProcessto respect$VerbosePreferencevariable in addition to-Verboseparameter.PR Context
Fixes#12148
$PSCmdlet.ShouldProcess()was only emitting verbose output when the-Verboseparameter was explicitly specified, completely ignoring the$VerbosePreferencevariable setting. This is inconsistent with how other cmdlets handle verbose output.The issue occurred because
DoShouldProcess()andCalculatePossibleShouldProcessOptimization()were checkingthis.Verbose(which only tracks the-Verboseparameter flag) instead of callingIsWriteVerboseEnabled()(which properly checks both-Verboseparameter and$VerbosePreferencevariable).Changes:
MshCommandRuntime.csto useIsWriteVerboseEnabled()instead ofthis.Verbosein two locationsBefore:
After:
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header