Skip to content
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

Azure CLI commands return character '[0m' at the end of the command output #9903

Closed
jlausuch opened this issue Jul 10, 2019 · 24 comments
Closed
Assignees
Labels
Core CLI core infrastructure Output

Comments

@jlausuch
Copy link

jlausuch commented Jul 10, 2019

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

Describe the bug
I get the char sequence '[0m' at the end of the command output.

To Reproduce
any az command.
It doesn't happen if the command is pipped or redirected to a file.

Expected behavior
It should not print that character.

Environment summary

 sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
 zypper addrepo --name "Azure CLI" --check https://packages.microsoft.com/yumrepos/azure-cli azure-cli
 zypper in --from azure-cli -y azure-cli

Additional context

$ az -v

azure-cli                         2.0.68

command-modules-nspkg               2.0.3
core                              2.0.68
nspkg                              3.0.4
telemetry                          1.0.3

Python location '/usr/bin/python2'
Extensions directory '/root/.azure/cliextensions'

Python (Linux) 2.7.14 (default, Nov 30 2017, 11:33:17) [GCC]

Legal docs and information: aka.ms/AzureCliLegal


Your CLI is up-to-date.
@marstr
Copy link
Member

marstr commented Jul 10, 2019

Howdy jlausuch, is this new to 2.0.68 or has this behavior been present for older versions as well?

I suspect that the terminal you're using isn't playing nice with our colorizer. It would explain why an unexpected control character is being output, and also why it doesn't happen when you're not using an interactive terminal session.

@jlausuch
Copy link
Author

It didn't happen with version 2.0.64 actually.

@marstr marstr added bug This issue requires a change to an existing behavior in the product in order to be resolved. and removed Packaging Needs Triage labels Jul 17, 2019
@charlesguoooo
Copy link

charlesguoooo commented Jul 18, 2019

I have the same problem in pycharm.The version is 2.0.69.

@marstr
Copy link
Member

marstr commented Jul 18, 2019

@charlesguoooo, is your platform also SUSE?

@charlesguoooo
Copy link

@marstr no, It was happened in pycharm on mac. look this

#!/usr/bin/env python

import re
import json
import subprocess

result = subprocess.check_output(["az account show"],shell=True)

print json.dumps(result)

# return result include \u001b[0m in the end

But if not in pycharm,Just run python test.py,the return result is right

@marstr
Copy link
Member

marstr commented Jul 29, 2019

I'm going to unassign myself, as this does not relate to the Packaging as originally believed. @zikalino, @yugangw-msft you guys may want to re-triage or re-assign this.

@marstr marstr removed their assignment Jul 29, 2019
@kh-sonrai
Copy link

Any updates on this issue? Still seeing this problem with az cli version 2.0.74 on mac when running in pycharm.
@zikalino @yugangw-msft

@haroldrandom haroldrandom added bug This issue requires a change to an existing behavior in the product in order to be resolved. Output labels Oct 25, 2019
@ellieayla
Copy link

ellieayla commented Oct 30, 2019

Also observing trailing escape character in 2.0.70 in IntelliJ IDEA 2019.2 with above snippet, with or without --output json. Presumably there's some automagical terminal-detection thing going on here?

@yonzhan yonzhan added this to the S162 milestone Oct 31, 2019
@jiasli
Copy link
Member

jiasli commented Nov 13, 2019

