Fix exception when writing messages #2438
Merged
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.
Type
Related Issues?
Fixes #2185
What is in this Pull Request ?
Fix a "Index out of range" exception when writing messages to ConsoleHost with a Window width less than 8.
This happens in Azure Automation Runbooks as
cmdlet.Host.UI.RawUI.MaxWindowSize.Width
is set to 0, but happens also locally when setting a Width of 7.#Investigation
I added some logs to WordWrap to see how the "Index out of range" exception was thrown and it was caused by passing
maxLineLength
parameter with value -8 , callingtext.LastIndexOfAny(... , startIndex: -8)
which is invalidThis means
cmdlet.Host.UI.RawUI.MaxWindowSize.Width
is set to 0 in Azure Automation, which makes sense as there is no UI.I was able to trigger the same exception locally setting the Window Size of PowerShell 7.1 to 7 pixels:
The error is more readable when expanding the window after having triggered the exception:
#Tests
After excluding the logic of special formatting in case of a too small window, this is how it looks:
Width 120:
Width 9:
Width 7:
After expanding the window:
Testing it in Azure Automation connections correctly, see the output of
Get-PnPDiagnostics
with no exception and a connection to a site: