This directory contains ReScript's build system and CLI (rescript build,
watch, clean, format, and compiler-args). It ships as rescript in the
@rescript/<platform> packages and is tested with the integration suite in
../tests/build_tests/integration.
The compiler is linked in: parses and compiles run in-process on a pool of
OCaml domains rather than as bsc subprocesses (see
In-process compilation).
From the repository root, with the OPAM dependencies of rescript.opam
installed:
make # builds bsc and packages/@rescript/<platform>/bin/rescript.exe
dune build build_system/rescript.exe # just this executableThe npm launcher (cli/rescript.js) supplies the runtime path. To run the
dune executable directly, point it at the runtime:
export RESCRIPT_RUNTIME="$PWD/packages/@rescript/runtime"
_build/default/build_system/rescript.exe build path/to/projectBuilds and rescript format use the embedded compiler. The bsc.exe next to
rescript.exe is still recorded in lib/rescript/compiler-info.json, where editor
tooling looks for the platform binaries.
Linux release builds use dune's static profile, so the published executable
does not depend on the runner's libc. Its version comes from the shared
rescript_version library (compiler/version/).
| Area | Modules |
|---|---|
| CLI and entry point | cli, rescript, output |
| Configuration | config_types, config_decode (strict JSON decoding), config |
| Projects and packages | project_context (workspace roots and dependency lookup), package_resolution, package_traversal, package_graph, package_plan, package_diagnostics |
| Sources | source, source_filter (--filter), source_dirs |
| Build orchestration | build (one build or watch rebuild), build_preparation, build_session (state kept across watch rebuilds), build_attempt (state of one attempt), build_state, build_report |
| Parsing and compiling | package_parse, package_compilation, compiler_args, compiler_process (logical compiler jobs and their in-process execution), compiler_execution_mode (worker count), compiler_scheduler, compiler_info (fingerprints that decide when a package is cleaned), compile_assets, module_graph, graph |
| Embedded compiler | compiler/driver/rescript_compiler_driver (one request: argv in, exit code and captured output out), compiler/typing/compiler_request_state and compiler_request_output (per-request working directory and output capture) |
| Artifacts | build_artifacts (output paths, publication, stale cleanup), build_freshness, clean, file_util |
| Processes | process (worker pool), process_child (one child and its pipes), after_build |
| Platform | platform.mli implemented by platform_unix.ml or platform_windows.ml (selected by dune), windows_job_stubs.c, termination_signals, build_lock |
| Watching | watcher (rebuild loop), watch_scope (what to watch), watch_snapshot (filesystem snapshots), native_watcher (libuv notifications) |
| Other commands | format, compiler_args_command |
Native filesystem notifications only wake the watcher. The snapshot taken afterwards decides what changed, so merged, reordered, or dropped events can't cause missed rebuilds.
Each package keeps its artifacts flat in lib/rescript, keyed by the
compiler's file name of each module (X or X-Ns): ASTs, .cmi, .cmj,
.cmt and .cmti files, namespace maps, .compiler.log,
compiler-info.json, .sourcedirs.json and, in the project that takes them,
the build and watch locks. The compiler runs in that directory and writes
there directly; dependencies are on its include path as their
lib/rescript.
- Freshness is timestamp-based: an AST newer than its source is current, a
.cmtrecords the last compile, and a.cmithe last change of an interface or of its.cmj. Dependents read the.cmjtoo (the values they copy in, arities, and whether the module has side effects), and it can change without the interface, so a changed.cmjalso dates the.cmi. - ASTs, interfaces,
.cmjfiles 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.cmjon disk. An interface or.cmjis 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 build sees part of one. On Windows the rename fails while an editor has the file open without delete sharing; the file is then written in place, and a failed write removes its partial file.
- To make the next build parse a source again, the build dates its AST back instead of removing it: a later compile of the same build or watch session may still read it.
- Stale public JavaScript is removed only when an AST shows that the build generated it for a source that is gone.
lib/bsandlib/ocaml, left by ReScript 12 and earlier, make the first build clean the package and are removed with it.
Independent parse and compile requests run on a bounded pool of OCaml domains.
The pool has min(8, max(1, CPUs - 1)) workers; set RESCRIPT_COMPILER_DOMAINS
to choose another count: a positive integer, lowered to the machine's limit of
max(12, min(32, CPUs)). The main domain schedules dependencies;
workers run compiler requests and publish what they produce.
The runtime's allocation report at exit (OCAMLRUNPARAM=v=0x400) only counts
the main domain. With RESCRIPT_REPORT_WORKER_ALLOCATIONS set, the build also
prints worker_allocated_words: <n>, the words its workers allocated, which
the compiler benchmarks (scripts/compilerBenchmarks.js) add to it.
With RESCRIPT_TRACE=<file>, the build writes a trace of what each domain did
to the file when it exits, in the Chrome trace event format. Open it in
Perfetto (ui.perfetto.dev) to see it as a timeline:
- The main track has a span per dependency graph (
parse,compile,namespaces,format) and per scheduler step, and counters for the tasks that are ready and running. Gaps on the worker tracks while tasks are ready are time lost to scheduling. - Each worker track has a span per compiler request, named after its input file
and categorized by phase (
parse,interface,implementation,namespace,format). Its arguments hold the words the request allocated and the garbage collections that ran meanwhile. Implementations have child spans for type checking and code generation. Apublishspan follows each request, for publishing the files it wrote.
A relative path is relative to the current directory. A command that records
nothing, such as rescript clean, leaves the file alone. A watch session keeps
its events in memory and writes them when it stops; a killed process writes
nothing. The format is for people and may change.
The driver gives each request fresh compiler state (flags, warnings, caches, predefined types, identifier counters) and restores it on every exit, so requests on different domains don't interfere and repeated requests produce byte-identical artifacts. Compiler output is captured per request instead of going to the process's stdout and stderr. The compiler build identity is derived from the compiler sources, so compiler changes clean the build while build-system-only changes don't.
External PPXs, js-post-build hooks, and --after-build remain subprocesses
under the process owners described below.
rescript.jsonmaps that the build system interprets reject duplicate keys. Anamespace-entrywithout a namespace is an error.- Configuration errors use the compiler-style form
path: message, and JSON syntax errors usepath:line:column: message. Messages name the field and the accepted values. - The CLI uses Cmdliner: help is rendered as a man page,
--no-timingtakes no value, help takes precedence over version, and--versionalso works after a subcommand. --filtertakes a Perl-style regular expression as the Re library reads it. Constructs it cannot match, such as backreferences, are rejected with an error. Non-ASCII names are matched byte-wise.- A failing
--after-buildcommand fails the build. On Unix, hooks inherit redirected stdin but not an interactive terminal, because they run in their own process group. format -vandcompiler-args -vwrite their logs to stderr, so stdout remains formatted source or JSON.- While the root configuration is invalid, only configuration changes trigger recovery attempts in watch mode.
- Watch mode notices a missing dependency being installed into an existing
node_modulesof an ancestor directory, where the dependency lookup finds it. - A damaged published AST, such as one cut short by a killed build, makes its module parse again.
- Unix: children are started with
spawnin their own process groups. SIGINT, SIGTERM, SIGHUP, and SIGQUIT terminate those groups beforerescriptexits. - Windows: children are started through
windows_job_stubs.c, which assigns each child to a Job Object before it runs. Cancellation then also ends its descendants. - Pipes: each child's stdout and stderr are drained by reader threads,
because Windows can't
selecton anonymous pipes. - Watching: uses libuv through
luv.rescript.opam.templatepins luv to a commit of the rescript-lang/luv fork (libuv 1.53).
The test suite compiles platform_windows.ml on every platform, so the Windows
backend is always type-checked. Its process and Job Object paths only run on
Windows CI.
make test-build # all build system tests
make -C tests/build_tests unit # unit tests
make -C tests/build_tests isolated # isolated end-to-end tests
make -C tests/build_tests integration # integration suite in tests/build_tests/integrationThe scripts in tests/build_tests/isolated/check_*.sh check configuration
acceptance, command validation, and verbose and interactive output, partly
against snapshots in tests/build_tests/isolated/snapshots/. Run them with
UPDATE_SNAPSHOTS=1 to rewrite the snapshots after an intended output change.
tests/build_tests/isolated/check_integration_test_coverage.sh ensures that every
integration test script is run by tests/build_tests/integration/suite.sh.
Each platform package contains bin/rescript.exe, together with
THIRD_PARTY_LICENSES for the bundled third-party libraries. After a build,
node scripts/updateArtifactList.js must leave packages/artifacts.json
unchanged.