Skip to content

Commit bc1b12e

Browse files
authored
Merge pull request #3553 from mtzguido/github_errs
Errors: Introducing '--message_format github', for github actions
2 parents 19c2d3d + bae3a55 commit bc1b12e

5 files changed

Lines changed: 84 additions & 20 deletions

File tree

mk/diff.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
if [ $# -ne 2 ]; then
4+
echo "usage: $0 <actual_output> <expected_output>" >&2
5+
exit 1
6+
fi
7+
8+
ACTUAL="$1"
9+
EXPECTED="$2"
10+
11+
DIFF="diff -u --strip-trailing-cr"
12+
13+
if $DIFF "$ACTUAL" "$EXPECTED" ; then
14+
# OK
15+
exit 0
16+
else
17+
# We're gonna fail. If we're running in CI, emit a Github
18+
# error message.
19+
if [ -v GITHUB_ENV ]; then
20+
DIFFTEXT=$($DIFF "$ACTUAL" "$EXPECTED" | sed 's/$/%0A/' | tr -d '\n')
21+
ACTUAL=$(realpath "$ACTUAL")
22+
ACTUAL="${ACTUAL#$FSTAR_ROOT}"
23+
EXPECTED=$(realpath "$EXPECTED")
24+
EXPECTED="${EXPECTED#$FSTAR_ROOT}"
25+
echo "::error::Diff failed for files $ACTUAL and $EXPECTED:%0A%0A$DIFFTEXT"
26+
else
27+
echo "error: Diff failed for files $ACTUAL and $EXPECTED" >&2
28+
fi
29+
exit 1
30+
fi

mk/test.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ $(OUTPUT_DIR)/%.out: $(OUTPUT_DIR)/%.exe
119119
### Checking expected output for any kind of file (error output, ml, etc)
120120
$(OUTPUT_DIR)/%.diff: $(OUTPUT_DIR)/% %.expected
121121
$(call msg, "DIFF", $<)
122-
diff -u --strip-trailing-cr $^
122+
$(FSTAR_ROOT)/mk/diff.sh $^
123123
touch $@
124124

125125
$(OUTPUT_DIR)/%.accept: $(OUTPUT_DIR)/%

src/basic/FStarC.Errors.fst

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ open FStarC.Compiler.Effect
2222
open FStarC.Compiler.List
2323
open FStarC.Compiler.Util
2424
open FStarC.Compiler.Range
25-
open FStarC.Class.Monad
2625
open FStarC.Options
2726
module List = FStarC.Compiler.List
2827
module BU = FStarC.Compiler.Util
2928
module PP = FStarC.Pprint
3029

30+
open FStarC.Class.Monad
31+
open FStarC.Class.Show
3132
open FStarC.Errors.Codes
3233
open FStarC.Errors.Msg
3334
open FStarC.Json
@@ -175,27 +176,26 @@ let optional_def (f : 'a -> PP.document) (def : PP.document) (o : option 'a) : P
175176

176177
let format_issue' (print_hdr:bool) (issue:issue) : string =
177178
let open FStarC.Pprint in
178-
let level_header = doc_of_string (string_of_issue_level issue.issue_level) in
179-
let num_opt =
180-
if issue.issue_level = EError || issue.issue_level = EWarning
181-
then blank 1 ^^ optional_def (fun n -> doc_of_string (string_of_int n)) (doc_of_string "<unknown>") issue.issue_number
182-
else empty
183-
in
184179
let r = issue.issue_range in
185-
let atrng : document =
186-
match r with
187-
| Some r when r <> Range.dummyRange ->
188-
blank 1 ^^ doc_of_string "at" ^^ blank 1 ^^ doc_of_string (Range.string_of_use_range r)
189-
| _ ->
190-
empty
191-
in
192180
let hdr : document =
193-
if print_hdr
194-
then
181+
if print_hdr then (
182+
let level_header = doc_of_string (string_of_issue_level issue.issue_level) in
183+
let num_opt =
184+
if issue.issue_level = EError || issue.issue_level = EWarning
185+
then blank 1 ^^ optional_def (fun n -> doc_of_string (string_of_int n)) (doc_of_string "<unknown>") issue.issue_number
186+
else empty
187+
in
188+
let atrng : document =
189+
match r with
190+
| Some r when r <> Range.dummyRange ->
191+
blank 1 ^^ doc_of_string "at" ^^ blank 1 ^^ doc_of_string (Range.string_of_use_range r)
192+
| _ ->
193+
empty
194+
in
195195
doc_of_string "*" ^^ blank 1 ^^ level_header ^^ num_opt ^^
196196
atrng ^^
197197
doc_of_string ":" ^^ hardline
198-
else empty
198+
) else empty
199199
in
200200
let seealso : document =
201201
match r with
@@ -230,6 +230,38 @@ let format_issue issue : string = format_issue' true issue
230230
let print_issue_json issue =
231231
json_of_issue issue |> string_of_json |> BU.print1_error "%s\n"
232232

233+
(*
234+
Printing for nicer display in github actions runs. See
235+
https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions
236+
for more info. The idea here is basically render it as text and then
237+
add a github header. Also we replace newlines by %0A which become
238+
newlines in the rendered github annotation, though that does not seem
239+
to be very well documented (https://github.com/orgs/community/discussions/26736)
240+
*)
241+
let print_issue_github issue =
242+
match issue.issue_level with
243+
| ENotImplemented
244+
| EInfo -> ()
245+
| EError
246+
| EWarning ->
247+
let level = if EError? issue.issue_level then "error" else "warning" in
248+
let rng = dflt dummyRange issue.issue_range in
249+
let msg = format_issue' true issue in
250+
let msg = msg |> BU.splitlines |> String.concat "%0A" in
251+
let num =
252+
match issue.issue_number with
253+
| None -> ""
254+
| Some n -> BU.format1 "(%s) " (show n)
255+
in
256+
BU.print_warning <|
257+
BU.format6 "::%s file=%s,line=%s,endLine=%s::%s%s\n"
258+
level
259+
(Range.file_of_range rng)
260+
(show (rng |> Range.start_of_range |> Range.line_of_pos))
261+
(show (rng |> Range.end_of_range |> Range.line_of_pos))
262+
num
263+
msg
264+
233265
let print_issue_rendered issue =
234266
let printer =
235267
match issue.issue_level with
@@ -243,6 +275,7 @@ let print_issue issue =
243275
match FStarC.Options.message_format () with
244276
| Human -> print_issue_rendered issue
245277
| Json -> print_issue_json issue
278+
| Github -> print_issue_github issue
246279

247280
let compare_issues i1 i2 =
248281
match i1.issue_range, i2.issue_range with

src/basic/FStarC.Options.fst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ let rec specs_with_types warn_unsafe : list (char & string & opt_type & Pprint.d
991991
992992
( noshort,
993993
"message_format",
994-
EnumStr ["human"; "json"],
994+
EnumStr ["human"; "json"; "github"],
995995
text "Format of the messages emitted by F* (default `human`)");
996996
997997
( noshort,
@@ -1997,6 +1997,7 @@ let message_format () =
19971997
match get_message_format () with
19981998
| "human" -> Human
19991999
| "json" -> Json
2000+
| "github" -> Github
20002001
| illegal -> failwith ("print_issue: option `message_format` was expected to be `human` or `json`, not `" ^ illegal ^ "`. This should be impossible: `message_format` was supposed to be validated.")
20012002
let force () = get_force ()
20022003
let full_context_dependency () = true

src/basic/FStarC.Options.fsti

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type codegen_t =
3333

3434
type split_queries_t = | No | OnFailure | Always
3535

36-
type message_format_t = | Json | Human
36+
type message_format_t = | Json | Human | Github
3737

3838
type option_val =
3939
| Bool of bool

0 commit comments

Comments
 (0)