diff --git a/src/System.Management.Automation/engine/parser/Parser.cs b/src/System.Management.Automation/engine/parser/Parser.cs
index 149c971127c..b0b84572dd5 100644
--- a/src/System.Management.Automation/engine/parser/Parser.cs
+++ b/src/System.Management.Automation/engine/parser/Parser.cs
@@ -8059,7 +8059,7 @@ internal ParseError(IScriptExtent extent, string errorId, string message, bool i
///
public override string ToString()
{
- return PositionUtilities.VerboseMessage(Extent) + "\n" + Message;
+ return PositionUtilities.VerboseMessage(Extent) + Environment.NewLine + Message;
}
///
diff --git a/test/powershell/Language/Parser/Parsing.Tests.ps1 b/test/powershell/Language/Parser/Parsing.Tests.ps1
index 586d3b213c1..8b79d994744 100644
--- a/test/powershell/Language/Parser/Parsing.Tests.ps1
+++ b/test/powershell/Language/Parser/Parsing.Tests.ps1
@@ -459,3 +459,23 @@ Describe "Ternary Operator parsing" -Tags CI {
$expr.IfFalse | Should -BeOfType System.Management.Automation.Language.ConstantExpressionAst
}
}
+
+Describe "ParserError type tests" -Tag CI {
+ # This test was added because there use to be a hardcoded newline in the ToString() method of
+ # the ParseError class. This makes sure the proper newlines are used.
+ It "Should use consistant newline depending on OS" {
+ $ers = $null
+ [System.Management.Automation.Language.Parser]::ParseInput('$x =', [ref]$null, [ref]$ers) | Out-Null
+ $measureResult = $ers[0].ToString() -split [System.Environment]::NewLine | Measure-Object
+
+ # We expect the string to have 4 lines. That means that if we split by NewLine for that platform,
+ # We should have 4 as the count.
+ $measureResult.Count | Should -BeExactly 4
+
+ # Just checking the above is not enough. We should also make sure that on non-Windows, there are no
+ # `r`n
+ if (!$IsWindows) {
+ $measureResult = $ers[0].ToString() | Should -Not -Contain "`r`n"
+ }
+ }
+}