Skip to content

Output line with ANSI escape code disappears #5528

@jmid

Description

@jmid

Expected Behavior

Consider this OCaml file (repro/test2.ml) using ANSI escape codes:

type color =  Normal | Cyan

let int_of_color = function
  | Normal -> 0
  | Cyan   -> 6

let in_color c pp out x =
  let n = int_of_color c in
  Printf.fprintf out "\x1b[3%dm" n;
  pp out x;
  Printf.fprintf out "\x1b[0m"

let reset_line = "\x1b[2K\r"
;;
Printf.printf "%sVery Secret!\n%!" reset_line;
Printf.printf "%s\n%!" (String.make 15 '-');
Printf.printf "%a\n%!" (in_color Cyan output_string) "Can you see it?";

and a corresponding repro/dune file:

(test
 (name test2)
 (modules test2))

When executed with dune exec the first line is displayed - and the last line is displayed in cyan:

$ dune exec repro/test2.exe
Very Secret!
---------------
Can you see it?

I also get this behaviour when running the executable _build/default/repro/test2.exe directly (outside dune).

However if I run the above through dune with dune runtest the first line is not visible (the last line is still in cyan).

Actual Behavior

$ dune runtest repro -f
                                  
---------------
Can you see it?

Reproduction

To reproduce

  1. save repro/test2.ml and repro/dune
  2. run dune exec repro/test2.exe
  3. run dune runtest repro -f

Specifications

  • Version of dune (output of dune --version): 3.0.3
  • Version of ocaml (output of ocamlc --version): 4.13.1
  • Operating system (distribution and version): Linux Mint, running 5.4.0-105-generic

Additional information

Digging some more I learned about the script command useful to study the produced output.
Compare the following:

$ script log -qc ' _build/default/repro/test2.exe' && hexdump -C log
Very Secret!
---------------
Can you see it?
00000000  53 63 72 69 70 74 20 73  74 61 72 74 65 64 20 6f  |Script started o|
00000010  6e 20 32 30 32 32 2d 30  33 2d 32 33 20 31 37 3a  |n 2022-03-23 17:|
00000020  34 32 3a 33 31 2b 30 31  3a 30 30 20 5b 54 45 52  |42:31+01:00 [TER|
00000030  4d 3d 22 78 74 65 72 6d  2d 32 35 36 63 6f 6c 6f  |M="xterm-256colo|
00000040  72 22 20 54 54 59 3d 22  2f 64 65 76 2f 70 74 73  |r" TTY="/dev/pts|
00000050  2f 32 22 20 43 4f 4c 55  4d 4e 53 3d 22 31 34 32  |/2" COLUMNS="142|
00000060  22 20 4c 49 4e 45 53 3d  22 34 36 22 5d 0a 1b 5b  |" LINES="46"]..[|
00000070  32 4b 0d 56 65 72 79 20  53 65 63 72 65 74 21 0d  |2K.Very Secret!.|
00000080  0a 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |.---------------|
00000090  0d 0a 1b 5b 33 36 6d 43  61 6e 20 79 6f 75 20 73  |...[36mCan you s|
000000a0  65 65 20 69 74 3f 1b 5b  30 6d 0d 0a 0a 53 63 72  |ee it?.[0m...Scr|
000000b0  69 70 74 20 64 6f 6e 65  20 6f 6e 20 32 30 32 32  |ipt done on 2022|
000000c0  2d 30 33 2d 32 33 20 31  37 3a 34 32 3a 33 31 2b  |-03-23 17:42:31+|
000000d0  30 31 3a 30 30 20 5b 43  4f 4d 4d 41 4e 44 5f 45  |01:00 [COMMAND_E|
000000e0  58 49 54 5f 43 4f 44 45  3d 22 30 22 5d 0a        |XIT_CODE="0"].|
000000ee

I get the same log when running script log -qc 'dune exec repro/test2.exe -f --display=quiet' && hexdump -C log

When run with dune runtest it seems the first line is swallowed:

$ script log -qc 'dune runtest repro -f --display=quiet' && hexdump -C log

---------------
Can you see it?
00000000  53 63 72 69 70 74 20 73  74 61 72 74 65 64 20 6f  |Script started o|
00000010  6e 20 32 30 32 32 2d 30  33 2d 32 33 20 31 37 3a  |n 2022-03-23 17:|
00000020  34 33 3a 34 35 2b 30 31  3a 30 30 20 5b 54 45 52  |43:45+01:00 [TER|
00000030  4d 3d 22 78 74 65 72 6d  2d 32 35 36 63 6f 6c 6f  |M="xterm-256colo|
00000040  72 22 20 54 54 59 3d 22  2f 64 65 76 2f 70 74 73  |r" TTY="/dev/pts|
00000050  2f 32 22 20 43 4f 4c 55  4d 4e 53 3d 22 31 34 32  |/2" COLUMNS="142|
00000060  22 20 4c 49 4e 45 53 3d  22 34 36 22 5d 0a 0d 0a  |" LINES="46"]...|
00000070  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 0d  |---------------.|
00000080  0a 1b 5b 33 36 6d 43 61  6e 20 79 6f 75 20 73 65  |..[36mCan you se|
00000090  65 20 69 74 3f 1b 5b 30  6d 0d 0a 0a 53 63 72 69  |e it?.[0m...Scri|
000000a0  70 74 20 64 6f 6e 65 20  6f 6e 20 32 30 32 32 2d  |pt done on 2022-|
000000b0  30 33 2d 32 33 20 31 37  3a 34 33 3a 34 35 2b 30  |03-23 17:43:45+0|
000000c0  31 3a 30 30 20 5b 43 4f  4d 4d 41 4e 44 5f 45 58  |1:00 [COMMAND_EX|
000000d0  49 54 5f 43 4f 44 45 3d  22 30 22 5d 0a           |IT_CODE="0"].|
000000dd

I believe this is related to #2664 and #3160 and the escape code parsing in https://github.com/ocaml/dune/blob/main/otherlibs/stdune/ansi_color.ml

The test case is a reduced version based on c-cube/qcheck#227

Metadata

Metadata

Assignees

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