From ea0baeb4bce71e5e300a68fa8c24a8fdf98c0459 Mon Sep 17 00:00:00 2001 From: Anurag Soni Date: Tue, 14 Aug 2018 22:27:16 -0400 Subject: [PATCH] multi line error highlights Reference: #1131 Signed-off-by: Anurag Soni --- CHANGES.md | 1 + src/loc.ml | 25 +++++++++++++++++++ .../blackbox-tests/test-cases/intf-only/run.t | 2 ++ .../test-cases/missing-loc-run/run.t | 6 +++++ .../test-cases/no-installable-mode/run.t | 5 ++++ .../test-cases/several-packages/run.t | 7 ++++++ .../test-cases/too-many-parens/d/dune | 15 +++++++++++ .../test-cases/too-many-parens/d/dune-project | 1 + .../test-cases/too-many-parens/run.t | 11 ++++++++ 9 files changed, 73 insertions(+) create mode 100644 test/blackbox-tests/test-cases/too-many-parens/d/dune create mode 100644 test/blackbox-tests/test-cases/too-many-parens/d/dune-project diff --git a/CHANGES.md b/CHANGES.md index aa6da4e4..44356bf5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -19,6 +19,7 @@ next - Fix parsing `ocamllex` stanza in jbuild files (#1150, @rgrinberg) +- Highlight multi-line errors (#1131, @anuragsoni) 1.1.1 (08/08/2018) ------------------ diff --git a/src/loc.ml b/src/loc.ml index b529fbe9..a1d5a614 100644 --- a/src/loc.ml +++ b/src/loc.ml @@ -70,10 +70,27 @@ let file_line path n = input_line ic ) +let file_lines path ~start ~stop = + Io.with_file_in ~binary:true path + ~f:(fun ic -> + let rec aux acc lnum = + if lnum > stop then + List.rev acc + else if lnum < start then + (ignore (input_line ic); + aux acc (lnum + 1)) + else + let line = input_line ic in + aux ((string_of_int lnum, line) :: acc) (lnum + 1) + in + aux [] 1 + ) + let print ppf loc = let { start; stop } = loc in let start_c = start.pos_cnum - start.pos_bol in let stop_c = stop.pos_cnum - start.pos_bol in + let num_lines = stop.pos_lnum - start.pos_lnum in let pp_file_excerpt pp () = let whole_file = start_c = 0 && stop_c = 0 in if not whole_file then @@ -85,6 +102,14 @@ let print ppf loc = Format.fprintf pp "%s\n%*s\n" line stop_c (String.make len '^') + else if num_lines <= 10 then + let lines = file_lines path ~start:start.pos_lnum ~stop:stop.pos_lnum in + let last_lnum = Option.map ~f:fst (List.last lines) in + let padding_width = Option.value_exn + (Option.map ~f:String.length last_lnum) in + List.iter ~f:(fun (lnum, l) -> + Format.fprintf pp "%*s: %s\n" padding_width lnum l) + lines in Format.fprintf ppf "@{File \"%s\", line %d, characters %d-%d:@}@\n%a" diff --git a/test/blackbox-tests/test-cases/intf-only/run.t b/test/blackbox-tests/test-cases/intf-only/run.t index 9222fd2e..cd7dc429 100644 --- a/test/blackbox-tests/test-cases/intf-only/run.t +++ b/test/blackbox-tests/test-cases/intf-only/run.t @@ -24,6 +24,8 @@ Errors: $ dune build --display short --root a foo.cma Entering directory 'a' File "dune", line 1, characters 0-21: + 1: (library + 2: (name foo)) Warning: Some modules don't have an implementation. You need to add the following field to this stanza: diff --git a/test/blackbox-tests/test-cases/missing-loc-run/run.t b/test/blackbox-tests/test-cases/missing-loc-run/run.t index 565a8f90..3ae2de65 100644 --- a/test/blackbox-tests/test-cases/missing-loc-run/run.t +++ b/test/blackbox-tests/test-cases/missing-loc-run/run.t @@ -3,6 +3,9 @@ Exact path provided by the user: $ dune runtest --root precise-path Entering directory 'precise-path' File "dune", line 1, characters 0-49: + 1: (alias + 2: (name runtest) + 3: (action (run ./foo.exe))) Error: No rule found for foo.exe [1] @@ -21,5 +24,8 @@ Path in deps field of alias stanza $ dune runtest --root alias-deps-field Entering directory 'alias-deps-field' File "dune", line 1, characters 0-38: + 1: (alias + 2: (name runtest) + 3: (deps foobar)) Error: No rule found for foobar [1] diff --git a/test/blackbox-tests/test-cases/no-installable-mode/run.t b/test/blackbox-tests/test-cases/no-installable-mode/run.t index 28834080..3a29387a 100644 --- a/test/blackbox-tests/test-cases/no-installable-mode/run.t +++ b/test/blackbox-tests/test-cases/no-installable-mode/run.t @@ -3,6 +3,11 @@ message is displayed: $ dune build --root=public --display=short File "jbuild", line 4, characters 2-74: + 4: ( + 5: (name mylib) + 6: (public_name mylib) + 7: (modes (shared_object)) + 8: ) Error: No installable mode found for this executable. One of the following modes is required: - exe diff --git a/test/blackbox-tests/test-cases/several-packages/run.t b/test/blackbox-tests/test-cases/several-packages/run.t index 304ed0ae..6e4bc2d7 100644 --- a/test/blackbox-tests/test-cases/several-packages/run.t +++ b/test/blackbox-tests/test-cases/several-packages/run.t @@ -5,6 +5,9 @@ displayed. This can happen for: $ dune build --root executable File "dune", line 1, characters 0-43: + 1: (executable + 2: (public_name an_executable) + 3: ) Error: I can't determine automatically which package this stanza is for. I have the choice between these ones: - pkg1 (because of pkg1.opam) @@ -29,6 +32,10 @@ displayed. This can happen for: $ dune build --root install File "dune", line 1, characters 0-44: + 1: (install + 2: (section etc) + 3: (files file.conf) + 4: ) Error: I can't determine automatically which package this stanza is for. I have the choice between these ones: - pkg1 (because of pkg1.opam) diff --git a/test/blackbox-tests/test-cases/too-many-parens/d/dune b/test/blackbox-tests/test-cases/too-many-parens/d/dune new file mode 100644 index 00000000..b97b1eab --- /dev/null +++ b/test/blackbox-tests/test-cases/too-many-parens/d/dune @@ -0,0 +1,15 @@ +(executable + (name hello) + (libraries (a + b + c + d + e + f + g + h + i + j + k + l) + )) diff --git a/test/blackbox-tests/test-cases/too-many-parens/d/dune-project b/test/blackbox-tests/test-cases/too-many-parens/d/dune-project new file mode 100644 index 00000000..de4fc209 --- /dev/null +++ b/test/blackbox-tests/test-cases/too-many-parens/d/dune-project @@ -0,0 +1 @@ +(lang dune 1.0) diff --git a/test/blackbox-tests/test-cases/too-many-parens/run.t b/test/blackbox-tests/test-cases/too-many-parens/run.t index 6fc31fad..93b17ef5 100644 --- a/test/blackbox-tests/test-cases/too-many-parens/run.t +++ b/test/blackbox-tests/test-cases/too-many-parens/run.t @@ -3,6 +3,11 @@ are readable. $ dune build --root a File "dune", line 1, characters 12-72: + 1: (executable ( + 2: (name hello) + 3: (public_name hello) + 4: (libraries (lib)) + 5: )) These parentheses are no longer necessary with dune, please remove them. [1] @@ -19,3 +24,9 @@ are readable. ^^^^^^^ These parentheses are no longer necessary with dune, please remove them. [1] + +Checking that extra long stanzas (over 10 lines) are not printed + $ dune build --root d + File "dune", line 3, characters 13-192: + These parentheses are no longer necessary with dune, please remove them. + [1]