From eaae5981087a814e08ea1f93b2c00c7af6e3c69f Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Fri, 6 Nov 2026 20:54:16 +0000 Subject: [PATCH 1/2] Report every file in rescript format, in order, with short argument errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Why: One file with a syntax error stopped rescript format, leaving other files unformatted; --check listed files in completion order with absolute paths, duplicate arguments counted twice, and invalid arguments surfaced raw compiler errors (#9243). Behavior modified (rescript format): Before (representation): a syntax error in one file ↦ the run stops; some other files stay unformatted format --check ↦ files in completion order, absolute in project mode format --check src/F.res src/F.res ↦ The 2 files listed above need formatting format src/Missing.res ↦ We've found a bug for you! … I/O error: …: No such file or directory format src ↦ Error formatting src: don't know what to do with src format --stdin with a syntax error ↦ /tmp/rescript-format-7546c4.res:1:8-2:0 After (representation): a syntax error in one file ↦ every other file is formatted; the errors are reported, then the run fails format --check ↦ files sorted, relative to the current directory format --check src/F.res src/F.res ↦ The file listed above needs formatting format src/Missing.res ↦ src/Missing.res: No such file format src ↦ src is a directory; give .res or .resi files, or no arguments to format the project format --stdin with a syntax error ↦ :1:8-2:0 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gGBR9g3DD93vrWbYzU Signed-off-by: Christoph Knittel --- changes/format-reporting.fix.md | 1 + rewatch-ocaml/format.ml | 79 ++++++++++++++----- .../tests/check_command_validation.sh | 35 +++++++- rewatch-ocaml/tests/run.sh | 6 +- .../command_validation/format-directory.txt | 3 +- .../format-missing-file.txt | 8 +- .../format-unsupported-extension.txt | 3 +- tests/rewatch_ounit_tests/format_tests.ml | 14 ++-- 8 files changed, 106 insertions(+), 43 deletions(-) create mode 100644 changes/format-reporting.fix.md diff --git a/changes/format-reporting.fix.md b/changes/format-reporting.fix.md new file mode 100644 index 00000000000..ce2765b7207 --- /dev/null +++ b/changes/format-reporting.fix.md @@ -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 ``. diff --git a/rewatch-ocaml/format.ml b/rewatch-ocaml/format.ml index a79bfb5740c..7316e259544 100644 --- a/rewatch-ocaml/format.ml +++ b/rewatch-ocaml/format.ml @@ -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 -> @@ -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 @@ -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 "" + (Str.global_replace (Str.regexp_string path) "" + 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)) diff --git a/rewatch-ocaml/tests/check_command_validation.sh b/rewatch-ocaml/tests/check_command_validation.sh index 518b1cd695d..4817f447f2c 100755 --- a/rewatch-ocaml/tests/check_command_validation.sh +++ b/rewatch-ocaml/tests/check_command_validation.sh @@ -648,6 +648,35 @@ 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" +grep -F '[format check]' "$work/rescript.err" >"$work/format-broken.listed" +if ! cmp -s "$work/format-broken.expected" "$work/format-broken.listed" || \ + ! grep -F 'Error formatting src/Broken.res:' "$work/rescript.err" \ + >/dev/null || \ + ! grep -F 'The 4 files listed above need formatting' "$work/rescript.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:" @@ -677,7 +706,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 :' require_error_contains format-invalid-stdin \ 'This let-binding misses an expression' checked=$((checked + 1)) @@ -933,8 +962,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] /packages/local/base/Base.res' -native_file='[format check] /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='/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 || \ diff --git a/rewatch-ocaml/tests/run.sh b/rewatch-ocaml/tests/run.sh index 50031297328..4aceb4d6211 100644 --- a/rewatch-ocaml/tests/run.sh +++ b/rewatch-ocaml/tests/run.sh @@ -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 :" "$work/format-invalid.err" >/dev/null format_stdin_tmp="$work/format-stdin-tmp" format_stdin_fifo="$work/format-stdin.fifo" @@ -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 diff --git a/rewatch-ocaml/tests/snapshots/command_validation/format-directory.txt b/rewatch-ocaml/tests/snapshots/command_validation/format-directory.txt index 678474fbe56..5f0a033ada0 100644 --- a/rewatch-ocaml/tests/snapshots/command_validation/format-directory.txt +++ b/rewatch-ocaml/tests/snapshots/command_validation/format-directory.txt @@ -1,5 +1,4 @@ status: 1 --- stdout --- stderr -Error formatting /project/src: don't know what to do with /project/src - +/project/src is a directory; give .res or .resi files, or no arguments to format the project diff --git a/rewatch-ocaml/tests/snapshots/command_validation/format-missing-file.txt b/rewatch-ocaml/tests/snapshots/command_validation/format-missing-file.txt index c6b74b61303..ae3135bc8b6 100644 --- a/rewatch-ocaml/tests/snapshots/command_validation/format-missing-file.txt +++ b/rewatch-ocaml/tests/snapshots/command_validation/format-missing-file.txt @@ -1,10 +1,4 @@ status: 1 --- stdout --- stderr -Error formatting /project/src/Missing.res: - We've found a bug for you! - _none_ - - I/O error: /project/src/Missing.res: No such file or directory - - +/project/src/Missing.res: No such file diff --git a/rewatch-ocaml/tests/snapshots/command_validation/format-unsupported-extension.txt b/rewatch-ocaml/tests/snapshots/command_validation/format-unsupported-extension.txt index 9fe7596e21b..b687dcf4d3c 100644 --- a/rewatch-ocaml/tests/snapshots/command_validation/format-unsupported-extension.txt +++ b/rewatch-ocaml/tests/snapshots/command_validation/format-unsupported-extension.txt @@ -1,5 +1,4 @@ status: 1 --- stdout --- stderr -Error formatting /project/src/A.txt: don't know what to do with /project/src/A.txt - +/project/src/A.txt is not a .res or .resi file diff --git a/tests/rewatch_ounit_tests/format_tests.ml b/tests/rewatch_ounit_tests/format_tests.ml index ef49fae32e1..93b71877064 100644 --- a/tests/rewatch_ounit_tests/format_tests.ml +++ b/tests/rewatch_ounit_tests/format_tests.ml @@ -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 "" "\ninvalid source\n" + = "Error formatting :\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") @@ -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 From 82407f838c59b41c0bb1191994bdb0cabd8d0f2d Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Fri, 6 Nov 2026 21:29:41 +0000 Subject: [PATCH 2/2] Compare the format listing with forward slashes on Windows Windows prints the relative paths with backslashes, which the new format-continues-after-syntax-error check compared literally. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gGBR9g3DD93vrWbYzU Signed-off-by: Christoph Knittel --- rewatch-ocaml/tests/check_command_validation.sh | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/rewatch-ocaml/tests/check_command_validation.sh b/rewatch-ocaml/tests/check_command_validation.sh index 4817f447f2c..1ec819d756e 100755 --- a/rewatch-ocaml/tests/check_command_validation.sh +++ b/rewatch-ocaml/tests/check_command_validation.sh @@ -663,12 +663,14 @@ run_case --cwd "$format_broken" format-continues-after-syntax-error reject \ 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" -grep -F '[format check]' "$work/rescript.err" >"$work/format-broken.listed" +# 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/rescript.err" \ + ! grep -F 'Error formatting src/Broken.res:' "$work/format-broken.err" \ >/dev/null || \ - ! grep -F 'The 4 files listed above need formatting' "$work/rescript.err" \ - >/dev/null; then + ! 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