azure-cli icon indicating copy to clipboard operation
azure-cli copied to clipboard

az cli version 2.0.74 commands write to stderr stream even when the command succeeds

Open tejasd1990 opened this issue 7 years ago • 13 comments

az feedback auto-generates most of the information requested below, as of CLI version 2.0.62

We own the "Azure CLI" Azure Pipelines task. We have come across 3 customer issues so far where az cli version 2.0.74 commands write to stderr stream even when the command succeeds Could be happening since some other older version, I don't know about that, but see this issue with at least this version.

Describe the bug We have come across 3 customer issues so far where az cli version 2.0.74 commands write to stderr stream even when the command succeeds At line:1 char:1

  • az extension add --name application-insights --output none 2>&1
  •   + CategoryInfo          : NotSpecified: (WARNING: Extens...eady installed.:String) [], RemoteException
      + FullyQualifiedErrorId : NativeCommandError
    
    

To Reproduce For example, az extension add --name application-insights --output none 2>&1

Expected behavior successful commands should not write to stderr stream

Environment summary Azure Pipelines Hosted Agent, Powershell on my local machine

Additional context Add any other context about the problem here.

tejasd1990 avatar Oct 16 '19 07:10 tejasd1990

Hi, We have experienced this issue as well.

But according to the reply from this comment (https://github.com/Azure/azure-cli/issues/9891#issuecomment-539316006), this is the expected behavior.

So, what is the right approach? IMO, the error stream should not contain any garbage that is not really related to errors happened during the execution.

dmitrykarnitski avatar Oct 16 '19 09:10 dmitrykarnitski

@jiasli please take a look.

yonzhan avatar Oct 19 '19 09:10 yonzhan

It is by-design to write warnings to stderr. To remove the warning, on Windows use

az extension add --name application-insights --output none 2>nul

On Linux use

az extension add --name application-insights --output none 2>/dev/null

jiasli avatar Nov 19 '19 11:11 jiasli

@jiasli using Azure DevOps pipelines to execute scripts it is common to turn on "Fail on Standard Error" because commands don't always exit with a non-zero value. Having Azure CLI commands output non-fatal errors to stderr makes it much more difficult to capture actual errors from CLI commands while still dealing with other script elements that don't behave nicely in other ways.

tvanfosson avatar Nov 21 '19 18:11 tvanfosson

Can we close this issue?

yonzhan avatar Dec 15 '19 11:12 yonzhan

@tvanfosson if you see any az command that returns zero value when it fails, please let us know. We will consider adding --no-warning or --quite switch in the future.

jiasli avatar Dec 24 '19 03:12 jiasli

@jiasli we don't limit ourselves to just Azure CLI commands in our scripts. It's hard to definitely say that we can rely on command exit values only.

tvanfosson avatar Dec 30 '19 17:12 tvanfosson

@jiasli

It is by-design to write warnings to stderr. To remove the warning, on Windows use

az extension add --name application-insights --output none 2>nul

This didn't work for me. Appending 2>null to --output none results in the following output:

out-file : FileStream was asked to open a device that was not a file. For support for devices like 'com1:' or 'lpt1:', 
call CreateFile, then use the FileStream constructors that take an OS handle as an IntPtr.
At D:\a\1\s\automation\CreateTeam.ps1:120 char:13
+             $updateDenyPermissions = az devops security permission up ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (:) [Out-File], NotSupportedException
    + FullyQualifiedErrorId : FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand

smholvoet avatar Jan 09 '20 15:01 smholvoet

For PowerShell, please use $null as the destination of the redirection:

az extension add --name application-insights --output none 2>$null

jiasli avatar Jan 10 '20 05:01 jiasli

That fixed it, thanks.

smholvoet avatar Jan 10 '20 09:01 smholvoet

We have added option --only-show-errors in #12604 to disable warnings that are written to stderr. It will be included in S167 release.

jiasli avatar Mar 24 '20 13:03 jiasli

-bash: $null: ambiguous redirect getting this error, after using 2>$null

Rajuvimal avatar Mar 24 '22 04:03 Rajuvimal

@Rajuvimal $null is for PowerShell. On bash use 2>/dev/null

crytlig avatar Mar 30 '22 11:03 crytlig