Skip to content

Commit

Permalink
fix(mdx): record runtime dependency to prelude
Browse files Browse the repository at this point in the history
When mdx is used in program-generation mode (version >= 0.2), the paths
used as preludes are recorded in the executable, but the contents of
files are only read when the program is executed. This adds the missing
dependencies.

Fixes ocaml#7077

Signed-off-by: Etienne Millon <[email protected]>
  • Loading branch information
emillon committed Mar 30, 2023
1 parent be43ee2 commit 0284642
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
3.7.1 (unreleased)
------------------

- Fix preludes not being recorded as dependencies in the `(mdx)` stanza (#7109,
fixes #7077, @emillon).

3.7.0.post1 (2023-02-21)
------------------------

Expand Down
19 changes: 15 additions & 4 deletions src/dune_rules/mdx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,20 @@ module Prelude = struct
in
enter decode_env <|> decode_default

(** Generated program will read some files when it runs. *)
let runtime_deps ~dir t : _ Command.Args.t =
match t with
| Default file | Env { env = _; file } ->
Hidden_deps
(Dep.Set.of_files [ Path.build (Path.Build.append_local dir file) ])

let to_args ~dir t : _ Command.Args.t list =
let bpath p = Path.build (Path.Build.append_local dir p) in
match t with
| Default file -> [ A "--prelude"; Dep (bpath file) ]
| Default file ->
[ A "--prelude"; Dep (Path.build (Path.Build.append_local dir file)) ]
| Env { env; file } ->
let arg = sprintf "%s:%s" env (Path.Local.to_string file) in
[ A "--prelude"; A arg; Hidden_deps (Dep.Set.of_files [ bpath file ]) ]
[ A "--prelude"; A arg; runtime_deps ~dir t ]
end

type t =
Expand Down Expand Up @@ -317,7 +324,11 @@ let gen_rules_for_single_file stanza ~sctx ~dir ~expander ~mdx_prog
generated executable *)
let open Command.Args in
match mdx_prog_gen with
| Some prog -> (Ok (Path.build prog), [ Dep (Path.build files.src) ])
| Some prog ->
( Ok (Path.build prog)
, [ Dep (Path.build files.src)
; S (List.map ~f:(Prelude.runtime_deps ~dir) stanza.preludes)
] )
| None ->
let prelude_args =
List.concat_map stanza.preludes ~f:(Prelude.to_args ~dir)
Expand Down
39 changes: 39 additions & 0 deletions test/blackbox-tests/test-cases/mdx-stanza/github7077.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
When mdx is used to generate a program, the program will read the preludes at
run time. This test makes sure that this is recorded as a dependency.

$ cat > dune << EOF
> (mdx (preludes prelude.ml))
> EOF

$ cat > dune-project << EOF
> (lang dune 3.6)
> (using mdx 0.3)
> EOF

$ cat > prelude.ml << EOF
> let foo () = 1
> EOF

$ cat > README.md << 'EOF'
> ```ocaml
> # foo ();;
> - : int = 1
> ```
> EOF

$ dune runtest

$ echo 'let foo () = 2' > prelude.ml

$ dune runtest --auto-promote
File "README.md", line 1, characters 0-0:
Error: Files _build/default/README.md and
_build/default/.mdx/README.md.corrected differ.
Promoting _build/default/.mdx/README.md.corrected to README.md.
[1]

$ cat README.md
```ocaml
# foo ();;
- : int = 2
```

0 comments on commit 0284642

Please sign in to comment.