Skip to content

Change $ErrorActionPreference to not affect stderr output #13361

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 7, 2020

Conversation

SteveL-MSFT
Copy link
Member

@SteveL-MSFT SteveL-MSFT commented Aug 6, 2020

PR Summary

Based on @PowerShell/powershell-committee discussion, we agreed that stderr should not be treated as an ErrorRecord as many native commands use that as an alternate stream from stdout and it does not signify an error (like verbose or progress information). Stderr output is still wrapped as ErrorRecords, but the runtime no longer applies $ErrorActionPreference if the ErrorRecord comes from a native command. The diff makes the change look bigger than it is, but it's simply wrapping existing code that applies $ErrorActionPreference and writing to $Error to not apply if the ErrorRecord is simply wrapping stderr.

Updated TestExe to have a switch to write output to stderr.

PR Context

Fix #3996

PR Checklist

@SteveL-MSFT
Copy link
Member Author

CodeFactor is an existing issue complaining about the complexity of the method that was updated. Not going to change in this PR.

@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Aug 6, 2020
@daxian-dbw daxian-dbw merged commit 511858a into PowerShell:master Aug 7, 2020
@felixfbecker
Copy link
Contributor

Can the stderr ErrorRecords still be captured with -ErrorVariable?

@daxian-dbw daxian-dbw added CL-BreakingChange Indicates that a PR should be marked as a breaking change in the Change Log CL-Engine Indicates that a PR should be marked as an engine change in the Change Log labels Aug 10, 2020
@daxian-dbw daxian-dbw added this to the 7.1.0-preview.6 milestone Aug 10, 2020
@daxian-dbw
Copy link
Member

@felixfbecker No it cannot be captured by -ErrorVariable after this change. I believe this is intentional but @SteveL-MSFT can clarify it.

@felixfbecker
Copy link
Contributor

Basically my question is: How can a user still run a native command, get the stdout output if it was successful, but also get access to the stderr output if it was unsuccessful? That's a very important use case.
Maybe this is solved in conjunction with #3415, e.g. the stderr output would be available on the exception object?

@daxian-dbw
Copy link
Member

How can a user still run a native command, get the stdout output if it was successful, but also get access to the stderr output if it was unsuccessful? That's a very important use case.

I'm curious how this is done today in console before this PR? In console, unless we redirect the stderr stream, the message written to stderr from the native command will always handled by the console (print on console).

Maybe this is solved in conjunction with #3415, e.g. the stderr output would be available on the exception object?

The discussion on it so far is that the exception only has the exit code, not the content from stderr.
The idea is: many native commands write to stderr not as error messages, but an alternate stream for additional information that doesn't make sense to write to stdout. This includes content such as verbose or progress output.
But that can be further discussed in #3415.

@ghost
Copy link

ghost commented Aug 17, 2020

🎉v7.1.0-preview.6 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-BreakingChange Indicates that a PR should be marked as a breaking change in the Change Log CL-Engine Indicates that a PR should be marked as an engine change in the Change Log
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent handling of native command stderr
5 participants