Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
734 views
in Technique[技术] by (71.8m points)

visual studio - PowerShell displays some git command results as error in console even though operation was successful

When I execute Git commands in PowerShell everything is working great except one minor difference between the PowerShell console and the NuGet console. The output from a "git push" is displayed in red error text in the NuGet window but displays fine in the PowerShell window.

Here is a video showing the difference. "git push" displays its results as an error in the package manager console. The operation worked, it's just annoying that the output from the operation is displayed as an error.

See the video

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Root cause: git push is sending output to stderr, not stdout. See here, here.

Powershell.exe as a host does not get worried when native tools send output to stderr, since this is a somewhat common way to print not just error messages but status messages and other stuff. For example, try running something totally bogus like

PS C:> $result = findstr.exe q w e r t y

Findstr is sending error messages to stderr, so Powershell.exe knows not to assign that "error" output to your variable, but it also doesn't freak out.

The NuGet package manager host, on the other hand, is not as smart in this regard. When running any native tool, this host interprets anything in stderr as being truly an error. Thus you get the red text, diagnostic messages, etc. Try the same findstr example above in the PM, you will see full errors.

A couple of workarounds/suggestions:

  • Use the --porcelain parameter, which causes output to go to stdout, not stderr.
  • Set $errorView = 'CategoryView' which will at least minimize the error messages, though not remove them
  • Redirect stderr and do a plain console write like this: git push 2>&1 | write-host

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...