Skip to content

Retain ANSI escape sequences in buffered builds without a tty #6323

@MisterDA

Description

@MisterDA

Hello all!
I've noticed that alcotest, the colorful test framework, doesn't show colors when run in CI (ocaml-ci or setup-ocaml in GitHub Actions). The culprit is that both CI systems run dune runtest, which removes ANSI escape sequences. A solution could be to run dune using --no-buffer. This won't write the tests logs to a file, but I think we don't care too much about that since users usually look at the log feed produced and don't extract the log files as artifacts from the CI run (in ocaml-ci that's not possible, with GHA this is possible with additional scripting). So, defaulting to --no-buffer seems sensible, but the manual page advices to use -j 1 to avoid interleaved output of parallel commands (and use the --verbose flag too, but that's less of a concern). It's a performance problem for me to disable parallelism.
Could it be possible to have the cake and eat it too? A buffered mode for Dune so that parallel command outputs are not interleaved and we can still have parallelism, that would act as a pass-through for ANSI escape codes?

For instance in Irmin there's always (run %{exe:test.exe} -q --color=always) in Dune stanzas, so people try to force colors (Alcotest already outputs them because of INSIDE_DUNE) but dune runtest removes them.
Furthermore, if I mock isatty to return false, tweak the action stanza with :

(action
  (pipe-stdout
   (run %{test})
   (run hexdump -C)))

and run

opam exec -- env LD_PRELOAD="$PWD/isatty_false.so" dune runtest

the output has (escaped) ANSI sequences, but run (unescaped) without hexdump -C they're suppressed by dune.

This is apparently controlled by Ansi_colors.strip. Could we add another escape hatch to prevent Dune from stripping the colors?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions