Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/format-reporting.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- `rescript format` formats or checks every file even when one has a syntax error, then fails; it reports files once each, sorted and relative to the current directory, rejects missing files, directories and non-ReScript files with a short message, and reports `--stdin` errors at `<stdin>`.
79 changes: 59 additions & 20 deletions rewatch-ocaml/format.ml
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,28 @@ let files_in_scope ~verbosity =
|> List.concat_map package_sources
|> List.sort_uniq String.compare

(* The compiler's diagnostics start and end with blank lines. *)
let formatting_error target stderr =
Printf.sprintf "Error formatting %s: %s" target stderr
Printf.sprintf "Error formatting %s:\n%s" target (String.trim stderr)

let format_request path =
Compiler_process.{args = ["-format"; path]; cwd = Sys.getcwd ()}

let formatted ?poll ~target path =
let result = Compiler_process.run ?poll (format_request path) in
if not (Process.succeeded result) then
raise (Error (formatting_error target result.stderr));
result.stdout

let format_check_summary = function
| 1 -> "The file listed above needs formatting"
| count -> Printf.sprintf "The %d files listed above need formatting" count

(* Every file is formatted, also after one fails, and the results are
reported in path order once all are done, relative to the current
directory. *)
let format_files ?poll ~check files =
let incorrect = ref 0 in
let files = List.sort_uniq String.compare files in
let display path =
if Filename.is_relative path then path
else Project_context.relative_or_absolute ~root:(Sys.getcwd ()) path
in
let unformatted = ref [] in
let failures = ref [] in
let works =
files
|> List.mapi (fun index path ->
Expand All @@ -139,22 +143,32 @@ let format_files ?poll ~check files =
let next path = function
| None -> Some (Compiler_process.task (format_request path))
| Some result ->
if not (Process.succeeded result) then
raise (Error (formatting_error path result.stderr));
let original = read_file path in
if original <> result.stdout then
if check then (
incr incorrect;
prerr_endline ("[format check] " ^ path))
else write_file path result.stdout;
(if not (Process.succeeded result) then
failures := (path, result.stderr) :: !failures
else
try
if read_file path <> result.stdout then
if check then unformatted := path :: !unformatted
else write_file path result.stdout
with Error message -> failures := (path, message) :: !failures);
None
in
Process.run_dependency_graph
~max_jobs:(Compiler_execution_mode.configured_count ())
?poll works ~next;
if !incorrect > 0 then (
prerr_endline (format_check_summary !incorrect);
raise (Error "Formatting check failed"))
List.iter
(fun path -> prerr_endline ("[format check] " ^ display path))
(List.sort String.compare !unformatted);
if !unformatted <> [] then
prerr_endline (format_check_summary (List.length !unformatted));
List.iter
(fun (path, message) ->
prerr_endline (formatting_error (display path) message))
(List.sort compare !failures);
match (!failures, !unformatted) with
| [], [] -> ()
| _ :: _, _ -> raise (Error "Formatting failed")
| [], _ :: _ -> raise (Error "Formatting check failed")

type stdin_read_result = Stdin_contents of string | Stdin_error of exn

Expand Down Expand Up @@ -203,9 +217,34 @@ let format_stdin ?poll extension =
(fun () ->
let contents = read_stdin_interruptibly ?poll () in
File_util.write_file path contents;
print_string (formatted ?poll ~target:"stdin" path))
let result = Compiler_process.run ?poll (format_request path) in
if not (Process.succeeded result) then
(* Diagnostics name the temporary file the input went through. *)
raise
(Error
(formatting_error "<stdin>"
(Str.global_replace (Str.regexp_string path) "<stdin>"
result.stderr)));
print_string result.stdout)

(* Explicit arguments are checked before any is formatted, so a typo does not
reach the compiler as an I/O error. *)
let check_argument path =
if not (Sys.file_exists path) then
raise (Error (Printf.sprintf "%s: No such file" path))
else if Sys.is_directory path then
raise
(Error
(Printf.sprintf
"%s is a directory; give .res or .resi files, or no arguments to \
format the project"
path))
else if
not (Filename.check_suffix path ".res" || Filename.check_suffix path ".resi")
then raise (Error (Printf.sprintf "%s is not a .res or .resi file" path))

let run_files ?poll ~verbosity ~check paths =
List.iter check_argument paths;
let files = if paths = [] then files_in_scope ~verbosity else paths in
List.iter
(fun file -> Output.trace ~channel:stderr ~verbosity ("Formatting " ^ file))
Expand Down
37 changes: 34 additions & 3 deletions rewatch-ocaml/tests/check_command_validation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,37 @@ run_case format-directory reject format "$project/src"
check_output_snapshot format-directory
run_case format-unsupported-extension reject format "$project/src/A.txt"
check_output_snapshot format-unsupported-extension
# A syntax error does not stop the run: every other file is checked and
# listed in path order, then the run fails.
format_broken="$work/format-broken"
mkdir -p "$format_broken/src"
printf '{"name":"format-broken","sources":["src"]}\n' \
>"$format_broken/rescript.json"
for name in D B C A; do
printf 'let value = 1\n' >"$format_broken/src/$name.res"
done
printf 'let value =\n' >"$format_broken/src/Broken.res"
run_case --cwd "$format_broken" format-continues-after-syntax-error reject \
format --check
printf '%s\n' '[format check] src/A.res' '[format check] src/B.res' \
'[format check] src/C.res' '[format check] src/D.res' \
>"$work/format-broken.expected"
# Windows prints the relative paths with backslashes.
tr '\\' '/' <"$work/rescript.err" >"$work/format-broken.err"
grep -F '[format check]' "$work/format-broken.err" >"$work/format-broken.listed"
if ! cmp -s "$work/format-broken.expected" "$work/format-broken.listed" || \
! grep -F 'Error formatting src/Broken.res:' "$work/format-broken.err" \
>/dev/null || \
! grep -F 'The 4 files listed above need formatting' \
"$work/format-broken.err" >/dev/null; then
echo "format-continues-after-syntax-error: not every file was reported in order" >&2
dump_output
exit 1
fi
run_case --cwd "$format_broken" format-duplicate-arguments reject \
format --check src/A.res src/A.res
require_error_contains format-duplicate-arguments \
'The file listed above needs formatting'
run_case --cwd "$work/empty" format-no-config reject format
require_error_contains format-no-config \
"Could not read rescript.json at $work/empty:"
Expand Down Expand Up @@ -677,7 +708,7 @@ if [ "$rescript_status" -eq 0 ] || [ -s "$work/rescript.out" ]; then
echo "format-invalid-stdin: expected the formatter to reject" >&2
exit 1
fi
require_error_contains format-invalid-stdin 'Error formatting stdin:'
require_error_contains format-invalid-stdin 'Error formatting <stdin>:'
require_error_contains format-invalid-stdin \
'This let-binding misses an expression'
checked=$((checked + 1))
Expand Down Expand Up @@ -933,8 +964,8 @@ run_case --cwd "$work/format-source-selection" \
normalize_project_path "$work/rescript.err" "$work/rescript.err.norm" \
"$work/format-source-selection"
missing_folder='Could not read folder: "missing". Specified in dependency: installed'
base_file='[format check] <ROOT>/packages/local/base/Base.res'
native_file='[format check] <ROOT>/packages/local/native/Native.res'
base_file='[format check] packages/local/base/Base.res'
native_file='[format check] packages/local/native/Native.res'
other_file='<ROOT>/packages/local/other/Other.res'
if ! grep -F "$missing_folder" "$work/rescript.err.norm" >/dev/null || \
! grep -F "$base_file" "$work/rescript.err.norm" >/dev/null || \
Expand Down
6 changes: 3 additions & 3 deletions rewatch-ocaml/tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ if printf 'let =\n' | "$port" format --stdin .res \
echo "format stdin unexpectedly accepted invalid syntax" >&2
exit 1
fi
grep -F "Error formatting stdin:" "$work/format-invalid.err" >/dev/null
grep -F "Error formatting <stdin>:" "$work/format-invalid.err" >/dev/null

format_stdin_tmp="$work/format-stdin-tmp"
format_stdin_fifo="$work/format-stdin.fifo"
Expand Down Expand Up @@ -778,9 +778,9 @@ if "$port" format --check "$work/unformatted.res" \
echo "format check unexpectedly accepted an unformatted file" >&2
exit 1
fi
unformatted_native=$(native_path "$work/unformatted.res")
# Paths are shown relative to the current directory when they are below it.
tr '\\' '/' <"$work/format-check.err" | \
grep -F "[format check] $unformatted_native" >/dev/null
grep -E '^\[format check\] .*unformatted\.res$' >/dev/null
grep -F "The file listed above needs formatting" \
"$work/format-check.err" >/dev/null
grep -F "Formatting check failed" "$work/format-check.err" >/dev/null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
status: 1
--- stdout
--- stderr
Error formatting <WORK>/project/src: don't know what to do with <WORK>/project/src

<WORK>/project/src is a directory; give .res or .resi files, or no arguments to format the project
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
status: 1
--- stdout
--- stderr
Error formatting <WORK>/project/src/Missing.res:
We've found a bug for you!
_none_

I/O error: <WORK>/project/src/Missing.res: No such file or directory


<WORK>/project/src/Missing.res: No such file
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
status: 1
--- stdout
--- stderr
Error formatting <WORK>/project/src/A.txt: don't know what to do with <WORK>/project/src/A.txt

<WORK>/project/src/A.txt is not a .res or .resi file
14 changes: 8 additions & 6 deletions tests/rewatch_ounit_tests/format_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ let with_temp_dir = Test_support.with_temp_dir "rewatch-ocaml-format-"
let tests =
"format_tests" >:: fun _context ->
check
(Format.formatting_error "stdin" "invalid source"
= "Error formatting stdin: invalid source")
(Format.formatting_error "<stdin>" "\ninvalid source\n"
= "Error formatting <stdin>:\ninvalid source")
"stdin formatting failures do not expose a temporary filename";
check
(Format.formatting_error "src/A.res" "invalid source"
= "Error formatting src/A.res: invalid source")
= "Error formatting src/A.res:\ninvalid source")
"file formatting failures retain the source path";
check
(Format.format_check_summary 1 = "The file listed above needs formatting")
Expand Down Expand Up @@ -95,11 +95,13 @@ let tests =
check
(try
write_file unformatted "let =\n";
Format.format_files ~check:false [unformatted];
write_file formatted "let second=2\n";
Format.format_files ~check:false [unformatted; formatted];
false
with Format.Error message ->
contains message ("Error formatting " ^ unformatted))
"syntax errors are reported with the source path");
message = "Formatting failed"
&& File_util.read_file formatted = "let second = 2\n")
"a syntax error fails the run after the other files are formatted");
with_temp_dir (fun root ->
let root_source = Test_support.path root "src/App.res" in
let orphan_interface = Test_support.path root "src/Orphan.resi" in
Expand Down
Loading