Skip to content

Status component ignores NO_COLOR when output is redirected to file #1810

@rcdailey

Description

@rcdailey

Information

  • OS: Windows and MacOS
  • Version: 0.50.0
  • Terminal: N/A (command is piped to a file)

Describe the bug
When the NO_COLOR environment variable is specified, the Status feature of Spectre.Console still writes ANSI escape sequences when piping the command to a file. While console output correctly honors NO_COLOR, redirected output (e.g., to a log file) still contains ANSI escape sequences. This appears to specifically affect the Status component's spinner and positioning escape sequences.

To Reproduce

  1. Set the NO_COLOR environment variable (e.g., export NO_COLOR=1 in shell configuration)
  2. Run a command that uses Spectre.Console's Status feature
  3. Pipe the output to a file (e.g., command > output.log or command >> output.log 2>&1)
  4. Examine the output file and observe ANSI escape sequences are still present

Expected behavior
When NO_COLOR is set, no ANSI escape sequences should be written to the output, regardless of whether output is displayed in the terminal or redirected to a file.

Screenshots
N/A

Additional context
Example of problematic output in redirected file containing escape sequences:

[?25l                              
⣷ Updating Git Repositories...
                              
[2A                              
⣯ Updating Git Repositories...
                              
[2A                              
⣟ Updating Git Repositories...
                              
[2A                              
⡿ Updating Git Repositories...
                              [WRN] Config Templates Repo: Deleting local git repo and retrying git operation due to error
[WRN] Config Templates Repo: Using explicit SHA1 for local repository: 123abc456def7890abcdef1234567890abcdef12
[INF] Config Templates Repo: Cloning...

[2A                              
⢿ Updating Git Repositories...
                              
[2K[1A[2K[1A[2K[?25h

Example code using the Status feature:

internal class ConsoleMultiRepoUpdater(
    IAnsiConsole console,
    IReadOnlyCollection<IUpdateableRepo> repos
) : IMultiRepoUpdater
{
    public async Task UpdateAllRepositories(bool hideConsoleOutput, CancellationToken token)
    {
        var options = new ParallelOptions { CancellationToken = token, MaxDegreeOfParallelism = 3 };

        var task = Parallel.ForEachAsync(
            repos,
            options,
            async (repo, innerToken) => await repo.Update(innerToken)
        );

        if (!hideConsoleOutput)
        {
            await console.Status().StartAsync("Updating Git Repositories...", _ => task);
        }
        else
        {
            await task;
        }
    }
}

I've successfully reproduced this issue consistently on:

  • Windows using both Git Bash and PowerShell terminals when piping to a file
  • macOS (M2 Max MacBook) using iTerm terminal

Interestingly, when the same command is run from crontab, the output doesn't contain ANSI sequences. This suggests that Spectre.Console might be correctly detecting some console/redirection environment conditions but not others.

The issue specifically occurs when using IAnsiConsole.Status() method with the NO_COLOR environment variable set and when output is redirected to a file through normal shell redirection.

For additional details and reproduction steps from a user of a project that uses Spectre.Console, please see: recyclarr/recyclarr#462

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions