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
15 changes: 9 additions & 6 deletions build_system/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ there directly; dependencies are on its include path as their

- Freshness is timestamp-based: an AST newer than its source is current, a
`.cmt` records the last compile, and a `.cmi` the last change of an
interface.
- ASTs, interfaces and typed trees are held in memory until their request
finished (`Build_file_store.set_held_writes`), so a failed request leaves
the previous AST and interface on disk. An interface is written only when
it changed, which keeps the modification time that dependents compare
with, and before the typed tree that marks the module compiled, so a build
interface or of its `.cmj`. Dependents read the `.cmj` too (the values they
copy in, arities, and whether the module has side effects), and it can
change without the interface, so a changed `.cmj` also dates the `.cmi`.
- ASTs, interfaces, `.cmj` files and typed trees are held in memory until
their request finished (`Build_file_store.set_held_writes`), so a failed
request leaves the previous AST, interface and `.cmj` on disk. An interface
or `.cmj` is written only when it changed, which keeps the modification time
that dependents compare with, and before the typed tree that marks the
module compiled, so a build
killed in between compiles the module again. A failed compile publishes its
partial typed tree for editor tooling after dating its AST back.
- Published files are renamed into place, so neither a reader nor a killed
Expand Down
83 changes: 54 additions & 29 deletions build_system/compiler_process.ml
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,16 @@ let run_tasks ?trace_label ?poll ?on_complete tasks =

let run_requests requests = requests |> List.map task |> run_tasks

(* A request writes its ASTs, interfaces and typed trees into the artifact
directory that later requests and builds read. They are held in memory
(* A request writes its ASTs, interfaces, .cmj files and typed trees into the
artifact directory that later requests and builds read. They are held in memory
until the request finished (Build_file_store.set_held_writes, which the
build installs with this predicate), so a failed request leaves the
previous AST and interface on disk, and a build killed before it published
an interface leaves the typed tree that marks its module compiled stale. *)
let is_held path =
List.exists
(Filename.check_suffix path)
[".ast"; ".iast"; ".cmi"; ".cmt"; ".cmti"]
[".ast"; ".iast"; ".cmi"; ".cmj"; ".cmt"; ".cmti"]

(* Renamed into place, so that neither a reader nor a build killed while
writing sees part of a file. On Windows the rename fails while another
Expand Down Expand Up @@ -183,7 +183,21 @@ let run_parses ?poll ?on_complete parses =
result))
|> run_tasks ~trace_label:"parse" ?poll ?on_complete

type compiler_artifact = Cmi | Required of string | Typed_tree of string
type compiler_artifact = Cmi | Cmj | Typed_tree of string

(* Publishes a held interface or .cmj only when it changed, and tells whether
it did. The compiler does not write one identical to the file on disk
(Cmi_format.create_cmi, Js_cmj_format.to_file). *)
let publish_if_changed ?(before_write = ignore) path =
try
Build_file_store.publish_held ~if_changed:true
~write:(fun path contents ->
before_write ();
write_held path contents)
path
with Not_found ->
if Build_file_store.exists path then false
else raise (Unix.Unix_error (Unix.ENOENT, "open", path))

