Skip to content

Commit

Permalink
fix(ci): never re-run the anonymous function
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro committed Apr 27, 2023
1 parent 53dbb69 commit 4c51eaf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
3 changes: 3 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Unreleased
- melange: allow installing melange in more OCaml versions and compiler
switches. Melange now migrates binary AST to the version it understands
([#548](https://github.com/melange-re/melange/pull/548))
- melange: don't run anonymous args function from
`[@@@bs.config {flags = [| ... |]}]` attributes
([#554](https://github.com/melange-re/melange/pull/554))

0.3.2 2022-11-19
---------------
Expand Down
64 changes: 37 additions & 27 deletions jscomp/main/melc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,44 @@ let process_file sourcefile

let ppf = Format.err_formatter

let anonymous ~(rev_args : string list) =
if !Js_config.as_ppx then
match rev_args with
| [output; input] ->
`Ok (Melange_ppx_lib.Ppx_apply.apply_lazy
~source:input
~target:output
Melange_ppx_lib.Ppx_entry.rewrite_implementation
Melange_ppx_lib.Ppx_entry.rewrite_signature)
| _ -> `Error(false, "`--as-ppx` requires 2 arguments: `melc --as-ppx input output`")
else
begin
if !Js_config.syntax_only then begin
Ext_list.rev_iter rev_args (fun filename ->
begin
(* Clflags.reset_dump_state (); *)
(* Warnings.reset (); *)
process_file filename ppf
end );
`Ok ()
end else
let anonymous =
let executed = ref false in
fun ~(rev_args : string list) ->
match !executed with
| true ->
(* Don't re-run anonymous arguments again if this function has already
* been executed. If this code is executing the 2nd time, it's coming
* from a [@bs.config { flags = [| ... |] }].. *)
`Ok ()
| false ->
executed := true;
if !Js_config.as_ppx then
match rev_args with
| [output; input] ->
`Ok (Melange_ppx_lib.Ppx_apply.apply_lazy
~source:input
~target:output
Melange_ppx_lib.Ppx_entry.rewrite_implementation
Melange_ppx_lib.Ppx_entry.rewrite_signature)
| _ -> `Error(false, "`--as-ppx` requires 2 arguments: `melc --as-ppx input output`")
else
begin
if !Js_config.syntax_only then begin
Ext_list.rev_iter rev_args (fun filename ->
begin
(* Clflags.reset_dump_state (); *)
(* Warnings.reset (); *)
process_file filename ppf
end );
`Ok ()
end else

match rev_args with
| [filename] -> `Ok (process_file filename ppf)
| [] -> `Ok ()
| _ ->
`Error (false, "can not handle multiple files")
end
match rev_args with
| [filename] -> `Ok (process_file filename ppf)
| [] -> `Ok ()
| _ ->
`Error (false, "can not handle multiple files")
end

(** used by -impl -intf *)
let impl filename =
Expand Down
15 changes: 8 additions & 7 deletions test/as-ppx-config-attribute.t
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@

$ cat > foo.ml <<EOF
> [@@@bs.config {flags = [| "-unsafe" |] }]
> [@@@bs.config {flags = [| "-unsafe"; "-nopervasives" |] }]
> let x = 1
> EOF
$ melc foo.ml -as-pp foo.ml > foo.pp.ml
$ melc --as-ppx foo.pp.ml bar.pp.ml
melc: `--as-ppx` requires 2 arguments: `melc --as-ppx input output`
1 | [@@@bs.config {flags = [| "-unsafe" |] }]
^^^^^^^^^^^^^^^
Error: Invalid configuration
[2]
$ melc ./bar.pp.ml
// Generated by Melange
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
'use strict';
var x = 1;
exports.x = x;
/* No side effect */

0 comments on commit 4c51eaf

Please sign in to comment.