Textualize / rich

Rich is a Python library for rich text and beautiful formatting in the terminal.
https://rich.readthedocs.io/en/latest/
MIT License
49.73k stars 1.73k forks source link

[BUG] Syntax content is dedented and stripped in GitHub Actions #3500

Closed AAriam closed 2 months ago

AAriam commented 2 months ago

Describe the bug

from rich.console import Console
from rich.panel import Panel
from rich.syntax import Syntax

yaml_syntax = """
test: true
examples:
    ex1: 1
    ex2: 2
"""
code_panel = Panel(Syntax(yaml_syntax, lexer="yaml"))
console = Console(force_terminal=True)
console.print(code_panel)

Works fine in a terminal:

Screenshot 2024-09-25 at 17 07 19

But not on GitHub Actions:

[!TIP] When force_terminal is not set for Console, the problem is solved, but then of course you don't get any syntax highlighting!

Screenshot 2024-09-25 at 17 07 54

[!NOTE] The panel is just there to better illustrate the issue; the results are the same without it.

github-actions[bot] commented 2 months ago

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

willmcgugan commented 2 months ago

This isn't a Rich issue. You might want to raise it with Github?

AAriam commented 2 months ago

Hi @willmcgugan,

Thanks for your time and the great work! Do you maybe have an idea what exactly is happening there?

willmcgugan commented 2 months ago

If I were to guess. It looks like it interpreting the output as HTML, which will collapse spaces.

AAriam commented 2 months ago

So, basically what's happening is that GitHub Actions ignores any whitespace-only segment that is enclosed by two ANSI sequences, or one ANSI and the end of line:

Sample workflow to reproduce:

on: workflow_dispatch
jobs:
  test-ansi:
    runs-on: ubuntu-latest
    steps:
      - shell: python
        run: |
          print("The following must print a red line but it does not:")
          print("\033[48;2;255;0;0m                                  ")

          print("It only prints the line if it has a non-space character:")
          print("\033[48;2;255;0;0m               X                  ")

          print("Leading spaces are normally respected:")
          print("                        \033[48;2;255;0;0m     X    ")

          print("But not when they are enclosed by ANSI code:")
          print("\033[48;2;255;0;0m                       \033[0m  X ")

          print(
            "Basically, all segments containing only spaces are ignored "
            "when enclosed between two ANSI sequences, "
            "or one ANSI sequence and the end of the line:"
          )
          print("\033[48;2;255;0;0m         \033[48;2;0;255;0m           \033[48;2;0;0;255m          ")

Output: Screenshot 2024-09-25 at 23 04 55

Thanks for the reply @willmcgugan; I opened a ticket with GitHub.

github-actions[bot] commented 2 months ago

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

willmcgugan commented 2 months ago

Nice sleuthing. Do you have a link to the issue? I’d like to follow it.

AAriam commented 2 months ago

I opened an issue at https://support.github.com/contact/bug-report, the issue is at https://support.github.com/ticket/3019506, but I think its only accessible by me?