(* A typed tree marks its module compiled, so it is published last. Not every
compile writes one (-bs-no-bin-annot). *)
Expand All @@ -206,26 +220,37 @@ let publish_compiler_artifacts ~artifact_dir ~basename artifacts =
(fun artifact ->
match artifact with
| Cmi ->
let path = Filename.concat artifact_dir (basename ^ ".cmi") in
let changed =
try
Build_file_store.publish_held ~if_changed:true ~write:write_held
path
with Not_found ->
(* The compiler does not write an interface identical to the
one on disk (Cmi_format.create_cmi). *)
if Build_file_store.exists path then false
else raise (Unix.Unix_error (Unix.ENOENT, "open", path))
in
cmi_change :=
if changed then Compiler_scheduler.Cmi_changed
if
publish_if_changed
(Filename.concat artifact_dir (basename ^ ".cmi"))
then Compiler_scheduler.Cmi_changed
else Compiler_scheduler.Cmi_unchanged
| Required extension ->
let path =
Filename.concat artifact_dir (basename ^ "." ^ extension)
| Cmj -> (
(* Dependents read the .cmj too: the values they inline, the
arities they call with, and whether the module has side effects,
which decides whether they import it. All of these can change
without the interface. The interface's modification time then
records the change too, which a later build compares with. It is
dated before the .cmj is written, so a build killed in between
still compiles the dependents next time. *)
let date_interface () =
if !cmi_change <> Compiler_scheduler.Cmi_changed then (
Unix.utimes
(Filename.concat artifact_dir (basename ^ ".cmi"))
0. 0.;
cmi_change := Compiler_scheduler.Cmi_changed)
in
if not (Build_file_store.exists path) then
raise (Unix.Unix_error (Unix.ENOENT, "open", path))
match
publish_if_changed ~before_write:date_interface
(Filename.concat artifact_dir (basename ^ ".cmj"))
with
| true -> ()
| false -> (
match !cmi_change with
| Compiler_scheduler.Cmi_change_unknown ->
cmi_change := Compiler_scheduler.Cmi_unchanged
| Cmi_changed | Cmi_unchanged -> ()))
| Typed_tree extension ->
publish_typed_tree
(Filename.concat artifact_dir (basename ^ "." ^ extension)))
Expand Down Expand Up @@ -294,13 +319,13 @@ let namespace_task ~runtime ~artifact_dir ~entry ~package_dirty ~force namespace
(fun extension ->
Build_file_store.forget
(Filename.concat artifact_dir (namespace ^ extension)))
[".cmi"; ".cmt"];
[".cmi"; ".cmj"; ".cmt"];
raise
(Compiler_scheduler.Build_failure
(result.Process.stderr ^ result.stdout)));
let cmi_change =
publish_compiler_artifacts ~artifact_dir ~basename:namespace
[Cmi; Required "cmj"; Typed_tree "cmt"]
[Cmi; Cmj; Typed_tree "cmt"]
in
Compiler_scheduler.{stderr = result.stderr; cmi_change});
})
Expand Down Expand Up @@ -349,6 +374,7 @@ let discard_compile ~artifact_dir ~(config : Config.t) path =
(Source.compiler_asset_basename config path ^ extension)
in
Build_file_store.forget (artifact ".cmi");
Build_file_store.forget (artifact ".cmj");
(try
Build_artifacts.invalidate_ast
(Build_artifacts.ast_path ~artifact_dir path)
Expand All @@ -375,14 +401,13 @@ let publish ~artifact_dir ~is_local ~(config : Config.t) ~has_interface
[Cmi; Typed_tree "cmti"]
| Source.Implementation when has_interface ->
(* The implementation is compiled with -bs-read-cmi, so its .cmi is
the one published after compiling the interface. *)
ignore
(publish_compiler_artifacts ~artifact_dir ~basename
[Required "cmj"; Typed_tree "cmt"]);
Compiler_scheduler.Cmi_unchanged
the one published after compiling the interface. Its .cmj can
still change. *)
publish_compiler_artifacts ~artifact_dir ~basename
[Cmj; Typed_tree "cmt"]
| Source.Implementation ->
publish_compiler_artifacts ~artifact_dir ~basename
[Cmi; Required "cmj"; Typed_tree "cmt"]);
[Cmi; Cmj; Typed_tree "cmt"]);
(* The parse and the typed tree's source digest read the source through
the store. *)
List.iter Build_file_store.evict
Expand Down
1 change: 1 addition & 0 deletions changes/rebuild-dependents-for-cmj-change.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Compile dependents again when a module changes in a way its interface doesn't show, such as a new value behind a `.resi` or a new side effect. Their JavaScript used to keep the old value, or miss the import that runs the side effect.
6 changes: 3 additions & 3 deletions compiler/backend/js_cmj_format.mli
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ val single_na : arity

val from_file : string -> t

(*
Note writing the file if its content is not changed
*)
(* Write the .cmj. Unchanged files are not rewritten when [check_exists] is
true, so the build system can tell from a rewrite that dependents need to
be compiled again. *)
val to_file : string -> check_exists:bool -> t -> unit