\x1b[0m is appended by colorama to reset the terminal color. @jlausuch, which terminal you are using? Bash and zsh should work fine with this ANSI escape code.

@jiasli
Copy link
Member

jiasli commented Nov 13, 2019

@charlesguoooo @kh-sonrai , the issue on PyCharm isn't limited to CLI. We can test by creating printcolor.py with only init():

from colorama import init
init()

Then call it in caller.py with PyCharm:

import subprocess

result = subprocess.check_output("<python_path>\\python.exe <absolute_path>\\printcolor.py")
decoded = result.decode(encoding='UTF-8')
print(decoded)

The actual output is empty but when debugging into it, we can see

result = {bytes} b'\x1b[0m'

This is because colorama registers reset_all for atexit which writes \x1b[0m at the end of stdout. https://github.com/tartley/colorama/blob/d7a538212c3b8d00dc0051a45f99dc9b201dd3b2/colorama/initialise.py#L45-L48

@yonzhan yonzhan modified the milestones: S162, S163 Dec 15, 2019
@yonzhan yonzhan added Core CLI core infrastructure and removed bug This issue requires a change to an existing behavior in the product in order to be resolved. labels Dec 30, 2019
@yonzhan yonzhan removed this from the S163 milestone Dec 30, 2019
@jiasli
Copy link
Member

jiasli commented Jan 13, 2020

We are not able to reproduce with docker on Mac.

docker run -it --rm mcr.microsoft.com/azure-cli:latest
az vm list | jq '.[].name'
az vm list | tail -c 16 | xxd
00000000: 6573 223a 206e 756c 6c0a 2020 7d0a 5d0a  es": null.  }.].

Are you running docker on Windows, Linux or Mac? If you run az vm list directly, is any colored output shown?

@Arnavion
Copy link
Contributor

Okay, I know what the problem is.

My jq / tail commands are outside the Docker container. ie my command is doing the equivalent of docker run -it --rm mcr.microsoft.com/azure-cli az vm list | jq , because my az is a shell function that effectively wraps docker run -it --rm mcr.microsoft.com/azure-cli az "$@". The az program inside the container thinks its stdout is not being redirected in this case, so it includes the escape codes in its output.

The -t that I'm passing to the docker run is why az thinks its output is not being redirected. If I take the -t out then it stops including the escape codes as desired. I only noticed now because it hadn't been printing escape codes in the older versions.

Sorry for the false alarm.

@jiasli
Copy link
Member

jiasli commented Jan 13, 2020

Thanks for the analysis. Per docker's doc,

-t : Allocate a pseudo-tty

This makes colorama think this is a tty and thus send the escaping chars.

@wouterh-dev
Copy link

Why does the azure-cli even output control characters when I request json output? And how can I turn this off? This is breaking my scripts

@yonzhan yonzhan modified the milestones: S164, S165 Feb 1, 2020
@yonzhan yonzhan modified the milestones: S165, S167 Feb 15, 2020
@yonzhan yonzhan modified the milestones: S167, S169 Mar 21, 2020
@marcin-vt
Copy link

It is still happening in az cli 2.5.1 (only in pycharm) and actually it is breaking DefaultAzureCredential (azure-identity 1.4.0b2) as it cannot parse token from az output (json.decoder.JSONDecodeError: Extra data)

@jiasli
Copy link
Member

jiasli commented May 5, 2020

@marcin-vt, could you try disabling the color following #12601 and see if it still happens?

@marcin-vt
Copy link

Thnx @jiasli - that helped!

@yonzhan
Copy link
Collaborator

yonzhan commented May 5, 2020

@marcin-vt was your issue resolved?

@marcin-vt
Copy link

Mine? Yes, adding the environmental variable solved the issue.

@jiasli
Copy link
Member

jiasli commented Jun 16, 2020

I have created issue tartley/colorama#263 on colorama's repo.

NVolcz added a commit to NVolcz/azure-sdk-for-python that referenced this issue Jun 27, 2020
The Azure CLI supports colored output by using colorama which resets the color
after execution by printing "[0m" if the terminal supports color. This can in
some cases cause the AzureCliCredential to fail for example when developing
in PyCharm: Azure/azure-cli#9903
Azure CLI allows color output to be disabled by setting the environment
variable:AZURE_CORE_NO_COLOR.

The PR in azure-cli: Azure/azure-cli#12601
@m4ss1m0g
Copy link

On azure-cli 2.35.0 and VSCode 1.66.2 is still preset

azure-cli                         2.35.0

core                              2.35.0
telemetry                          1.0.6

Extensions:
logic                              0.1.3

Dependencies:
msal                              1.17.0
azure-mgmt-resource               20.0.0

Python location 'C:\Users\xx\scoop\apps\azure-cli\current\python.exe'
Extensions directory 'C:\Users\xx\.azure\cliextensions'

Python (Windows) 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 12:53:06) [MSC v.1929 32 bit (Intel)]

Legal docs and information: aka.ms/AzureCliLegal


Your CLI is up-to-date.

Please let us know how we are doing: https://aka.ms/azureclihats
and let us know if you're interested in trying out our newest features: https://aka.ms/CLIUXstudy

Resolved setting AZURE_CORE_NO_COLOR environment variable to True

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core CLI core infrastructure Output
Projects
None yet
Development

No branches or pull requests