SharpeRAD / Cake.Powershell

Powershell addin for Cake
http://cakebuild.net
MIT License
83 stars 36 forks source link

Powershell output with curly brackets written by Out-Host causing FormatException #51

Open asci13 opened 6 years ago

asci13 commented 6 years ago

I'm using Cake.Powershell version 0.4.3 to run pester powershell unit tests. The output that comes from pester sometimes includes curly brackets (like for example if ($true) { return $true } - especially in the code coverage usecase. If that happens, we're seeing a FormatException.

Reproduction steps: 1) Test code with cake available in this sample repo: https://github.com/asci13/Cake_Powershell_Repro 2) run build.ps1 3) the error occurs when running the script in the RunScriptWithAddin task. The RunScriptWithProcess task is the workaround we're currently using.
I'm running this in VSCode 1.20.1 on Windows.

This is a little test script that produces the error the same way Pester coverage does. It will only happen with Out-Host, not with Write-Host (out-host is what pester uses to report coverage data).

var script= "'if ($true) { return $a - $b }' | Out-Host";
var results = StartPowershellScript(script, settings);

Output of the RunScriptWithAddin task (including full stack trace):

Executing: 'if ($true) { return $a - $b }' | Out-Host
if ($true)
An error occurred when executing task 'RunScriptWithAddin'.
Error: One or more errors occurred.
        Failed to Execute Powershell Script: System.Management.Automation.CmdletInvocationException: Input string was not in a correct format. ---> System.FormatException: Input string was not in a correct format.
   at Cake.Core.Diagnostics.Formatting.FormatParser.ParseProperty(TextReader reader)
   at Cake.Core.Diagnostics.Formatting.FormatParser.<Parse>d__0.MoveNext()
   at Cake.Core.Diagnostics.CakeBuildLog.Write(Verbosity verbosity, LogLevel level, String format, Object[] args)
   at Cake.Powershell.CakePSHostInterface.WriteLine(String value)
   at Microsoft.PowerShell.Commands.Internal.Format.ConsoleLineOutput.OnWriteLine(String s)
   at Microsoft.PowerShell.Commands.Internal.Format.ConsoleLineOutput.WriteLine(String s)
   at Microsoft.PowerShell.Commands.Internal.Format.OutCommandInner.ProcessPayload(FormatEntryData fed, OutputContext c)
   at Microsoft.PowerShell.Commands.Internal.Format.OutCommandInner.ProcessObject(PSObject so)
   at Microsoft.PowerShell.Commands.Internal.Format.OutCommandInner.ProcessRecord()
   at System.Management.Automation.CommandProcessor.ProcessRecord()
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.DlrScriptCommandProcessor.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
   at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
   at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()

After a look into the repo it might have something to do with the escaping of curly brackets.

The workaround mentioned above is fine for now, but using the Cake.Powershell Addin would be much better and cleaner, so it would be very much appreciated if you could take a look at this and let me know if I can provide any more information to help.

SharpeRAD commented 6 years ago

We already escape curly brackets so the issue will be with how Cake.Core formats logs.

Check the output from Pester and see what character combination is causing Cake to throw, then we could look at escaping it. Provided escaping doesn't effect standard output.

asci13 commented 6 years ago

Thanks for looking into it! I was a bit unsure where the issue belongs exactly.

The exception occurs when there's both a { and a } in the same line. If there's only one of them present, it works fine. Whether there is other characters between the brackets seems to make no difference.

asci13 commented 6 years ago

This issue still exists with Cake 0.28.0 and Cake.Powershell 0.4.5. Unfortunately i'm not sure if this is an issue of Cake.Powershell or Cake itself - if its on the Cake end of things i'll happily move the issue there.