type cmj_load_info = {cmj_table: t}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/bin/bash
# Verifies that dependents are compiled again when a module's .cmj changes
# while its interface does not. Dependents read the .cmj for the values they
# copy in and for whether the module has side effects, which decides whether
# they import it.

cd $(dirname $0)
source "../utils.sh"

bold "Test: Rebuild dependents when a .cmj changes behind an interface"

fixture=$(mktemp -d 2>/dev/null || mktemp -d -t build-system-cmj-change)
trap "rm -rf '$fixture'" EXIT

mkdir -p "$fixture/src"

cat > "$fixture/package.json" <<'EOF'
{
"name": "cmj-change",
"version": "0.0.1"
}
EOF

cat > "$fixture/rescript.json" <<'EOF'
{
"name": "cmj-change",
"sources": { "dir": "src" },
"package-specs": { "module": "esmodule", "in-source": true },
"suffix": ".mjs"
}
EOF

# The interface hides the value, so changing it leaves A's .cmi as it is.
printf 'let flag: bool\n' > "$fixture/src/A.resi"
printf 'let flag = true\n' > "$fixture/src/A.res"
printf 'let f = A.flag\n' > "$fixture/src/B.res"

build() {
if ! compiler_output=$(cd "$fixture" && "$RESCRIPT_EXECUTABLE" build 2>&1); then
error "$1: build failed"
printf "%s\n" "$compiler_output" >&2
exit 1
fi
}

build "Initial build"
if ! grep -q 'let f = true' "$fixture/src/B.mjs"; then
error "Expected B to copy in A.flag"
cat "$fixture/src/B.mjs" >&2
exit 1
fi

# A copied value changes.
printf 'let flag = false\n' > "$fixture/src/A.res"
build "Build after changing A.flag"
if ! grep -q 'let f = false' "$fixture/src/B.mjs"; then
error "Expected B to be compiled again with the new value of A.flag"
cat "$fixture/src/B.mjs" >&2
exit 1
fi

# A gains a side effect, so B must import it.
printf 'let flag = false\nConsole.log("effect")\n' > "$fixture/src/A.res"
build "Build after adding a side effect to A"
if ! grep -q 'A.mjs' "$fixture/src/B.mjs"; then
error "Expected B to import A once A has a side effect"
cat "$fixture/src/B.mjs" >&2
exit 1
fi

# An edit that changes neither A's interface nor its .cmj compiles only A.
printf '// A comment\nlet flag = false\nConsole.log("effect")\n' > "$fixture/src/A.res"
build "Build after a line shift in A"
if ! printf '%s\n' "$compiler_output" | grep -q "Compiled 1 module"; then
error "Expected only A to be compiled after a line shift"
printf "%s\n" "$compiler_output" >&2
exit 1
fi

