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

ANSI escape color codes broken on Git Bash in Windows Terminal #19391

Closed
tiesmaster opened this issue Aug 30, 2021 · 4 comments
Closed

ANSI escape color codes broken on Git Bash in Windows Terminal #19391

tiesmaster opened this issue Aug 30, 2021 · 4 comments
Assignees
Labels
Core CLI core infrastructure customer-reported Issues that are reported by GitHub users external to the Azure organization.

Comments

@tiesmaster
Copy link

tiesmaster commented Aug 30, 2021

Describe the bug
Running the az CLI on Windows using Git Bash in the Windows Terminal shows color output is broken. This is specific to this situation (see below for more context). When running $ az help, the output ends with:

[...more output elided for brevity...]
←[1mFor more specific examples, use: az find "az "←[0m

←[0mPlease let us know how we are doing: ←[96mhttps://aka.ms/azureclihats←[0m
←[0mand let us know if you're interested in trying out our newest features: ←[96mhttps://aka.ms/CLIUXstudy←[0m

To Reproduce

  1. Install latest Windows Terminal
  2. Install latest Git Bash, and enable the checkbox to add the "Windows Terminal" profile
  3. Install latest Azure CLI
  4. Start Windows Terminal, and open the Git Bash profile
  5. Run: $ az help

Expected behavior
Output is showing colors.

Environment summary

Windows-10-10.0.19041-SP0
Python 3.8.9
Installer: MSI

azure-cli 2.27.1

Windows Terminal
Version: 1.9.1942.0

Git for Windows: 2.33.0.2

Additional context
I ran into this when setting up a new dev box, and also found out the following details:

  • when starting the powershell profile within Windows Terminal, the output is colored correctly
  • when starting Git Bash from the Start Menu (this uses the mintty terminal that comes preinstalled with the Git for Windows install), the output is not colored, but the escape codes don't appear
  • when running the Azure CLI on my old system, the coloring output is working inside the Windows Terminal (same version) in Git Bash. The version of the Azure CLI that's installed there is: 2.15.1
  • running ANSI escape sequences like this does ouput colors

UPDATE: Results of some additional testing

I wanted to be sure that the reproduction steps are sound on a new system, with pretty much all the defaults, and installing from MSI, or Windows Store (for the Windows Terminal). So I created a new VM in Azure with Windows 10 Pro 20H2, and managed to reproduce this in this fresh box.

I also did some additional research where this broke, so I did a "git bisect" but then via installing the MSIs, and there are my results:

  • 2.18: PASS
  • 2.20: PASS
  • 2.21: PASS
  • 2.22: FAIL
  • 2.23: FAIL
  • 2.27.2: FAIL

Needless to say, version 2.22 broke this.

@ghost ghost added needs-triage This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that customer-reported Issues that are reported by GitHub users external to the Azure organization. labels Aug 30, 2021
@yonzhan yonzhan added Core CLI core infrastructure and removed question The issue doesn't require a change to the product in order to be resolved. Most issues start as that needs-triage This is a new issue that needs to be triaged to the appropriate team. labels Aug 30, 2021
@yonzhan yonzhan added this to the Backlog milestone Aug 30, 2021
@yonzhan
Copy link
Collaborator

yonzhan commented Aug 30, 2021

@jiasli for awareness

@jiasli
Copy link
Member

jiasli commented Sep 2, 2021

Azure CLI has been affected by colorama bugs for a long time (microsoft/knack#209). That's why we stopped initializing colorama in Linux terminals (microsoft/knack#238).

If we check isatty in the original Git Bash terminal, it shows:

$ "/c/Program Files (x86)/Microsoft SDKs/Azure/CLI2/python.exe" -c "import sys; print(sys.stdin.isatty())"
False

This corrupts the color detection logic:

https://github.com/microsoft/knack/blob/03b46abc9735fdc88e90b89b9c88192758a83277/knack/cli.py#L276-L278

        if sys.stdout.isatty() and sys.stderr.isatty() and self.out_file is sys.stdout:
            self.init_debug_log.append("Enable color in terminal.")
            return True

CLI thinks this is not a TTY and turns off the color, even though escape sequences are indeed supported.

$ "/c/Program Files (x86)/Microsoft SDKs/Azure/CLI2/python.exe" -c 'print("\x1b[36mhello world\x1b[0m")'

image

Found these discussions:

However, in Git Bash running in Windows Terminal, isatty returns True:

$ "/c/Program Files (x86)/Microsoft SDKs/Azure/CLI2/python.exe" -c "import sys; print(sys.stdin.isatty())"
True

But escape sequences can't be displayed correctly:

$ "/c/Program Files (x86)/Microsoft SDKs/Azure/CLI2/python.exe" -c 'print("\x1b[36mhello world\x1b[0m")'

image

Discussions on the topic:

So this is actually a "Git Bash in Windows Terminal" problem.

For now, I would recommend these workarounds:

  • Disable color with the no_color configuration:
    az config set core.no_color=True
    
  • Use other shells (like PowerShell, WSL) officially supported by Azure CLI

@jiasli jiasli closed this as completed Sep 2, 2021
@jiasli jiasli modified the milestones: Backlog, Aug 2021 (2021-09-07) Sep 2, 2021
@tiesmaster
Copy link
Author

@jiasli Thanks for the detailed write-up. I suspected it was a simple regression, perhaps pulling in a new version of a 3rd party dependency that regressed, however, it's not unsurprising that the plot thickens this much with things like git-for-windows, and the WT 😅

The workaround is really helpful, I didn't knew about that. It really makes the output usable, and made my day ❤️ Thnx!

@jiasli
Copy link
Member

jiasli commented Sep 4, 2021

I am glad to know the workaround works for you! Please free feel to contact us if there are any problems. 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Core CLI core infrastructure customer-reported Issues that are reported by GitHub users external to the Azure organization.
Projects
None yet
Development

No branches or pull requests

3 participants