1

I am working on a set of build scripts which are called from a ubuntu hosted CI environment. The powershell build script calls jest via react-scripts via npm. Unfortunately jest doesn't use stderr correctly and writes non-errors to the stream.

I have redirected the error stream using 3>&1 2>&1 and this works fine from just powershell core ($LASTEXITCODE is 0 after running, no content from stderr is written in red).

However when I introduce docker via docker run, the build script appears to not behave and outputs the line that should be redirected from the error stream in red (and crashes). i.e. something like: docker : PASS src/App.test.js. Error: Process completed with exit code 1..

Can anyone suggest what I am doing wrong? because I'm a bit stumped. I include the sample PowerShell call below:-

function Invoke-ShellExecutable
{
    param (
        [ScriptBlock]
        $Command
    )

    $Output = Invoke-Command $Command -NoNewScope | Out-String

    if($LASTEXITCODE -ne 0) {
        $CmdString = $Command.ToString().Trim()
        throw "Process [$($CmdString)] returned a failure status code [$($LASTEXITCODE)]. The process may have outputted details about the error."
    }

    return $Output
}

Invoke-ShellExecutable { 
  ($env:CI = "true") -and (npm run test:ci)
} 3>&1 2>&1
chris
  • 75
  • 5
  • 1
    Don't use `-and`: it'll only ever return `$true` or `$false`, and your `npm` call's output will be lost - see [this answer](https://stackoverflow.com/a/41816341/45375). Also note that your environment variable definition will linger. If you `throw` before returning output, you won't be able to inspect the output. Move the redirection (`2>&1` will do) inside the `Invoke-ShellExecutable` call, to `Invoke-Command`. More simply, use `$Output = . $Command 2>&1 | Out-String`. (Or use the more typical `&` instead of `.`, as scoping doesn't apply to _external programs_.) – mklement0 Jul 03 '21 at 22:29
  • Thanks this helped me get to my answer. There was also an interaction with $ErrorActionPreference = "Stop" which was present at the top of the file. Which seems to get reset even when you force no new scope. I really don't understand why exit behaviour and error behaviour have any affect on how stderr error is reported to the console. – chris Jul 06 '21 at 23:07
  • Glad to hear it. As for the surprising interaction between `$ErrorActionPreference = 'Stop'` and stderr output from external programs, see [this answer](https://stackoverflow.com/a/48877892/45375). – mklement0 Jul 06 '21 at 23:32

0 Answers0