success "Dependents are compiled again exactly when a .cmj changes"
1 change: 1 addition & 0 deletions tests/build_tests/integration/suite.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ fi
./compile/21-rebuild-dependency-for-package-specs.sh &&
./compile/22-qualified-own-namespace-dependency.sh &&
./compile/23-root-source-dir-rebuild.sh &&
./compile/24-rebuild-dependents-for-cmj-change.sh &&
./compile/14-no-testrepo-changes.sh &&
./compile/15-no-new-files.sh &&
./compile/16-snapshots-unchanged.sh &&
Expand Down
71 changes: 71 additions & 0 deletions tests/build_tests/unit/compiler_process_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,20 @@ let publication_releases_files_tests _context =
in
Test_support.write_file (artifact "Same.cmi") "same interface";
Unix.utimes (artifact "Same.cmi") 1_000_000_000. 1_000_000_000.;
Test_support.write_file (artifact "Same.cmj") "code";
Test_support.write_file (artifact "Kept.cmi") "kept interface";
Test_support.write_file (artifact "Kept.cmj") "code";
Test_support.write_file (artifact "Inlined.cmi") "inlined interface";
Test_support.write_file (artifact "Inlined.cmj") "old body";
Unix.utimes (artifact "Inlined.cmi") 1_000_000_000. 1_000_000_000.;
Test_support.write_file (artifact "Hidden.cmi") "interface of Hidden.resi";
Test_support.write_file (artifact "Hidden.cmj") "pure";
Unix.utimes (artifact "Hidden.cmi") 1_000_000_000. 1_000_000_000.;
Test_support.write_file (artifact "Stuck.cmi") "stuck interface";
Unix.utimes (artifact "Stuck.cmi") 1_000_000_000. 1_000_000_000.;
Test_support.write_file (artifact "Stuck.cmj") "old code";
Test_support.write_file (artifact "Failed.cmi") "previous interface";
Test_support.write_file (artifact "Failed.cmj") "previous code";
in_build (fun () ->
List.iter
(fun name ->
Expand Down Expand Up @@ -163,15 +175,74 @@ let publication_releases_files_tests _context =
((publish "src/Kept.res").cmi_change
= Compiler_scheduler.Cmi_unchanged)
"an interface the compiler did not rewrite is no change";
(* Dependents read the .cmj too: inlined values, arities, and
whether the module has side effects. *)
Build_file_store.write (artifact "Inlined.cmj") "new body";
check
((publish "src/Inlined.res").cmi_change
= Compiler_scheduler.Cmi_changed)
"a changed .cmj is a change even when the interface is not";
assert_equal "new body" (File_util.read_file (artifact "Inlined.cmj"));
check
((Unix.stat (artifact "Inlined.cmi")).st_mtime > 1_000_000_000.)
"a changed .cmj dates the interface, which the next build compares \
with";
(* The implementation of a module with an interface publishes no
.cmi, so only its .cmj can tell dependents that it changed. *)
Build_file_store.write (artifact "Hidden.cmj") "has side effects";
check
((Compiler_process.publish ~artifact_dir ~is_local:true ~config
~has_interface:true ~source_kind:Source.Implementation
"src/Hidden.res" succeeded)
.cmi_change = Compiler_scheduler.Cmi_changed)
"a changed .cmj behind an interface is a change";
assert_equal "interface of Hidden.resi"
(File_util.read_file (artifact "Hidden.cmi"))
~msg:"the interface's contents stay those of the .resi";
check
((Unix.stat (artifact "Hidden.cmi")).st_mtime > 1_000_000_000.)
"a changed .cmj behind an interface dates the interface";
(* The interface is dated before the .cmj is written, so a build
that stops in between still compiles the dependents next time.
Permissions don't stop root, so the case needs another user. *)
if Unix.geteuid () <> 0 then begin
Build_file_store.write (artifact "Stuck.cmj") "new code";
(* Comparing still works, but writing the .cmj fails: neither
renaming into the directory nor writing the file in place is
allowed. *)
Unix.chmod (artifact "Stuck.cmj") 0o444;
Unix.chmod artifact_dir 0o555;
let outcome =
match publish "src/Stuck.res" with
| exception
Compiler_scheduler.Publication_failure
(_, Compiler_scheduler.Cmi_changed) ->
true
| exception _ -> false
| _ -> false
in
Unix.chmod artifact_dir 0o755;
Unix.chmod (artifact "Stuck.cmj") 0o644;
check outcome
"a .cmj that cannot be written is reported as a changed interface";
check
((Unix.stat (artifact "Stuck.cmi")).st_mtime > 1_000_000_000.)
"a .cmj that cannot be written has dated the interface already"
end;
Test_support.write_file (artifact "Failed.ast") "ast";
Build_file_store.write (artifact "Failed.cmi") "failed interface";
Build_file_store.write (artifact "Failed.cmj") "failed code";
Build_file_store.write (artifact "Failed.cmt") "partial typed tree";
Compiler_process.discard_compile ~artifact_dir ~config
"src/Failed.res";
check
(Build_file_store.read (artifact "Failed.cmi")
= Some "previous interface")
"a failed compile leaves the previous interface";
check
(Build_file_store.read (artifact "Failed.cmj")
= Some "previous code")
"a failed compile leaves the previous .cmj";
check
((Unix.stat (artifact "Failed.ast")).st_mtime < 2.)
"a failed compile dates its AST back, so the next build compiles \
Expand Down
Loading