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

Output line with ANSI escape code disappears #5528

Closed
jmid opened this issue Mar 23, 2022 · 0 comments · Fixed by #6214 or ocaml/opam-repository#22317
Closed

Output line with ANSI escape code disappears #5528

jmid opened this issue Mar 23, 2022 · 0 comments · Fixed by #6214 or ocaml/opam-repository#22317
Assignees

Comments

@jmid
Copy link
Contributor

jmid commented Mar 23, 2022

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

@emillon emillon self-assigned this Mar 30, 2022
emillon added a commit to emillon/dune that referenced this issue Oct 11, 2022
CSI n K is used to clear the current line. It was not properly parsed by
`Ansi_color.parse`. It is now ignored.

Closes ocaml#5528
Signed-off-by: Etienne Millon <[email protected]>
emillon added a commit to emillon/dune that referenced this issue Oct 11, 2022
CSI n K is used to clear the current line. It was not properly parsed by
`Ansi_color.parse`. It is now ignored.

Closes ocaml#5528
Signed-off-by: Etienne Millon <[email protected]>
emillon added a commit to emillon/dune that referenced this issue Oct 13, 2022
CSI n K is used to clear the current line. It was not properly parsed by
`Ansi_color.parse`. It is now ignored.

Closes ocaml#5528
Signed-off-by: Etienne Millon <[email protected]>
emillon added a commit that referenced this issue Oct 13, 2022
CSI n K is used to clear the current line. It was not properly parsed by
`Ansi_color.parse`. It is now ignored.

Closes #5528
Signed-off-by: Etienne Millon <[email protected]>
emillon added a commit to emillon/opam-repository that referenced this issue Oct 19, 2022
…ne-site, dune-rpc, dune-rpc-lwt, dune-private-libs, dune-glob, dune-configurator, dune-build-info, dune-action-plugin and chrome-trace (3.5.0)

CHANGES:

- macOS: Handle unknown fsevents without crashing (ocaml/dune#6217, @rgrinberg)

- Enable file watching on MacOS SDK < 10.13. (ocaml/dune#6218, @rgrinberg)

- Sandbox running cinaps actions starting from cinaps 1.1 (ocaml/dune#6176, @rgrinberg)

- Add a `runtime_deps` field in the `cinaps` stanza to specify runtime
  dependencies for running the cinaps preprocessing action (ocaml/dune#6175, @rgrinberg)

- Shadow alias module `Foo__` when building a library `Foo` (ocaml/dune#6126, @rgrinberg)

- Extend dune describe to include the root path of the workspace and the
  relative path to the build directory. (ocaml/dune#6136, @reubenrowe)

- Allow dune describe workspace to accept directories as arguments.
  The provided directories restrict the worskpace description to those
  directories. (ocaml/dune#6107, fixes ocaml/dune#3893, @esope)

- Add a terminal persistence mode that attempts to clear the terminal history.
  It is enabled by setting terminal persistence to
  `clear-on-rebuild-and-flush-history` (ocaml/dune#6065, @rgrinberg)

- Disallow generating targets in sub direcories in inferred rules. The check to
  forbid this was accidentally done only for manually specified targets (ocaml/dune#6031,
  @rgrinberg)

- Do not ignore rules marked `(promote (until-clean))` when
  `--ignore-promoted-rules` (or `-p`) is passed. (ocaml/dune#6010, fixes ocaml/dune#4401, @emillon)

- Dune no longer considers .aux files as targets during Coq compilation. This
  means that .aux files are no longer cached. (ocaml/dune#6024, fixes ocaml/dune#6004, @Alizter)

- Cinaps actions are now sandboxed by default (ocaml/dune#6062, @rgrinberg)

- Allow rules producing directory targets to be not sandboxed (ocaml/dune#6056,
  @rgrinberg)

- Introduce a `dirs` field in the `install` stanza to install entire
  directories (ocaml/dune#5097, fixes ocaml/dune#5059, @rgrinberg)

- Menhir rules are now sandboxed by default (ocaml/dune#6076, @rgrinberg)

- Allow rules producing directory targets to create symlinks (ocaml/dune#6077, fixes
  ocaml/dune#5945, @rgrinberg)

- Inline tests are now sandboxed by default (ocaml/dune#6079, @rgrinberg)

- Fix build-info version when used with flambda (ocaml/dune#6089, fixes ocaml/dune#6075, @jberdine)

- Add an `(include <file>)` term to the `include_dirs` field for adding
  directories to the include paths sourced from a file. (ocaml/dune#6058, fixes ocaml/dune#3993,
  @gridbugs)

- Support `(extra_objects ...)` field in `(executable ...)` and `(library
  ...)` stanzas (ocaml/dune#6084, fixes ocaml/dune#4129, @gridbugs)

- Fix compilation of Dune under esy on Windows (ocaml/dune#6109, fixes ocaml/dune#6098, @nojb)

- Improve error message when parsing several licenses in `(license)` (ocaml/dune#6114,
  fixes ocaml/dune#6103, @emillon)

- odoc rules now about `ODOC_SYNTAX` and will rerun accordingly (ocaml/dune#6010, fixes
  ocaml/dune#1117, @emillon)

- dune install: copy files in an atomic way (ocaml/dune#6150, @emillon)

- Add `%{coq:...}` macro for accessing data about the configuration about Coq.
  For instance `%{coq:version}` (ocaml/dune#6049, @Alizter)

- update vendored copy of cmdliner to 1.1.1. This improves the built-in
  documentation for command groups such as `dune ocaml`. (ocaml/dune#6038, @emillon,
  ocaml/dune#6169, @shonfeder)

- The test suite for Coq now requires Coq >= 8.16 due to changes in the
  plugin loading mechanism upstream (which now uses `Findlib`).

- Starting with Coq build language 0.6, theories can be built without importing
  Coq's standard library by including `(stdlib no)`.
  (ocaml/dune#6165 ocaml/dune#6164, fixes ocaml/dune#6163, @ejgallego @Alizter @LasseBlaauwbroek)

- on macOS, sign executables produced by artifact substitution (ocaml/dune#6137, ocaml/dune#6231,
  fixes ocaml/dune#5650, fixes ocaml/dune#6226, @emillon)

- Added an (aliases ...) field to the (rules ...) stanza which allows the
  specification of multiple aliases per rule (ocaml/dune#6194, @Alizter)

- The `(coq.theory ...)` stanza will now ensure that for each declared `(plugin
 ...)`, the `META` file for it is built before calling `coqdep`. This enables
 the use of the new `Findlib`-based loading method in Coq 8.16; however as of
 Coq 8.16.0, Coq itself has some bugs preventing this to work yet. (ocaml/dune#6167 ,
 workarounds ocaml/dune#5767, @ejgallego)

- Allow include statement in install stanza (ocaml/dune#6139, fixes ocaml/dune#256, @gridbugs)

- Handle CSI n K code in ANSI escape codes from commands. (ocaml/dune#6214, fixes ocaml/dune#5528,
  @emillon)

- Add a new experimental feature `mode_specific_stubs` that allows the
  specification of different flags and sources for foreign stubs depending on
  the build mode (ocaml/dune#5649, @voodoos)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment